Wednesday, 29 June 2011

New Date and Time Functions in SQL Server 2008

Introduction

SQL Server 2008 introduces five new date and time functions namely SYSDATETIME, SYSDATETIMEOFFSET, SYSUTCDATETIME, SWITCHOFFSET and TODATETIMEOFFSET.

In this article described new functions in detail.


1)    SYSDATETIME

The SYSDATETIME function basically returns the current system timestamp without the time zone information.


SELECT SYSDATETIME()

GO

 

SYSDATETIME

------------------------

2011-06-27 16:06:15.9409129


2)    SYSDATETIMEOFFSET

The SYSDATETIMEOFFSET function is much similar to SYSDATETIME function; however it will also get you the time zone information.


SELECT SYSDATETIMEOFFSET()

GO

 

SYSDATETIMEOFFSET

-------------------------------

2011-06-27 16:07:01.8205935 +05:30

 

 

3)    SYSUTCDATETIME

The SYSUTCDATETIME function returns the Greenwich Mean Time (GMT) or the Coordinated Universal Time (UTC). This time is derived from the current local system time and the time zone settings of the server where SQL Server 2008 is running.


SELECT SYSUTCDATETIME()

GO

 

SYSUTCDATETIME

------------------------

2011-06-27 10:38:25.6239407


4)    SWITCHOFFSET

The SWITCHOFFSET function returns a DATETIMEOFFSET value that is changed from the stored time zone offset to a specified new time zone offset.


SELECT SYSDATETIMEOFFSET(),

       SWITCHOFFSET (SYSDATETIMEOFFSET(), '+00:00')

GO

 

SYSDATETIMEOFFSET                                            SWITCHOFFSET

-------------------------------                              -------------------------------------

2011-06-27 16:30:55.7127120 +05:30           2011-06-27 11:00:55.7127120 +00:00


5)    TODATETIMEOFFSET

The TODATETIMEOFFSET function returns a DATETIMEOFFSET value that is translated from a DATETIME2 expression.


SELECT TODATETIMEOFFSET (GETDATE(),'+05:30')

GO

 

TODATETIMEOFFSET

---------------------------------

2011-06-27 16:26:56.340 +05:30



3 comments:

  1. i want to convert the date into the same format of getdate() to have comparision with the

    date passed from front.? how can i do that..?


    alquiler de carros orlando

    ReplyDelete
    Replies
    1. Please go through the link - http://msdn.microsoft.com/en-us/library/ms187928.aspx

      It specifies all the different formats you can get using the CAST or the CONVERT functions.

      However, in you case you need not have the same format to compare 2 DateTime values as long as they are of DateTime data type. Please first convert your values to DateTime and then you can compare them normally.

      Hope, this helps!

      Delete
  2. Woah! I'm really digging the template/theme of this site. It's ѕіmple, yet effеctivе.
    A lot of times іt's difficult to get that "perfect balance" between superb usability and visual appearance. I must say that you'ѵe ԁone a very good job ωith
    this. Additionally, the blοg loads very quicκ foг me
    on Inteгnet explorеr. Excellеnt Blog!


    my wеblog :: cheap belly Button Rings

    ReplyDelete