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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

comparing to datetime column

Status
Not open for further replies.

zinkjo

Programmer
Apr 12, 2003
8
US
what is the best way to compare a constant such as '07-21-2004' to a datetime column

I want something like the following
select * from tableA where myDateCol = '07-21-2004'

if myDateCol has a value of '07-21-2004 10:15:00' the row should be selected. I want the query to ignore the time portion.
 
Try this:

select * from tableA where Convert(varchar(10),myDateCol,110) = '07-21-2004'

-VJ
 
The only problem with that is that it does a character comparison. so if the constant has '7-21-2004' instead of '07-21-2004' the row will not return.
 
Try this:

select * from tableA where Convert(varchar(10),Convert(datetime,myDateCol),110) = '07-21-2004'

-VJ
 
Than use this: ( just playing with CONVERT and CAST functions )


select * from tableA where Convert(varchar(10),myDateCol,110) = Convert(varchar(10), Cast( '7-21-2004' as datetime ),110)


Than you can use '7-21-2004' or '07-21-2004'

Zhavic



---------------------------------------------------------------
In the 1960s you needed the power of two Comodore64s to get a rocket to the moon. Now you need a machine which is a vast number of times more powerful just to run the most popular GUI.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top