Presentation is loading. Please wait.

Presentation is loading. Please wait.

14 T-SQL Functions You May Not Know

Similar presentations


Presentation on theme: "14 T-SQL Functions You May Not Know"— Presentation transcript:

1 14 T-SQL Functions You May Not Know
Sam Nasr, MCSA, MVP NIS Technologies September 29, 2018

2 About Me Software Developer since 1995
Sam Nasr Software Developer since 1995 Sr. Software Engineer (NIS Technologies) Certifications: MCSA, MCAD, MCT, MCTS President - Cleveland C#/VB.Net User Group President - .Net Study Group Author for Visual Studio Magazine Microsoft Most Valuable Professional (since 2013)

3 General Info Down the hall (right side) Participation is encouraged
Please mute cell phones

4 Cleveland C#/VB.Net User Group
Meets every month Free of charge , open to the public All topics related to .Net Meeting info:

5 14 New T-SQL Functions Conversion functions PARSE TRY_CONVERT
TRY_PARSE Date and time functions DATEFROMPARTS DATETIME2FROMPARTS DATETIMEFROMPARTS DATETIMEOFFSETFROMPARTS EOMONTH SMALLDATETIMEFROMPARTS TIMEFROMPARTS Logical functions CHOOSE IIF String functions CONCAT FORMAT

6 14 New T-SQL Functions Conversion functions PARSE TRY_CONVERT
TRY_PARSE Date and time functions DATEFROMPARTS DATETIME2FROMPARTS DATETIMEFROMPARTS DATETIMEOFFSETFROMPARTS EOMONTH SMALLDATETIMEFROMPARTS TIMEFROMPARTS Logical functions CHOOSE IIF String functions CONCAT FORMAT

7 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 Transform Patterns are limited by styles in .Net CLR in System.Globalization.NumberStyles and DateTimeStyles enumerations. NumberStyles.Currency DateTimeStyles.AllowWhiteSpaces DateTimeStyles.AssumeUniversal

8 TRY_CONVERT Converts an expression to a specified type
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 Style: integer expression specifies format of result If style=NULL, NULL is returned. When expression is float or real, style can be one of the values shown in the following table. Other values are processed as 0. 0 (default): A maximum of 6 digits. Use in scientific notation, when appropriate. 1: Always 8 digits. Always use in scientific notation. 2: Always 16 digits. Always use in scientific notation. 126, 128, 129: Included for legacy reasons and might be deprecated in a future release. SELECT CONVERT(binary(4), '4E616D65', 2) AS [Style 2, character to binary];

9 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.

10 Demo Conversion Functions

11 14 New T-SQL Functions Conversion functions PARSE TRY_CONVERT
TRY_PARSE Date and time functions DATEFROMPARTS DATETIME2FROMPARTS DATETIMEFROMPARTS DATETIMEOFFSETFROMPARTS EOMONTH SMALLDATETIMEFROMPARTS TIMEFROMPARTS Logical functions CHOOSE IIF String functions CONCAT FORMAT

12 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

13 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.

14 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

15 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.

16 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.

17 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.

18 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.

19 Demo Date and Time Functions

20 14 New T-SQL Functions Conversion functions PARSE TRY_CONVERT
TRY_PARSE Date and time functions DATEFROMPARTS DATETIME2FROMPARTS DATETIMEFROMPARTS DATETIMEOFFSETFROMPARTS EOMONTH SMALLDATETIMEFROMPARTS TIMEFROMPARTS Logical functions CHOOSE IIF String functions CONCAT FORMAT

21 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) When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence. If the conversion is not a supported implicit conversion, an error is returned. When both operand expressions have the same data type, the result of the operation has that data type. SQL Server uses the following precedence order for data types: user-defined data types (highest) sql_variant xml datetimeoffset datetime2 datetime smalldatetime date time float real decimal money smallmoney bigint int smallint tinyint bit ntext text image timestamp uniqueidentifier nvarchar (including nvarchar(max) ) nchar varchar (including varchar(max) ) char varbinary (including varbinary(max) ) binary (lowest)

22 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. IIF=Immediate IF

23 Demo Logical Functions

24 14 New T-SQL Functions Conversion functions PARSE TRY_CONVERT
TRY_PARSE Date and time functions DATEFROMPARTS DATETIME2FROMPARTS DATETIMEFROMPARTS DATETIMEOFFSETFROMPARTS EOMONTH SMALLDATETIMEFROMPARTS TIMEFROMPARTS Logical functions CHOOSE IIF String functions CONCAT FORMAT

25 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.

26 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}, Some methods, such as String.Format and StringBuilder.AppendFormat, support composite formatting. A composite format string is a kind of template that returns a single string that incorporates the string representation of zero, one, or more objects. Each object is represented in the composite format string by an indexed format item. The index of the format item corresponds to the position of the object that it represents in the method's parameter list. Indexes are zero-based. For example, in the following call to the String.Format method, the first format item, {0:D}, is replaced by the string representation of thatDate; the second format item, {1}, is replaced by the string representation of item1; and the third format item, {2:C2}, is replaced by the string representation of item1.Value.

27 Demo String Functions

28 Recap

29 Recap Conversion functions PARSE TRY_CONVERT TRY_PARSE
Date and time functions DATEFROMPARTS DATETIME2FROMPARTS DATETIMEFROMPARTS DATETIMEOFFSETFROMPARTS EOMONTH SMALLDATETIMEFROMPARTS TIMEFROMPARTS Logical functions CHOOSE IIF String functions CONCAT FORMAT

30 Resources

31 Contact Info @SamNasr Thank you for attending!


Download ppt "14 T-SQL Functions You May Not Know"

Similar presentations


Ads by Google