Presentation is loading. Please wait.

Presentation is loading. Please wait.

14 New T-SQL Functions By Sam Nasr, MCAD, MCTS. MVP Nasr Information Systems February 8, 2014.

Similar presentations


Presentation on theme: "14 New T-SQL Functions By Sam Nasr, MCAD, MCTS. MVP Nasr Information Systems February 8, 2014."— Presentation transcript:

1 14 New T-SQL Functions By Sam Nasr, MCAD, MCTS. MVP Nasr Information Systems February 8, 2014

2

3 Introduction Sam Nasr (sam@nasr.info)sam@nasr.info Software developer since 1995 Founder - Nasr Information Systems MCAD, MCT, MCTS(WSS/MOSS) President - Cleveland C#/VB.Net User Group President - Cleveland WPF User Group President -.Net Study Group INETA Mentor for Ohio INETA Community Champ (2010, 2013) Author for Visual Studio Magazine

4 Housekeeping Restrooms Forum for learning Feel free to ask questions Cell Phones on mute please

5 What is your background?.Net Developer T-Sql Developer DBA Hobbyist/other role

6 System Requirements (Recommended) 6GB HD min (feature dependant) 4GB RAM min (increased per DB size) 2.0 GHz CPU x86 or x64 Vista, Win 7, Win Server 2008 NTFS recommended (FAT32 supported).Net 3.5 SP1/4.0 Note: All requirements are version dependant. See MSDN for more installation details.

7 14 New T-SQL Functions Conversion functions 1. PARSE 2. TRY_CONVERT 3. TRY_PARSE Date and time functions 4. DATEFROMPARTS 5. DATETIME2FROMPARTS 6. DATETIMEFROMPARTS 7. DATETIMEOFFSETFROMPARTS 8. EOMONTH 9. SMALLDATETIMEFROMPARTS 10. TIMEFROMPARTS Logical functions 11. CHOOSE 12. IIF String functions 13. CONCAT 14. FORMAT

8 14 New T-SQL Functions Conversion functions 1. PARSE 2. TRY_CONVERT 3. TRY_PARSE Date and time functions 4. DATEFROMPARTS 5. DATETIME2FROMPARTS 6. DATETIMEFROMPARTS 7. DATETIMEOFFSETFROMPARTS 8. EOMONTH 9. SMALLDATETIMEFROMPARTS 10. TIMEFROMPARTS Logical functions 11. CHOOSE 12. IIF String functions 13. CONCAT 14. FORMAT

9 Parse PARSE ( string_value AS data_type [ USING culture ] ) Transforms a string expression to date/time or number type. Optional clause [Using Culture] Requires.Net CLR (cannot be remoted) Use only for converting strings to datetime and numbers For general type conversions, continue to use CAST or CONVERT Transform Patterns are limited by styles in.Net CLR

10 TRY_CONVERT TRY_CONVERT ( data_type [ ( length ) ], expression [, style ] ) Converts an expression to a specified type Returns null if not successful. Returns error when cast is explicitly not permitted Allows for optional use of styles.

11 TRY_PARSE TRY_PARSE ( string_value AS data_type [ USING culture ] ) Similar to PARSE Function Returns null if value is not a valid representation of data_type PARSE Function raises an error.

12 Demo Conversion Functions

13 14 New T-SQL Functions Conversion functions 1. PARSE 2. TRY_CONVERT 3. TRY_PARSE Date and time functions 4. DATEFROMPARTS 5. DATETIME2FROMPARTS 6. DATETIMEFROMPARTS 7. DATETIMEOFFSETFROMPARTS 8. EOMONTH 9. SMALLDATETIMEFROMPARTS 10. TIMEFROMPARTS Logical functions 11. CHOOSE 12. IIF String functions 13. CONCAT 14. FORMAT

14 DATEFROMPARTS DATEFROMPARTS ( year, month, day ) Returns a date value for separate integer values of year, month, and day. It cannot be executed remotely on an earlier version of SQL Server. All values must be valid date values

15 DATETIMEFROMPARTS DATETIMEFROMPARTS ( year, month, day, hour, minute, seconds, milliseconds ) DATETIMEFROMPARTS returns a fully initialized datetime value. If the arguments are not valid, then an error is raised. If required arguments are null, then a null is returned. Cannot be executed remotely on an earlier version of SQL Server.

16 DATETIME2FROMPARTS DATETIME2FROMPARTS ( year, month, day, hour, minute, seconds, fractions, precision ) Returns a datetime2 value for separate integer values of year, month, day, hour, minutes, seconds, fractions, precision All values must be valid date/time values “Precision” must accommodate “fractions” value Max precision is 7

