programming4us
         
 
 
SQL Server

SQL server 2012 : T-SQL Enhancements - Date and Time Data Types (part 2) - Date and Time Functions

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
7/10/2013 7:28:13 PM

Date and Time Functions

All of the traditional date-related and time-related functions, including DATEADD, DATEDIFF DATEDIFF, DATEPART, and DATENAME, of course fully support the newer date and time data types, and several functions have been added as well. We conclude our discussion of the SQL Server 2008 date and time data types by exploring the T-SQL extensions added to support them.

The SYSDATETIME and SYSUTCDATETIME functions return the date and time on the server as datetime2 types (with full seven-scale precision accuracy within 100 nanoseconds), just as the GETDATE and GETUTCDATE functions continue to return the current date and time as datetime types. Another new function, SYSDATETIMEOFFSET, returns the date and time on the server as a datetimeoffset type, with a time zone offset reflecting the regional settings established on the server, which includes awareness of local daylight savings time. The code in Example 3 shows the contrast between the various similar server date and time functions.

Example 3. Comparing server date and time functions.

SET NOCOUNT ON
SELECT GETDATE() AS 'GETDATE() datetime'
SELECT GETUTCDATE() AS 'GETUTCDATE() datetime'
SELECT SYSDATETIME() AS 'SYSDATETIME() datetime2'
SELECT SYSUTCDATETIME() AS 'SYSUTCDATETIME() datetime2'
SELECT SYSDATETIMEOFFSET() AS 'SYSDATETIMEOFFSET() datetimeoffset'

Running this code just after 8:20 PM on February 10, 2012 in New York results in the following output:

GETDATE() datetime
-----------------------
2012-02-10 20:21:19.380

GETUTCDATE() datetime
-----------------------
2012-02-11 01:21:19.380

SYSDATETIME() datetime2
---------------------------
2012-02-10 20:21:19.3807984

SYSUTCDATETIME() datetime2
---------------------------
2012-02-11 01:21:19.3807984

SYSDATETIMEOFFSET() datetimeoffset
----------------------------------
2012-02-10 20:21:19.3807984 -05:00

The TODATETIMEOFFSET and SWITCHOFFSET functions allow you to perform time zone offset manipulations. TODATETIMEOFFSET will convert any date or time type (that has no time zone offset) to a datetimeoffset type by applying whatever time zone offset you provide. SWITCHOFFSET makes it easy to find out what the same time is in two different time zones. You provide the datetimeoffset for a source location and a time zone offset for a target location, and SWITCHOFFSET returns a datetimeoffset representing the equivalent date and time in the target location, as shown in Example 4.

Example 4. Performing time zone offset manipulations using TODATETIMEOFFSET and SWITCHOFFSET.

DECLARE @TheTime datetime2
DECLARE @TheTimeInNY datetimeoffset
DECLARE @TheTimeInLA datetimeoffset

-- Hold a time that doesn't specify a time zone
SET @TheTime = '2012-02-10 7:35PM'

-- Convert it into one that specifies time zone for New York
SET @TheTimeInNY = TODATETIMEOFFSET(@TheTime, '-05:00')

-- Calculate the equivalent time in Los Angeles
SET @TheTimeInLA = SWITCHOFFSET(@TheTimeInNY , '-08:00')

SELECT @TheTime AS 'Any Time'
SELECT @TheTimeInNY AS 'NY Time'
SELECT @TheTimeInLA AS 'LA Time'

Here is the output result:

Any Time
---------------------------
2012-02-10 19:35:00.0000000

NY Time
----------------------------------
2012-02-10 19:35:00.0000000 -05:00

LA Time
----------------------------------
2012-02-10 16:35:00.0000000 -08:00

You can use TODATETIMEOFFSET with INSERT INTO…SELECT to bulk-insert date and time values with no time zone information from a source table into a target table and to apply a time zone offset to produce datetimeoffset values in the target table. For example, the following code copies all the row values from the dt2 column in table test1 (of type datetime2, which has no time zone information) into the dto column in test2 (of type datetimeoffset) and applies a time zone offset of –05:00 to each copied value:

