Notes from Daily Encounters with Technology RSS 2.0
 
# Tuesday, April 18, 2006

Today I’ve been evaluating database designs made by this year’s candidates for work at our company. One of the issues I’ve been encountering all the time was the handling of date and time values. The SQL Server’s common data type datetime for both values has been the source of quite some confusion, especially since the values required to be separate at some occasions and joined at others.

I suppose the decision on whether to store them separate or joined depends on how the values are going to be used. In most cases the extra storage space required by using separate columns for both values isn’t worth it but in some usage scenarios the resulting reduced query complexity and improved indexing usage can justify it.

Either way sooner or later the need for separating or joining the date and time parts will arise. The following query demonstrates both operations in what I believe is the most efficient way:

SELECT
   dateOnly = CONVERT(datetime, FLOOR(CONVERT(float, dateTimeColumn))),
   timeOnly = dateTimeColumn - CONVERT(datetime, FLOOR(CONVERT(float, dateTimeColumn))),
   dateAndTime = dateOnlyColumn + timeOnlyColumn 
FROM DateTable

Tuesday, April 18, 2006 11:38:07 PM (Central European Daylight Time, UTC+02:00)  #    Comments [1] - Trackback
Development | SQL
Saturday, April 22, 2006 2:22:19 PM (Central European Daylight Time, UTC+02:00)
I would say you think wrong :)))
try this:
SELECT DATEADD(d, DATEDIFF(d, 0, GetDate()), 0),
DATEADD(d, -DATEDIFF(d, 0, GetDate()), GetDate())

only 2 function uses and no conversion which is slow.
Comments are closed.
Sponsored Ads

About Me
Twitter
Potepanja v naravi: Abram na Nanosu http://t.co/vtlUEWJg 55 minutes ago
@MladenPrajdic @andrejt use the middle mouse button then 2 days ago
Great #DotNetRocks show: Troy Hunt Secures http://t.co/oxClbXLe http://t.co/MiMasNuZ PDF is worth checking out as well http://t.co/z4BHAzqh 3 days ago
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

All Content © 2012, Damir Arh, M. Sc. Send mail to the author(s) - Privacy Policy - Sign In
Based on DasBlog theme 'Business' created by Christoph De Baene (delarou)
Social Network Icon Pack by Komodo Media, Rogie King is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.