Skip to content Skip to sidebar Skip to footer

Sql Server: Getting "before Two Days" Date

I have a select, and I need to limit data only for two last days. So today is 24th Aug 2016 so I need to get data for 24th Aug 2016, 23th Aug 2016 and 22th Aug 2016. How can I do t

Solution 1:

DateAdd, exactly

SELECT*FROM TABLE_NAME 
WHEREDATEBETWEEN  dateadd(d,-2,cast(getDate() asdate)) ANDcast(getDate() asdate)

Solution 2:

SELECT*FROM TABLE_NAME 
WHEREcast(DATEasdate) BETWEENcast(DATEADD(D, -2, GETDATE()) asdate) ANDcast(GETDATE() asdate)

Solution 3:

SELECT*FROM TABLE_NAME WHEREcast(DATEasdate) >=cast(GETDATE()-2asdate)

or --For limiting future dates

SELECT*FROM TABLE_NAME 
    WHEREcast(DATEasdate)  in (cast(GETDATE()-2asdate),cast(GETDATE()-1asdate), cast(GETDATE() asdate))

Use GETDATE() function

Solution 4:

If there are future dates in this table you can perhaps use this

select*from table_name
wheredate<= getdate()
anddate>= dateadd(day,-2,getdate())

Solution 5:

SELECT*FROM TABLE_NAME WHEREDATE>=cast(getdate()-2asdate)--so this will be rounded off to midnight and it is sargable

Post a Comment for "Sql Server: Getting "before Two Days" Date"