This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*OFFSET FETCH Filter | |
Offset by 5 and filter next 5 rows*/ | |
SELECT orderid, orderdate, custid, empid | |
FROM Sales.Orders | |
ORDER BY orderdate, orderid | |
OFFSET 5 ROWS FETCH NEXT 5 ROWS ONLY; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*ROW_NUMBER() Window function assigns | |
unique incrementing integers to rows */ | |
SELECT orderid, custid, val, ROW_NUMBER() OVER (PARTITION BY custid ORDER BY val) AS rownum | |
FROM Sales.OrderValues | |
ORDER BY custid, val; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*CONCAT function (2012) | |
Handles replacing nulls*/ | |
Use TSQL2012 | |
SELECT custid, country, region, city, | |
CONCAT(country, N',' + region, N',' + city) | |
AS location | |
FROM Sales.Customers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*FORMAT Function | |
formats a number as a 10-digit string with leading zeros*/ | |
SELECT FORMAT(1759, 'd10') AS Cust_string; | |
/*formats a currency to various locale-aware strings*/ | |
SELECT FORMAT(5,'C','en-us') AS US_Value; --US | |
SELECT FORMAT(5,'C','de-de') AS DE_Value; --Germany |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*DATEFROMPARTS | |
example returns the a date using provide parts and input*/ | |
SELECT DATEFROMPARTS(2014,02,28) AS NewDate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*EOMONTH Function | |
Example returns the last day of 06/01/2014*/ | |
SELECT EOMONTH('06/01/2014') AS EndofMonth; | |
/*Example returns the last day of 06/01/2014 + 6months*/ | |
SELECT EOMONTH('06/01/2014',6) AS EndofSixthMonth; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Example IIF function*/ | |
DECLARE @a int = 65, @b int = 25; | |
SELECT IIF (@a < @b, 'Yes', 'No') AS Result; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Example Choose function*/ | |
SELECT CHOOSE ( 4, 'Option 1', 'Option 2', 'Option 3', 'Option 4' ) AS Result; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*ROW_NUMBER() Window function assigns | |
unique incrementing integers to rows */ | |
SELECT orderid, custid, val, ROW_NUMBER() OVER (PARTITION BY custid ORDER BY val) AS rownum | |
FROM Sales.OrderValues | |
ORDER BY custid, val; |
OlderNewer