SQL Date Comparison

How can I check if the recording date is set to midnight today?

datifik turns me on ...

+1


source to share


5 answers


Here's how to get 0 hours today in SQL

SELECT (CAST(FLOOR(CAST(GETDATE() as FLOAT)) AS DateTime))

      

Just compare your time with this.



Don't use varchar casts as they are slow.

Check this listing for more date reference.

+1


source


Try:



WHERE dtColumn < DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))

      

+6


source


Try the following:

where myColumn < cast( (cast(getdate() - 0.5 as int)) as datetime)

      

0


source


Do you mean the datatype of the column you were using is "TimeStamp" and not dateTime or smalldatetime?

If so, you're out of luck. This datatype has nothing to do with dates or times (it's really misleading ...) It's just guaranteed to be chronologically unique and consistent ... But there is no way to compare it to datetime

http://msdn.microsoft.com/en-us/library/aa260631(SQL.80).aspx

http://www.sqlteam.com/article/timestamps-vs-datetime-data-types

0


source


If ColumnName (the column you are studying) is of datetime datatype and NOT timestamp, then the fastest approach is

Select Case DateDiff(day, columnName, getDate()) 
   When 0 Then 'Today' Else 'Earlier' End
From TableName

      

assuming all date values ​​in ColumnName are now or earlier ...

The easiest way to think of a datiff is that it counts the number of "boundaries" of the specified type that you must pass in order to move from one day to the next ...

0


source







All Articles