SQL statement with ¸variable date

Guitarde

Board Regular
Joined
Mar 18, 2003
Messages
244
Hello everyone,

I am trying to extract the invoice listing of the day from our SQL server.
With the following sql statement it works ok (date greater than is set in the code);

stSQL = "SELECT t207_invoicing.date_maj, t207_invoicing.invoicing_invoice_number " & _
"FROM D73JRD.dbo.t207_invoicing t207_invoicing " & _
"WHERE (t207_invoicing.date_maj>={ts '2011-04-26 00:00:00'})"


What I want to extract is any invoices made today, so I am trying to include today's date into a variable an pass it into the sql criterias. I have tried several different ways but I always get some errors at the time of date extraction.


Dim vDateMin As date
vDateMin = Year(Now()) & "-" & Month(Now()) & "-" & Day(Now())

stSQL = "SELECT t207_invoicing.date_maj, t207_invoicing.invoicing_invoice_number " & _
"FROM D73JRD.dbo.t207_invoicing t207_invoicing " & _
"WHERE (t207_invoicing.date_maj>={ts '" & vDateMin & "'})"



I am using the code of XL Dennis for the query / SQL tester.

I tried several ways, with and without the ' , ts or }.
Please advise if you have any ideas to solve my sql statement.


Thanks
Eric
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hi Eric - you just need to format today's date:

Code:
stSQL = "SELECT t207_invoicing.date_maj, t207_invoicing.invoicing_invoice_number " & _
"FROM D73JRD.dbo.t207_invoicing t207_invoicing " & _
"WHERE (t207_invoicing.date_maj>={ts '" & Format(Date,"yyyy-mm-dd") & " 00:00:00'})"
 
Upvote 0

Forum statistics

Threads
1,224,607
Messages
6,179,871
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top