Skip to content Skip to sidebar Skip to footer

How Do I Compare Two Timestamps As Dates In Sql Server

I want to run a query to bring back that occurred on or after a date where the input date is a timestamp and the and the table is a timestamp. The problem i have is right now resul

Solution 1:

If you want to consider only the date, then remove the time component:

where datetime >=cast(getdate() asdate)

Of courese, you can do this for a variable or column as well:

where datetime >=cast(@inputdatetimeasdate)

Solution 2:

Now that I understand that you are wanting to compare based solely off of date, I am changing my code to reflect a way to do so:

DECLARE@inputDateTime DATETIME ='2015-01-15 12:13:24'-- DROP TABLE [#testTable]CREATETABLE [#testTable]
(
[Row] INTIDENTITY(1,1),
[DateTime] DATETIME
)

INSERTINTO [#testTable]
    (
    [DateTime]
    )
VALUES
    ('2015-01-16 23:12:11'),
    ('2015-01-15 06:12:24'),
    ('2015-01-14 23:12:24'),
    ('2015-01-15 23:12:24'),
    ('2015-01-12 23:12:24')

SELECT*FROM
    [#testTable]
WHERE
    [DateTime] >=CONVERT(DATETIME,FLOOR(CONVERT(FLOAT,@inputDateTime)))

Post a Comment for "How Do I Compare Two Timestamps As Dates In Sql Server"