SQL date comparison

How do I check if the timestamp date of a record is before midnight today?

datediff is driving me nuts...

Answers


Here is how to get 0 hour of today in SQL

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

Just compare your time against that.

Don't use varchar casts since they are slow.

Check this list for more date time help.


Try:

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

Do you mean that the datatype of the column you ate using is "TimeStamp", not dateTime or smalldatetime ?

If so, then 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 sequential... But there's no way ot compare it with a datetime

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

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


If ColumnName (column you are examining) is of datatype datetime, 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 dates values in ColumnName are now or earlier...

Easiest way to think of datediff is that it counts the number of "boundarys" of the specified type that you have to pass over to get from one datetime to another...


Try this:

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

Need Your Help

information from `label attribute` in R to `VARIABLE LABELS` in SPSS

r export labels spss

I'm working in R, but I need to deliver some data in SPSS format with both 'variable labels' and 'value labels' and I'm kinda stuck.

Compare products in Virtuemart

php mysql joomla virtuemart

I am doing compare page in virtuemart. I want to modify joomla\components\com_virtuemart\views\category\tmpl\default.php file in this way: