Monday, January 18, 2010

Date Functions



/*General Syntax */

SELECT DATENAME(datepart,'2007-10-30 12:15:32.1234567 +05:10')

/*Example */

SELECT DATENAME(yyyy,'2007-10-30 12:15:32.1234567 +05:10')



/*

Here is the result set.

datepart                     Return value



year, yyyy, yy                2007

quarter, qq, q                4

month, mm, m                October

dayofyear, dy, y            303

day, dd, d                    30

week, wk, ww                44

weekday, dw                    Tuesday

hour, hh                    12

minute, n                    15

second, ss, s                32

millisecond, ms                123

microsecond, mcs            123456

nanosecond, ns                123456700

TZoffset, tz                310



*/



/*General Syntax */

SELECT DATEPart(datepart,'2007-10-30 12:15:32.1234567 +05:10')

/*Example */

SELECT DATEpart(yyyy,'2007-10-30 12:15:32.1234567 +05:10')



/*

Here is the result set.

datepart                     Return value



year, yyyy, yy                2007

quarter, qq, q                4

month, mm, m                October

dayofyear, dy, y            303

day, dd, d                    30

week, wk, ww                44

weekday, dw                    Tuesday

hour, hh                    12

minute, n                    15

second, ss, s                32

millisecond, ms                123

microsecond, mcs            123456

nanosecond, ns                123456700

TZoffset, tz                310



Key difference between datepart and datename is,

    -    datepart will return numeric value

    -    datename will return both nuermic / text value

*/



/*

DateDiff - it will find and return the differences between two dates.



    First parameter - what difference you want to find ? - year / month /date

    second parameter - comparision date 1

    third parameter - comparision date 2

*/





SELECT DATEDIFF(year, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');



SELECT DATEDIFF(quarter, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');



SELECT DATEDIFF(month, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');



SELECT DATEDIFF(dayofyear, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');



SELECT DATEDIFF(day, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');



SELECT DATEDIFF(week, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');



SELECT DATEDIFF(hour, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');



SELECT DATEDIFF(minute, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');



SELECT DATEDIFF(second, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');



SELECT DATEDIFF(millisecond, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');

No comments:

Post a Comment