T-SQL

Rediscovered with
SQL Server 2012
GO BIG!
Jacob Sebastian, SQL Server MVP
jacob@beyondrelational.com

Agenda

What is today’s date?
GETDATE()

What is Tomorrow’s Date? GETDATE()+ 1 .

Beginning of the Month? SELECT CAST ( CAST(YEAR(GETDATE()) AS VARCHAR) + '-' + CAST(MONTH(GETDATE()) AS VARCHAR) + '-' + '01' AS DATE ) .

MONTH(GETDATE()).'-'.Beginning of the Month? SELECT CAST ( CAST(YEAR(GETDATE()) AS VARCHAR) + '-' + CAST(MONTH(GETDATE()) AS VARCHAR) + '-' + '01' SQL AS DATE Server ) 2012 SELECT CAST ( CONCAT ( YEAR(GETDATE()).'01' ) AS DATE ) .'-'.

' '.'') + ' ' + ISNULL(LastName.CONCAT() Function SELECT FirstName + ' ' + MiddleName + ' ' + LastName AS CustomerName FROM Customers SELECT ISNULL(FirstName. MiddleName. LastName) AS CustomerName FROM Customers .'') AS CustomerName FROM Customers SELECT CONCAT(FirstName.'') + ' ' + ISNULL(MiddleName. ' '.

MONTH(GETDATE()).1) .'01' ) AS DATE SQL Server ) 2012 SELECT DATEFROMPARTS(YEAR(GETDATE()).Beginning of the Month? SQL Server 2012 SELECT CAST ( CONCAT ( YEAR(GETDATE()).'-'.MONTH(GETDATE()).'-'.

MONTH(GETDATE()) + 1. -1. 1 ) ) . MONTH(GETDATE()) + 1.End of the Month? SELECT DATEFROMPARTS ( YEAR(GETDATE()). DATEFROMPARTS( YEAR(GETDATE()). 1 ) SELECT DATEADD ( D.

DATEFROMPARTS( CASE WHEN MONTH(GETDATE()) < 12 THEN YEAR(GETDATE()) ELSE YEAR(GETDATE()) + 1 END. -1. CASE WHEN MONTH(GETDATE()) = 12 THEN 1 ELSE MONTH(GETDATE()) + 1 END. 1 ) ) .End of the Month? SELECT DATEADD ( D.

IIF(MONTH(GETDATE()) = 12. 0.Server 2012 DATEFROMPARTS( 2012 YEAR(GETDATE()) + IIF(MONTH(GETDATE()) < 12. 1). SQL -1. MONTH(GETDATE()) + 1). 1. 1 ) ) .End of the Month? SELECT DATEADD ( SQL Server D.

IIF() Function SELECT CustName. 'Yes'. CASE IsActive WHEN 1 THEN 'Yes' ELSE 'No' END AS IsActive FROM Customers SELECT CustName. 'No') AS IsActive FROM Customers . IIF(IsActive = 1.

1 ) ) SQL Server 2012 SELECT EOMONTH(GETDATE()) . 1). DATEFROMPARTS( YEAR(GETDATE()) + IIF(MONTH(GETDATE()) < 12. IIF(MONTH(GETDATE()) = 12. 1. 0. -1.End of the Month? SELECT DATEADD ( D. MONTH(GETDATE()) + 1).

Format Date: 2011-01-01 12:30 PRINT CONVERT(VARCHAR. 101) -.03/13/2012 . GETDATE().

13 .03/13/2012 PRINT CONVERT(VARCHAR.2012. 101) -.03. GETDATE().Format Date: 2011-01-01 12:30 PRINT CONVERT(VARCHAR. GETDATE(). 102) -.

Format Date: 2011-01-01 12:30 PRINT CONVERT(VARCHAR.03.13 PRINT CONVERT(VARCHAR. 102) -.03/13/2012 PRINT CONVERT(VARCHAR. 101) -. GETDATE(). GETDATE(). GETDATE().2012. 103) -.13/03/2012 .

101) 102) 103) 104) ----- 03/13/2012 2012.Format Date: 2011-01-01 12:30 PRINT PRINT PRINT PRINT CONVERT(VARCHAR. CONVERT(VARCHAR.03. CONVERT(VARCHAR. CONVERT(VARCHAR. GETDATE().13 13/03/2012 13. GETDATE().2012 . GETDATE().03. GETDATE().

Format Date: 2011-01-01 12:30 PRINT PRINT PRINT PRINT PRINT CONVERT(VARCHAR. GETDATE().13 13/03/2012 13.03. CONVERT(VARCHAR.2012 13-03-2012 . 101) 102) 103) 104) 105) ------ 03/13/2012 2012. GETDATE(). GETDATE(). CONVERT(VARCHAR. GETDATE(). CONVERT(VARCHAR.03. CONVERT(VARCHAR. GETDATE().

CONVERT(VARCHAR. CONVERT(VARCHAR.03. GETDATE(). CONVERT(VARCHAR.13 13/03/2012 13.03.2012 13-03-2012 13 Mar 2012 . GETDATE(). GETDATE(). GETDATE(). CONVERT(VARCHAR. 101) 102) 103) 104) 105) 106) ------- 03/13/2012 2012. CONVERT(VARCHAR. GETDATE(). GETDATE().Format Date: 2011-01-01 12:30 PRINT PRINT PRINT PRINT PRINT PRINT CONVERT(VARCHAR.

03.2012 13-03-2012 13 Mar 2012 Mar 13.03. GETDATE(). CONVERT(VARCHAR. CONVERT(VARCHAR. GETDATE(). GETDATE(). GETDATE(). GETDATE(). CONVERT(VARCHAR. 101) 102) 103) 104) 105) 106) 107) -------- 03/13/2012 2012. GETDATE(). CONVERT(VARCHAR.Format Date: 2011-01-01 12:30 PRINT PRINT PRINT PRINT PRINT PRINT PRINT CONVERT(VARCHAR. GETDATE(). CONVERT(VARCHAR. CONVERT(VARCHAR.13 13/03/2012 13. 2012 .

CONVERT(VARCHAR. CONVERT(VARCHAR. GETDATE(). 101) 102) 103) 104) 105) 106) 107) 108) --------- 03/13/2012 2012. 2012 20:33:13 . GETDATE(). CONVERT(VARCHAR. GETDATE(). GETDATE().03. CONVERT(VARCHAR. GETDATE(). GETDATE().2012 13-03-2012 13 Mar 2012 Mar 13. GETDATE(). CONVERT(VARCHAR.13 13/03/2012 13.Format Date: 2011-01-01 12:30 PRINT PRINT PRINT PRINT PRINT PRINT PRINT PRINT CONVERT(VARCHAR. CONVERT(VARCHAR. GETDATE().03. CONVERT(VARCHAR.

13 13/03/2012 13.'. GETDATE().2012 13-03-2012 13 Mar 2012 Mar 13. GETDATE(). GETDATE(). GETDATE(). 108). CONVERT(VARCHAR. 102). 5) PRINT FORMAT(GETDATE(). GETDATE().Format Date: 2011-01-01 12:30 PRINT PRINT PRINT PRINT PRINT PRINT PRINT PRINT CONVERT(VARCHAR. CONVERT(VARCHAR. 101) 102) 103) 104) 105) 106) 107) 108) --------- 03/13/2012 2012. CONVERT(VARCHAR. GETDATE().03. CONVERT(VARCHAR. CONVERT(VARCHAR. CONVERT(VARCHAR. GETDATE(). 'yyyy-MM-dd hh:mm') . GETDATE(). '. '-') Server + ' ' + 2012 LEFT(CONVERT(VARCHAR. GETDATE(). 2012 20:33:13 PRINT SQL REPLACE(CONVERT(VARCHAR. CONVERT(VARCHAR.03. GETDATE().

FORMAT() Function PRINT FORMAT( GETDATE().Demonstrated On" yyyy-MM-dd "at" hh:mm') -.Demonstrated on 2012-03-13 at 09:03 PRINT FORMAT(GETDATE(). 'yyyy-MM-ddThh:mm:ssZ') 2012-03-13T09:03:03Z . '"-.

Error Handling .

Pagination .

Previous. Next. First. Last .

2012 . 2012 DECLARE @dt DATE = PARSE('03/05/2012' AS DATE USING 'en-US') -. 2012 -.May 3. 2012 DECLARE @dt DATE = PARSE('03/05/2012' AS DATE USING 'en-US') -.March 5.March 5.PARSE() DECLARE @dt DATE = '03/05/2012' SELECT @dt -.May 3.

Loaded from a file SELECT TRY_PARSE(@str AS DATE USING 'en-US') -.TRY_PARSE() DECLARE @str CHAR(10) = '15/03/2012' –.NULL SELECT TRY_PARSE(@str AS DATE USING 'en-GB') -.March 15.Loaded from a file DECLARE @dt DATE = PARSE(@str AS DATE USING 'en-US') DECLARE @str CHAR(10) = '15/03/2012' –. 2012 .

'VARCHAR(100)') DECLARE @x VARCHAR(100) = '<Students name="jacob"></students>' SELECT TRY_CONVERT(xml.value('(/Students/@name)[1]'. @x). 'VARCHAR(100)') .TRY_CONVERT() DECLARE @x VARCHAR(100) = '<Students name="jacob"></students>' SELECT CAST(@x as xml).value('(/Students/@name)[1]'.

Accessing Previous and Next Rows .

Accessing Previous and Next Rows ID Time Type 1 2011-01-01 10:00:00 IN 1 2011-01-01 18:00:00 OUT 1 2011-01-02 10:00:00 IN 2 2011-01-01 10:05:00 IN 2 2011-01-01 19:00:00 OUT 3 2011-01-01 11:00:00 IN 3 2011-01-01 17:00:00 OUT .

Accessing Previous and Next Rows ID Time Type 1 2011-01-01 10:00:00 IN 1 2011-01-01 18:00:00 OUT 1 2011-01-02 10:00:00 IN 2 2011-01-01 10:05:00 IN 2 2011-01-01 19:00:00 OUT 3 2011-01-01 11:00:00 IN 3 2011-01-01 17:00:00 OUT .

Accessing Previous and Next Rows Row# ID Time Type 1 1 2011-01-01 10:00:00 IN 2 1 2011-01-01 18:00:00 OUT 3 1 2011-01-02 10:00:00 IN 1 2 2011-01-01 10:05:00 IN 2 2 2011-01-01 19:00:00 OUT 1 3 2011-01-01 11:00:00 IN 2 3 2011-01-01 17:00:00 OUT .

Accessing Previous and Next Rows Row ID Time 1 1 2 Type Row ID Time Type 2011-01-01 10:00:00 IN 1 1 2011-01-01 10:00:00 IN 1 2011-01-01 18:00:00 OUT 2 1 2011-01-01 18:00:00 OUT 3 1 2011-01-02 10:00:00 IN 3 1 2011-01-02 10:00:00 IN 1 2 2011-01-01 10:05:00 IN 1 2 2011-01-01 10:05:00 IN 2 2 2011-01-01 19:00:00 OUT 2 2 2011-01-01 19:00:00 OUT 1 3 2011-01-01 11:00:00 IN 1 3 2011-01-01 11:00:00 IN 2 3 2011-01-01 17:00:00 OUT 2 3 2011-01-01 17:00:00 OUT .

Accessing Previous and Next Rows .

Questions .

Thank You .

Resources Developers http://msdn.micorsoft.com/ msdnindia @msdnindia Infrastructure Professionals http://technet.microsoft.com/ technetindia @technetindia .

Microsoft. All rights reserved. AS TO THE INFORMATION IN THIS PRESENTATION. EXPRESS. . MICROSOFT MAKES NO WARRANTIES. Because Microsoft must respond to changing market conditions.© 2012 Microsoft Corporation. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. Windows. Windows Vista and other product names are or may be registered trademarks and/or trademarks in the US and/or other countries. IMPLIED OR STATUTORY. it should not be interpreted to be a commitment on the part of Microsoft.

Sign up to vote on this title
UsefulNot useful