Today I noticed an odd little quirk about TSQL (Microsoft SQL) and SQL Server. If you have a smalldatetime, you only record date + hours + minutes. To get seconds, millis, and such, you need the full datetime.
Well, if you are using "getdate()" to compare against the smalldatetime, then know that smalldatetime rounds the seconds UP to the next minute. The smalldatetime on 12:36:24.00 is actually 12:37:00.00.
This caught me today in a query where I was doing a select looking for a record that occured after the last insertion time (smalldatetime) using "getdate()". That was always returning empty because the getdate() to smalldatetime conversion was giving me a date that was in the future thanks to the round-up behavior.
To work around it, I had to convert(smalldatetime,getdate()) and then compare against my smalldatetime values.
If you are inserting records with smalldatetime and using getdate() as the smalldatetime value, then those records will be recorded up to 1 minute into the future. Be wary.
Well, if you are using "getdate()" to compare against the smalldatetime, then know that smalldatetime rounds the seconds UP to the next minute. The smalldatetime on 12:36:24.00 is actually 12:37:00.00.
This caught me today in a query where I was doing a select looking for a record that occured after the last insertion time (smalldatetime) using "getdate()". That was always returning empty because the getdate() to smalldatetime conversion was giving me a date that was in the future thanks to the round-up behavior.
To work around it, I had to convert(smalldatetime,getdate()) and then compare against my smalldatetime values.
If you are inserting records with smalldatetime and using getdate() as the smalldatetime value, then those records will be recorded up to 1 minute into the future. Be wary.