17 DATETIMEOFFSETFROMPARTS DATETIMEOFFSETFROMPARTS ( year, month, day, hour, minute, seconds, fractions, hour_offset, minute_offset, precision ) Returns a datetimeoffset value for separate integer values of year, month, day, hour, minutes, seconds, fractions, precision, and time offset. Offset arguments represent the time zone offset. If required arguments are null, a null is returned. If “Precision” is null, an error is raised.

18 EOMONTH EOMONTH ( start_date [, month_to_add ] ) Returns End-Of-Month date for the month of specified date. Optional month_to_add integer expression for number of months to add to start_date. month_to_add is added to start_date and returns the last day of the month for the resulting date. If this addition overflows the valid range of dates, then an error is raised.

19 SMALLDATETIMEFROMPARTS SMALLDATETIMEFROMPARTS ( year, month, day, hour, minute ) Similar to DATETIMEFROMPARTS Returns a smalldatetime value. If the arguments are not valid, then an error is thrown. If required arguments are null, then null is returned. Cannot be executed remotely on an earlier version of SQL Server.

20 TIMEFROMPARTS TIMEFROMPARTS ( hour, minute, seconds, fractions, precision ) Returns a fully initialized time value. If the arguments are invalid, then an error is raised. If any of the parameters are null, null is returned. If the precision argument is null, then an error is raised.

21 Demo Date and Time Functions

22 14 New T-SQL Functions Conversion functions 1. PARSE 2. TRY_CONVERT 3. TRY_PARSE Date and time functions 4. DATEFROMPARTS 5. DATETIME2FROMPARTS 6. DATETIMEFROMPARTS 7. DATETIMEOFFSETFROMPARTS 8. EOMONTH 9. SMALLDATETIMEFROMPARTS 10. TIMEFROMPARTS Logical functions 11. CHOOSE 12. IIF String functions 13. CONCAT 14. FORMAT

23 CHOOSE CHOOSE ( index, val_1, val_2 [, val_n ] ) Returns the item at the specified index from a list of values. Note: Index is 1-based Returns the data type with the highest precedence from the set of types passed to the function Note: T-SQL has 30 types UDT (Highest) …. Binary (Lowest)

24 IIF IIF ( boolean_expression, true_value, false_value ) Returns the respective value, depending on evaluation of Boolean expression. Returns the data type with the highest precedence from the types in true_value and false_value. IIF is translated into a CASE statement. IIF & CASE statements can be nested only up to 10 max IIF is remoted as a semantically equivalent CASE statement.

25 Demo Logical Functions

26 14 New T-SQL Functions Conversion functions 1. PARSE 2. TRY_CONVERT 3. TRY_PARSE Date and time functions 4. DATEFROMPARTS 5. DATETIME2FROMPARTS 6. DATETIMEFROMPARTS 7. DATETIMEOFFSETFROMPARTS 8. EOMONTH 9. SMALLDATETIMEFROMPARTS 10. TIMEFROMPARTS Logical functions 11. CHOOSE 12. IIF String functions 13. CONCAT 14. FORMAT

27 CONCAT CONCAT ( string_value1, string_value2 [, string_valueN ] ) Returns a string that is the result of concatenating two or more string values. Requires 2 – 254 arguments or an error is raised. Arguments are implicitly converted to string types Null values are implicitly converted to an empty string If all the arguments are null, an empty string of type varchar(1) is returned.

28 FORMAT FORMAT ( value, format [, culture ] ) Returns a value formatted with the specified format Optional culture parameter Format must contain a valid.NET Framework format string Composite formatting is not supported: Format("Name = {0}, Birthday= {1:hh}", @myName, @myBDay);

29 Demo String Functions

30 Recap Conversion functions 1. PARSE 2. TRY_CONVERT 3. TRY_PARSE Date and time functions 4. DATEFROMPARTS 5. DATETIME2FROMPARTS 6. DATETIMEFROMPARTS 7. DATETIMEOFFSETFROMPARTS 8. EOMONTH 9. SMALLDATETIMEFROMPARTS 10. TIMEFROMPARTS Logical functions 11. CHOOSE 12. IIF String functions 13. CONCAT 14. FORMAT

31 Resources http://visualstudiomagazine.com/Articles/2013/01/01/ 4-New-TSQL-Functions.aspx?Page=1 http://visualstudiomagazine.com/Articles/2013/01/01/ 4-New-TSQL-Functions.aspx?Page=1

32 Contact Info sam@nasr.info http://ClevelandDotNet.blogspot.com @SamNasr http://www.linkedin.com/in/samsnasr http://speakerrate.com/samnasr Thank you for attending!


Download ppt "14 New T-SQL Functions By Sam Nasr, MCAD, MCTS. MVP Nasr Information Systems February 8, 2014."

Similar presentations


Ads by Google