Top5 functions sql2012

484 views

Published on

I was lucky enough to get a chance to present on the first "SQLBangUG" meet. I really enjoyed the 30 minutes and thanks to the UG lead and members for their encouragement. Many Thanks to Jacob, Vinod & Pinal.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
484
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
12
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Top5 functions sql2012

  1. 1. Top 5 Built-in Functions (Transact-SQL) Manas Ranjan Dash @simplymanas
  2. 2. AgendaProgrammability Enhancements (Database Engine)select (my) top 5 * from SQL_BuiltIn_Functions **All the function described in this session are purely related to SQL2012. There is no resemblance with SQL2005 or SQL2008. Compatibility level 110
  3. 3. What is today’s date?What is tomorrow’s date? SELECT GETDATE() SELECT GETDATE()+1
  4. 4. Beginning of the month?SELECT CAST (CAST (YEAR(GETDATE()) AS VARCHAR) + -+CAST(MONTH(GETDATE()) AS VARCHAR) + -+ 01 AS DATE)
  5. 5. Beginning of the month? I am from SQL 2012 SELECT CAST( CONCAT( YEAR(GETDATE()),-, MONTH(GETDATE()),-, 01) AS DATE)
  6. 6. Display Full Name SQL SERVER 2012SELECTSELECTCONCAT( FIRSTNAME + + ISNULL(FIRSTNAME,) + + FIRSTNAME, , MIDDLENAME + + ISNULL(MIDDLENAME,) + + MIDDLENAME, , LASTNAME AS FULLNAME AS FULLNAME ISNULL(LASTTNAME,) LASTNAMEFROM PERSON.PERSON ) AS FULLNAMEFROM PERSON.PERSON
  7. 7. CONCAT() Function BOL CONCAT ( string_value1, string_value2 [, string_valueN ] ) All arguments are implicitly converted to string types and then concatenated. BACHCHAN Null values are implicitly converted to an empty string. If all the arguments are null, an empty string of type varchar(1) is returned. TOP 1
  8. 8. Beginning of the month? SQLSELECT CAST ( SERVER 2012 CONCATSELECT ( DATEFROMPARTS ( YEAR(GETDATE()),-, YEAR(GETDATE()), MONTH(GETDATE()),-, MONTH(GETDATE()), 01 1) )AS DATE)
  9. 9. DATEFROMPARTS() Function BOL DATEFROMPARTS ( year, month, day ) DATEFROMPARTS returns a date value with the date portion BACHCHAN set to the specified year, month and day. TOP 2
  10. 10. End of the month? SELECT DATEADD SQLSELECT DATEADD (D,-1, SERVER (D,-1, DATEADD 2012 DATEFROMPARTS( SELECT CASE WHEN MONTH(GETDATE())<12 DATEFROMPARTS( (D,-1, YEAR(GETDATE()) THEN DECEMBER IIF(MONTH(GETDATE())<12, DATEFROMPARTS(YEAR(GETDATE()), ELSE YEAR(GETDATE())+1 YEAR(GETDATE()),YEAR(GETDATE()+1)), END, MONTH(GETDATE())+1,1) MONTH(GETDATE())+1), IIF(MONTH(GETDATE())=12,1, CASE WHEN MONTH(GETDATE())=12 THEN 1 ) 1) ELSE MONTH(GETDATE())+1) END, 1) )
  11. 11. IIF() Function BOL IIF ( boolean_expression, true_value, false_value) IIF is translated into CASE BACHCHAN IIF statements can also be nested only up to the maximum level of 10 like CASE. TOP 3
  12. 12. End of the month? SQL--THIS MONTH SERVER ENDSELECT DATEADDSELECT EOMONTH(GETDATE()) 2012 (D,-1,--NEXT MONTH END DATEFROMPARTS(SELECT EOMONTH(GETDATE(),1) IIF(MONTH(GETDATE())<12,SELECT EOMONTH(GETDATE(),2) YEAR(GETDATE()),YEAR(GETDATE()+1)),SELECT EOMONTH(GETDATE(),14) IIF(MONTH(GETDATE())=12,1, MONTH(GETDATE())+1),--PREVIOUS MONTH END 1)SELECT EOMONTH(GETDATE(),-1))SELECT EOMONTH(GETDATE(),-2)SELECT EOMONTH(GETDATE(),-14)
  13. 13. EOMONTH() Function BOL EOMONTH ( start_date [, month_to_add ] ) Even if you pass a string parameter you will get a date return type as it does a implicit conversion. BACHCHANDECLARE @date VARCHAR(255)SET @date = 12/1/2011SELECT EOMONTH ( @date ) AS ResultGO TOP 4
  14. 14. Lets Format a Date SELECT CONVERT(VARCHAR, GETDATE(), 101) --11/23/2012 SELECT CONVERT(VARCHAR, GETDATE(), 102) --2012.11.23 SELECT CONVERT(VARCHAR, GETDATE(), 103) --23/11/2012 SQL SELECT CONVERT(VARCHAR, GETDATE(), 104) --23.11.2012 SERVER SELECT CONVERT(VARCHAR, 2012 GETDATE(), 105) --23-11-2012 SELECT CONVERT(VARCHAR, GETDATE(), 108) --10:04:52SELECT FORMAT( GETDATE(), yyyy-MM-dd HH:MM) --2012-11-23 10:11SELECT FORMAT( GETDATE(), "First SQLUG Meet On" ddd dd"th" MMMMyyyy "at" HH:MM )-- First SQLUG Meet On Fri 24th November 2012 at 10:11
  15. 15. FORMAT() Function BOL FORMAT ( value, format [, culture ] ) BACHCHAN Use the FORMAT function for locale-aware formatting of date/time and number values as strings. For general data type conversions, use CAST or CONVERT. TOP 5
  16. 16. CULTURE You can use the SET LANGUAGE statement. Culture accepts any culture supported by the .NET Framework as an argument; it is not limited to the languages explicitly supported by SQL ServerDECLARE @d DATETIME = 01/01/2011;SELECT FORMAT ( @d, d, en-US ) AS US English Result ,FORMAT ( @d, d, en-gb ) AS Great Britain English Result ,FORMAT ( @d, d, de-de ) AS German Result ,FORMAT ( @d, d, zh-cn ) AS Simplified Chinese (PRC) Result;
  17. 17. NEWLY ADDED FUNCTIONSConversion functionsPARSE ()TRY_CONVERT ()TRY_PARSE ()Date and time functionsDATEFROMPARTS ()DATETIME2FROMPARTS () Logical functionsDATETIMEFROMPARTS () CHOOSE ()DATETIMEOFFSETFROMPARTS () IIF () The existing LOG (Transact-EOMONTH () String functions SQL) function now has anSMALLDATETIMEFROMPARTS () CONCAT () optional second base parameter.TIMEFROMPARTS () FORMAT () LOG ( float_expression [, base ] )
  18. 18. Q&A Best Reference: Books Onlinehttp://msdn.microsoft.com/en-us/library/bb510741.aspx
  19. 19. simplymanas@gmail.com @simplymanas
  20. 20. Inspired byVinod Kumar, Jacob Sebastian, Pinal Dave

×