Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

New TSQL Build In functions:

Eli Leiba


# New Function Example
1 DATETIMEFROMPARTS
DATETIMEFROMPARTS is similar to
DATEFROMPARTS, except it returns
a DateTime value, not just Date.
Syntax: DATETIMEFROMPARTS
(year, month, day, hour, minute,
seconds, milliseconds).
SELECT
DATETIMEFROMPARTS
(2012, 08, 18, 16,
01, 39, 0)


2
DATETIME2FROMPARTS
Building on the
DATETIMEFROMPARTS,
DATETIME2FROMPARTS offers
similar functionality, but yields more
precise DateTime2 data type,
containing fractions of a second to a
specified precision. Naturally, this
means the syntax for the function will
accommodate additional parameters
for fractions and precision. The syntax
for this function is
DATETIME2FROMPARTS (year,
month, day, hour, minute, seconds,
fractions, precision).
SELECT
DATETIME2FROMPARTS
(2012, 8, 18, 14, 23, 44, 50, 2)

3 EOMONTH
The next function is EOMONTH, which
returns the End-Of-Month date for the
month of specified date. The syntax of
the function is EOMONTH (start_date
[, month_to_add]). The second
argument is an optional month_to_add.
This is an integer expression
specifying the number of months to
add to start_date before calculating the
SELECT EOMONTH
('08/18/2012')

End-Of-Month date

4 SMALLDATETIMEFROMPARTS
SMALLDATETIMEFROMPARTS is
very similar to
DATETIMEFROMPARTS, except it
returns a SmallDateTime type. The
syntax for the function is
SMALLDATETIMEFROMPARTS
(year, month, day, hour, minute).
SELECT
SMALLDATETIMEFROMPA
RTS (2012, 8, 18, 14, 30)

5 TIMEFROMPARTS
The last function in the Date/time
category is TIMEFROMPARTS. This
function returns a fully initialized time
value from a set of integer arguments.
Note this function returns only a time
value, not a date/time value. The
syntax for this function is
TIMEFROMPARTS (hour, minute,
seconds, fractions, precision).
SELECT TIMEFROMPARTS
(14, 23, 44, 500, 3)


6 CHOOSE
The CHOOSE function returns the item
at the specified index from a list of
values. The syntax for this function is
CHOOSE (index, val_1, val_2 [,
val_n]), where the index value ranges
from 1-254. Naturally, this means you
can have up to 254 values, of different
type. Please note that most indexes
developers deal with are 0-based; this
function, however is 1-based.
SELECT CHOOSE (3,
'Manager',
'Director',
'Developer',
'Tester') ;


7 IIF
The other new logical function is IIF.
The syntax for the IIF function is IIF
(boolean_expression, true_value,
false_value) . It returns one of two
values, either true_value or
false_value, depending on the
DECLARE @a int = 45;
DECLARE @b int = 40;
SELECT IIF (@a > @b,
'A is greater', 'B is
bigger') ;

evaluation of the Boolean expression.
The data type returned has the highest
precedence of these two data types
(true value, false value).

8 CONCAT
The CONCAT function returns a string
that's the result of concatenating two or
more string values. The syntax for this
function is CONCAT (string_value1,
string_value2 [, string_valueN]),
requiring 2 254 arguments. If the
number of parameters is outside this
range, an error is raised. Arguments
are implicitly converted to string types.
However, NULL values are implicitly
converted to an empty string.
If all the arguments are NULL, an
empty string of type varchar(1) is
returned.
SELECT CONCAT
('Begin>', NULL,
NULL, '<End') ;


9 FORMAT
The second string function is
FORMAT. It returns a value formatted
with the specified format. The syntax
for this function is FORMAT (value,
format [, culture]), with culture being an
optional parameter. The format
parameter must contain a valid .NET
Framework format string. This function
is similar to its counterpart in the .Net
Framework, although with some
differences.
DECLARE @Date
DATETIME =
'08/18/2012';
SELECT FORMAT (@Date,
'd', 'en-US') ;

10 PARSE
PARSE is a T-SQL function that
transforms a string expression into a
date/time or number type. It accepts
two parameters and an optional third
parameter for culture. The syntax for
the statement is PARSE (string value
select Parse ('08-18-
2011' as datetime2
using 'en-US') ;

AS data type [USING culture])



11 TRY_CONVERT
The TRY_CONVERT function converts
an expression to a specified data type.
The syntax format is TRY_CONVERT
(data type [(length) ], expression [,
style]). It's similar to PARSE, but with a
few differences. First, it allows
conversion to any data type. Second, it
will return a NULL if not successful.
select
TRY_CONVERT(datetime2,
'08/18/2012') ;

select
TRY_CONVERT(datetime2
, '99/99/9999') ;


12 DATEFROMPARTS
The next category of the new T-SQL
functions contains date and time
functions. There are seven new
date/time functions.
The first of these is
DATEFROMPARTS. This function is
used to return a date value for
separate integer values of year, month
and day. The function's syntax is
DATEFROMPARTS (year, month,
day).

SELECT DATEFROMPARTS
(2012, 8, 18) ;

You might also like