martedì 20 novembre 2012

Datetime without hours, minutes and seconds

If you need to get date from datetime:

select dateadd(dd,0,datediff(dd,0,getdate()))

If you want only date without hours, minutes and seconds:
SELECT convert(varchar, getdate(), 103) 

103 format is dd/mm/yyyy

My Two Cents ...

T-SQL, Calculate Age From Date of Birth

Sometime could be useful calculate age from date of birth.
The following t-sql code shows how could be done it.

datediff(yy, date_of_birth, GETDATE()) -
       (case when (datepart(m date_of_birth) > datepart(m, GETDATE()))OR
                   (datepart(m date_of_birth) = datepart(m, GETDATE()) AND
                    datepart(d date_of_birth) > datepart(d, GETDATE()))
                        then 1
                        else 0
        end) AS AGE

My Two Cents...