Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL & VB DATE COMPARISON

Status
Not open for further replies.

dicolas

Technical User
Joined
Oct 22, 2001
Messages
7
Location
CA
Hi guys, I'm looking for the best way to compare SQL date with VB date (only date - not time). This way needs to work wathever the SQL dateformat and login language in use.

In a query, I'm using something like :

"... AND CONVERT(DATETIME, CONVERT(INT, CONVERT(FLOAT, MySqlDate))) = '" & Format(int(MyVBDate), sqlDateFormat) & "'"

SQL code is to only select DATE from a datetime field (without time)

VB code is to get DATE without TIME (int function) and to put it in the correct sql date format.

I define the format depending on login language :

SELECT dateformat FROM master.dbo.syslanguages WHERE
(name = (SELECT @@LANGUAGE)).

Case "dmy" => sqlDateformat = "d-m-yy"
Case "mdy" => sqlDateformat = "m-d-yy"
Case "ymd" => sqlDateformat = "yy-m-d"
(It seems that there is no "ydm" SQL dateformat)

Do someone has better suggestion?
Thanks a lot

Regards. Nicolas ;-)


 
You can simply convert the datetime to a date using the Convert function. Use the VB format function to prepare the comparison date.

Example: ODBC format - SQL style 120

Select * From MyTable
Where Convert(varchar(10), MyDateTime, 120)='2002-02-11'

Example: ISO format - SQL Style 112

Select * From MyTable
Where Convert(varchar(10), MyDateTime, 112)='20020211'

See Cast and Convert in SQL BOL for more information about converting dates and times.


There is an excellent article about writing VB code that is compatible with SQL Server or Access at the following link.

Terry L. Broadbent - Salt Lake City, UT
Home of the 2002 Winter Olympics (Feb 8-24)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top