Tuesday, July 24, 2007

How to Get Week-Day and Quarter of year in SQL Server 2005

  • You can get the week-day by using the DATEPART scalar valued function of the SQL Server 2005. For example the following statement returns 1 (one) as it is default value for Sunday.
SELECT DATEPART(weekday,'07/22/2007' )
or
SELECT DATEPART(dw,'07/22/2007' )

Result = 1
-------------------------------------------------------
The following are the default values for the week days:

SUNDAY = 1
MONDAY= 2
TUESDAY = 3
WEDNESDAY=4
THURSDAY= 5
FRIDAY = 6
SATURDAY = 7

  • Similarly you can check the quarter of the year by using one of the following query. SELECT DATEPART(quarter,'07/22/2007' ) or
    SELECT DATEPART(qq,'07/22/2007' ) or
    SELECT DATEPART(q,'07/22/2007' )
Result: 3 ( as this is 3rd quarter of the year)

You can get the details from MSDN online, Here is the reference of the MSDN:
MSDN ONLINE

No comments: