Friday, February 17, 2012

Date Comparison

I have two tables that I am needing to link by the tables date field. In one table the date field is defined as varchar(23). The time for this field is always zeros.
Example: '2005-12-27 00:00:00.000'

The other table is defined as datetime, and it does have the date and time in this field.
Example: 2005-12-27 08:00:35.000

The problem i am having is
2005-12-27 00:00:00.000 does not = 2005-12-27 08:00:35.000.

Because I will never have more than one record on the same date I would like to be able to only compare the date. Example 2005-12-27 = 2005-12-27

Since the fields are 2 different field types, this is giving me a problem. Could someone please help. I have tried everything I know to do.

What I really need is the a way to format the datetime fields date into a string such as '2005-12-27'.

Hopefully what i have written makes sense.

Thanksselect cast(convert(varchar(11),getdate()) as Datetime)

this eliminates the time stamp and sets it to 2006-05-27 00:00:00.000

This will allow you to join

Suggestion make your varchar field a date field.|||Or,Use the below code to convert date to varchar and compare with ur varchar field

select convert(varchar,DATEADD(dd, DATEDIFF(dd,0,getdate()), 0),121)|||That worked perfect! Thanks!

No comments:

Post a Comment