Share via

In SQL How is code written for Updating a SQL Database with Dates Between Date From and DateTo and Using ID Column

Gary Simpson 471 Reputation points
2022-02-25T13:55:39.847+00:00

Hi Good People

How Is Code Written for updating A SQL Database, Where the Date is between a Date From to a Date To AND where a ID column is used.

The code I have at the moment. Does not work, After " WHERE "

Code Below

 Private Sub UpdatePayment()

        SQL.AddParam("@DriverID", LbDriverID.Text)
        SQL.AddParam("@DateFrom", LbDateFrom.Text)
        SQL.AddParam("@DateTo", LbDateTo.Text)
        SQL.AddParam("@DatePaid", LbDatePaid.Text)
        SQL.AddParam("@GrossPay", LbGrossPay.Text)
        SQL.AddParam("@TaxableIncome", LbTaxableIncome.Text)
        SQL.AddParam("@TaxDeductions", LbTaxDeductions.Text)
        SQL.AddParam("@N_I_Deductions", LbN_I_Deductions.Text)
        SQL.AddParam("@NetPay", LbNetPay.Text)
        SQL.AddParam("@UpdatedBy", LbUpdatedBy.Text)
        SQL.AddParam("@DateUpdated", LbDateUpdated.Text)

        SQL.ExecQuery("UPDATE DailyRecords " &
                       "SET DatePaid=@DatePaid, " &
                       "GrossPay=@GrossPay, " &
                       "TaxableIncome=@TaxableIncome, " &
                       "TaxDeductions=@TaxDeductions, " &
                       "N_I_Deductions=@N_I_Deductions, " &
                       "NetPay=@NetPay, " &
                       "UpdatedBy=@UpdatedBy, " &
                       "DateUpdated=@DateUpdated " &
                       "WHERE DriverID=@DriverID And DateFrom=@DateFrom Between DateTo=@DateTo")



    End Sub

Can anyone help with the Code after Where. I need to use the DriverID column then Dates Between.

Kind Regards
Gary

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories


Answer accepted by question author

Viorel 127K Reputation points
2022-02-25T14:26:13.473+00:00

If there is a column called "Date", then try this: "WHERE DriverID=@DriverID And [Date] between @DateFrom And @DateTo".

Was this answer helpful?

1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. LiHong-MSFT 10,061 Reputation points
    2022-02-28T02:31:58.203+00:00

    Hi @Gary Simpson
    Please check :

    WHERE DriverID=@DriverID And DateFrom>=@DateFrom AND DateTo<=@DateTo  
    --Or  
    WHERE DriverID=@DriverID And DateFrom Between @DateFrom AND @DateTo And DateTo Between @DateFrom AND @DateTo   
    

    Best regards,
    LiHong

    Was this answer helpful?

    0 comments No comments

  2. Naomi Nosonovsky 8,906 Reputation points
    2022-02-25T16:51:22.577+00:00

    Try
    "WHERE DriverID=@DriverID And (DateFrom<=@DateTo and DateTo>=@DateFrom")

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.