Datetime conversion in T-SQL

  • I need to compare two dates in the stored procedure: one is datetime that gets value from the system by getDate() function and another is from the database.

    Both values have format "MMM DD YYYY HH:MM", but the system datetime gives current HH:MM, when the database values have 00:00.

    I don't care about time, I just want to compare the dates.

    Question: Is there a simple way to convert these datetime values into the plain date format "MM/DD/YYYY"?

    Thank you for the help

  • Simplest way I know is as follows:

    cast(floor(cast(getdate() as float)) as datetime)

    This will give the current date with no time.

  • Also you can use,

    convert(varchar(10),getdate(),101) = convert(varchar(10),'07/15/2004',101)

  • Well, thanks for the help. I'm almost there.

    Just tried these functions. They produced date with time "12:00 am". But the dates I have in my database have time "00:00". 

    So, I need to figure out how to set time to "00:00" to be able to compare the values.

  • You can use:

    SET DATEFORMAT mdy

    This doesn't affect the way the date is stored in SQL server, only how it is displayed. 

  • Thanks everybody for the help!

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply