14 New T-SQL Functions By Sam Nasr, MCAD, MCT, MCTS NIS

Slides:



Advertisements
Similar presentations
Moving from Access Databases to a Visual Studio/SQL Server Solutions Andrew Couch UK Access User Group asc associates
Advertisements

By Sam Nasr Nasr Information Systems May 14, 2013.
Project Management Database and SQL Server Katmai New Features Qingsong Yao
Copyright ©2014 Pearson Education, Inc. Chapter 6 Physical Design Chapter6.1.
Working with Data Types February 7, 2015 John Deardurff Website:
Module 2 Working with Data Types. Module Overview Using Data Types Working with Character Data Converting Data Types Working with Specialized Data Types.
BARBARIN DAVID SQL Server Senior Consultant Pragmantic SQL Server Denali : New development features.
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
Using New Data Types in 2008 Andrew Couch UK Access User Group ASC associates
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Sizing Basics  Why Size?  When to size  Sizing issues:  Bits and Bytes  Blocks (aka pages) of Data  Different Data types  Row Size  Table Sizing.
SQL Data Definition Language (DDL) Using Microsoft SQL Server 1SDL Data Definition Language (DDL)
11 3 / 12 CHAPTER Databases MIS105 Lec15 Irfan Ahmed Ilyas.
What’s New In Denali - TSQL David Ballantyne. Who am I Kent.Net/SqlServer.
Data Types Lesson 4. Skills Matrix Table A table stores your data. Tables are relational in that they are organized as rows and columns (a matrix). Each.
EXPRESSION Transformation. Introduction ►Transformations help to transform the source data according to the requirements of target system and it ensures.
SQL Server 2005 Implementation and Maintenance Chapter 3: Tables and Views.
SQL ORACLE 1. Data Type 2 3 VARCHAR2(size [BYTE | CHAR]) Variable-length character string having maximum length size bytes or characters. Maximum size.
03 | SQL Server Data Types Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
Data types  CHAR (size): This data type is used to store character strings values of fixed length. The size in brackets determines the number of characters.
IMS 4212: Data Modeling—Attributes and Domains 1 Dr. Lawrence West, Management Dept., University of Central Florida Attributes and Domains.
Academic Year 2015 Autumn. MODULE CC2006NI: Data Modelling and Database Systems Academic Year 2015 Autumn.
SQL server Section 2&3. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.
14 New T-SQL Functions By Sam Nasr, MCAD, MCT, MCTS NIS August 18, 2012.
14 New T-SQL Functions By Sam Nasr, MCAD, MCTS. MVP Nasr Information Systems February 8, 2014.
What is your Character Data Type? March 5, 2016 John Deardurff Website:
BI Development By Sam Nasr, MCAD, MCT, MCTS Nasr Information Systems February 5, 2011.
Creating Database Objects
FileTables Sam Nasr, MCAD, MCTS, MVP NIS Technologies
Programming with FileStream Object
Temporal Tables Sam Nasr, MCSA, MVP NIS Technologies July 22, 2017
Managing Tables, Data Integrity, Constraints by Adrienne Watt
Module 2: Creating Data Types and Tables
Lecture 6 Data Model Design (continued)
Building Applications with LUIS
FileTables By Sam Nasr, MCAD, MCT, MCTS NIS October 6, 2012
Data Definition and Data Types
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Attributes and Domains
Using FileTables Sam Nasr, MCSA, MCT, MVP NIS Technologies
Ouch! Our Data Type Choices Did THAT?
Using FileTables Sam Nasr, MCAD, MCTS, MVP NIS Technologies
SQL Server 2016 JSON Support FOR Data Warehousing
SQL Implementation & Administration
Working with SQL Server 2016 Data Types
Microsoft SQL Server Managing Database Applications
SQL Server 2016 Security Features
14 T-SQL Functions You May Not Know
Migrating a Disk-based Table to a Memory-optimized one in SQL Server
Proper DataType Usage = Guaranteed Better Performance and Accuracy
What is your Character Data Type?
SQL Server 2008 By Sam Nasr March 25, 2008.
Working with Data Types
Database systems Lecture 2 – Data Types
A JSON’s Journey through SQL Server
CIS16 Application Programming with Visual Basic
PT2520 Unit 5: Physical Design
Data Types Do Matter Start local instance of SQL Start ZoomIt
14 T-SQL Functions You May Not Know
Integrating REST API and SQL Server JSON Functions
Attributes and Domains
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Summary of what we learned yesterday
Creating Database Objects
Using FileTables Sam Nasr, MCSA NIS Technologies August 3, 2019.
Introduction to SQL Server and the Structure Query Language
Working with SQL Server 2016 Data Types
Data Time Travel with Temporal Tables
Presentation transcript:

14 New T-SQL Functions By Sam Nasr, MCAD, MCT, MCTS NIS October 6, 2012

Housekeeping Forum for learning - Feel free to ask questions Cell Phones on vibrate please Exit stairways at end of hallway Restrooms are near elevators Please fill out surveys and event evals (see room monitor) Presentations will be posted on SQL Saturday web site

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

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

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.

Demo Environment Intel core Duo CPU 2.40 GHz 4GB RAM 500 GB HD Visual Studio 2010 Ultimate (64-bit) SQL Server 2008/2012 Enterprise (64-bit) Windows 7 Ultimate (64-bit) Note: Windows Server 2008 implemented using “Shrink volume…” Shrink Volume: Control Panel > Administrative Tools > Computer Management > Disk Management; right click C:

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

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

Parse PARSE ( string_value AS data_type [ USING culture ] ) Transforms a string expression to date/time or number type. Optional “Using Culture” parameter 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

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 Allows for optional use of styles. 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];

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.

Demo Conversion Functions

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

DATEFROMPARTS DATEFROMPARTS ( year, month, day ) Returns a date value for separate integer values of year, month, and day. DATEFROMPARTS is capable of being remoted to SQL Server 2012 servers and above. It cannot be remoted to servers with a version previous to SQL Server 2012. All values must be valid date values

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. This function is capable of being remoted to SQL Server 2012 servers and above. It will not be remoted to servers that have a version below SQL Server 2012.

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

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.

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.

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. This function is capable of being remoted to SQL Server 2012 servers and above. It will not be remoted to servers that have a version below SQL Server 2012.

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.

Demo Date and Time Functions

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

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)

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.

Demo Logical Functions

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

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.

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

Demo String Functions

Conclusion Let’s recap…

Additional Changed Function LOG ( float_expression [, base ] ) Returns the natural logarithm of the specified float expression. Optional integer argument sets the base for the logarithm. Hint: Log (Exp(n)) = n Log (a, b) = c; b^c = a

Thank you for attending! Contact Info E-mail: sam@nasr.info Blog: http://ClevelandDotNet.blogspot.com Twitter: http://twitter.com/samnasr LinkedIN: http://www.linkedin.com/in/samsnasr SpeakerRate: http://speakerrate.com/samnasr Thank you for attending!