Microsoft® SQL Server® 2012 Bible

(Ben Green) #1

193


Chapter 8: Data Types, Expressions, and Scalar Functions


8


day dd, d
week wk, ww
weekday dw
hour hh
minute mi, n
second ss, s
millisecond ms
microsecond mcs
nanosecond ns
TZoffset tz

This code gets the month and weekday name:

select DATENAME(MONTH,CURRENT_TIMESTAMP) as "Month",
DATENAME(WEEKDAY,CURRENT_TIMESTAMP) As "Day"
Result:

Month Day
-------- -----------
February Tuesday

This code gets the month and weekday name and displays the results in Italian:

Set language Italian
select DATENAME(MONTH,CURRENT_TIMESTAMP) as "Month",
DATENAME(WEEKDAY,CURRENT_TIMESTAMP) As "Day"

Result:
Month Day
-------- -----------
Febbraio Martedi

For more information about datetime, datetime2, and other data types, refer to Chapter 2, “Data
Architecture.”

The following code example retrieves the proper names of some of the portions of the order
date using the DateName() function:

USE ADVENTUREWORKS
GO
SELECT OrderDate,
DATENAME(yy,OrderDate) AS [Year],
DATENAME(mm,OrderDate) AS [Month],
DATENAME(dd,OrderDate) AS [Day],
DATENAME(weekday, OrderDate) AS Today

c08.indd 193c08.indd 193 7/30/2012 4:21:15 PM7/30/2012 4:21:15 PM


http://www.it-ebooks.info
Free download pdf