INSERT INTO test2(dto)
 SELECT TODATETIMEOFFSET(dt2, '-05:00') FROM test1

The next example retrieves all the datetimeoffset values from the dto column in the test2 table, which can include values across a variety of different time zones. Using a SWITCHOFFSET function that specifies an offset of –05:00, the values are automatically converted to New York time from whatever time zone is stored in the test2 table:

SELECT SWITCHOFFSET(dto, '-05:00') FROM test2

Last, both the existing DATEPART and DATENAME functions have been extended to add support for microseconds (mcs), nanoseconds (ns), and time zone offsets (tz) in the higher-precision and time zone-aware types, as shown in Example 5.

Example 5. Using the new date portions in SQL Server 2008 with DATEPART and DATENAME.

SET NOCOUNT ON
DECLARE @TimeInNY datetimeoffset
SET @TimeInNY = SYSDATETIMEOFFSET()

-- Show the current time in NY
SELECT @TimeInNY AS 'Time in NY'

-- DATEPART with tz gets the time zone value
SELECT DATEPART(tz, @TimeInNY) AS 'NY Time Zone Value'

-- DATENAME with tz gets the time zone string
SELECT DATENAME(tz, @TimeInNY) AS 'NY Time Zone String'

-- Both DATEPART and DATENAME with mcs gets the microseconds
SELECT DATEPART(mcs, @TimeInNY) AS 'NY Time Microseconds'

-- Both DATEPART and DATENAME with ns gets the nanoseconds
SELECT DATEPART(ns, @TimeInNY) AS 'NY Time Nanoseconds'

Running this code returns the following output:

Time in NY
----------------------------------
2012-02-10 20:50:55.7851424 -05:00

NY Time Zone Value
------------------
-300

NY Time Zone String
------------------------------
-05:00

NY Time Microseconds
--------------------
785142

NY Time Nanoseconds
-------------------
785142400
Other -----------------
- SQL server 2012 : T-SQL Enhancements - Table-Valued Parameters (part 2)
- SQL server 2012 : T-SQL Enhancements - Table-Valued Parameters (part 1)
- SQL Server 2008 R2 : Database Files and Filegroups (part 2)
- SQL Server 2008 R2 : Database Files and Filegroups (part 1)
- Installing SQL Server 2012 : The Installation Process (part 4) - Post Installation Tasks
- Installing SQL Server 2012 : The Installation Process (part 3) - Installing SQL Server 2012 Through the Command Line, Installing SQL Server 2012 Through PowerShell
- Installing SQL Server 2012 : The Installation Process (part 2) - Installing SQL Server 2012 Through the Installation Center
- Installing SQL Server 2012 : The Installation Process (part 1) - SQL Server 2012 Installation Center
- Installing SQL Server 2012 : Preparing the Server, Selecting the Edition
- SQL Server 2012 : SQL Server Architecture - SQL SERVER’S EXECUTION MODEL AND THE SQLOS
- SQL Server 2012 : SQL Server Architecture - THE LIFE CYCLE OF A QUERY (part 3) - A Simple Update Query
- SQL Server 2012 : SQL Server Architecture - THE LIFE CYCLE OF A QUERY (part 2) - Plan Cache
- SQL Server 2012 : SQL Server Architecture - THE LIFE CYCLE OF A QUERY (part 1)
- Protecting SQL Server Data : CELL-LEVEL ENCRYPTION - Views and Stored Procedures (part 2) - Creating the Stored Procedures
- Protecting SQL Server Data : CELL-LEVEL ENCRYPTION - Views and Stored Procedures (part 1) - Creating the View
- Protecting SQL Server Data : Implementing Cell-Level Encryption
- Protecting SQL Server Data : Preparing for Cell-Level Encryption
- Microsoft SQL Server 2008 R2 : Monitoring Replication (part 2) - New and Improved Peer-to-Peer Replication
- Microsoft SQL Server 2008 R2 : Monitoring Replication (part 1) - Replication Monitoring SQL Statements
- Microsoft SQL Server 2008 R2 : Scripting Replication
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us
Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS
Celebrity Style, Fashion Trends, Beauty and Makeup Tips.