17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Appendix A: System Functions<br />

SYSDATETIMEOFFSET<br />

Similar to SYSDATETIME, this returns the current system date and time. Instead of the simple datetime2<br />

data type, however, SYSDATETIMEOFFSET returns the time in the new datetimeoffset data type (with<br />

a precision of 7), thus providing offset information versus universal time. The syntax is as follows:<br />

SYSDATETIMEOFFSET()<br />

SYSUTCDATETIME<br />

Much like the more venerable GETUTCDATE function, SYSDATETIME returns the current UTC date and<br />

time. SYSDATETIME, however, returns the newer datetime2 data type (to a precision of 7). The syntax is<br />

as follows:<br />

SYSUTCDATETIME()<br />

SWITCHOFFSET<br />

Returns a datetimeoffset value that is changed from the stored time zone offset to a specified new<br />

time zone offset.<br />

So, for example, we can see how the offset gets applied in an example:<br />

SWITCHOFFSET ( , )<br />

CREATE TABLE dbo.test<br />

(<br />

ColDatetimeoffset datetimeoffset<br />

);<br />

GO<br />

INSERT INTO dbo.test<br />

VALUES (‘1998-09-20 7:45:50.71345 -5:00’);<br />

GO<br />

SELECT SWITCHOFFSET (ColDatetimeoffset, ‘-08:00’)<br />

FROM dbo.test;<br />

GO<br />

--Returns: 1998-09-20 04:45:50.7134500 -08:00<br />

SELECT ColDatetimeoffset<br />

FROM dbo.test;<br />

--Returns: 1998-09-20 07:45:50.7134500 -05:00<br />

TODATETIMEOFFSET<br />

608<br />

Accepts a given piece of date/time information and adds a provided time offset to produce a<br />

datetimeoffset data type. The syntax is:<br />

TODATETIMEOFFSET(, )<br />

So, for example:<br />

DECLARE @OurDateTimeTest datetime;<br />

SELECT @OurDateTimeTest = ‘<strong>2008</strong>-01-01 12:54’;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!