Tag Archives: SQL SERVER 2012

SMALLDATETIMEFROMPARTS FUNCTION IN SQL SERVER 2012

SMALLDATETIMEFROMPARTS is one of the new built-in Date and Time Function introduced as a Part of Sql Server 2012. Returns a SMALLDATETIME value for the specified date and time. It is a Sql Server native function not dependent on the .NET CLR.

SYNTAX: SMALLDATETIMEFROMPARTS ( year, month, day, hour, minute )

WHERE: year, month, day, hour and minute are integer expressions representing valid year, month, day, hour and minute respectively.
Return Type: SMALLDATETIME

Example 1: SMALLDATETIMEFROMPARTS – with valid date and time part integer constants

SELECT 
 SMALLDATETIMEFROMPARTS(2013,6,23,1,20 ) AS 'SMALLDATETIME',
 SMALLDATETIMEFROMPARTS(1900,1,1,0,0) AS 'MIN SMALLDATETIME',
 SMALLDATETIMEFROMPARTS(2079,6,6,23,59) AS 'MAX SMALLDATETIME'

Result:

SMALLDATETIME         MIN SMALLDATETIME         MAX SMALLDATETIME
———————–           ———————–                   ———————–
2013-06-23 01:20:00   1900-01-01 00:00:00             2079-06-06 23:59:00

Example 2: SMALLDATETIMEFROMPARTS – with valid date and time part integer variables

DECLARE @year INT = 2013, @month INT = 6, @day INT = 23,
		@hour INT = 1, @minute INT = 20
SELECT SMALLDATETIMEFROMPARTS( @year, @month, @day, 
		@hour , @minute) AS 'SMALLDATETIME'

Result:

SMALLDATETIME
———————–
2013-06-23 01:20:00

Example 3: SMALLDATETIMEFROMPARTS – with Invalid part

DECLARE @year INT = 2013, @invalidmonth INT = 15, @day INT = 23,
		@hour INT = 1, @minute INT = 20
SELECT SMALLDATETIMEFROMPARTS( @year, @invalidmonth, @day, 
		@hour , @minute) AS 'SMALLDATETIME'

Result:
Msg 289, Level 16, State 4, Line 3
Cannot construct data type smalldatetime, some of the arguments have values which are not valid.

Example 4: SMALLDATETIMEFROMPARTS – with one of the part as NULL

DECLARE @year INT = 2013, @invalidmonth INT = 15, @day INT = 23,
		@hour INT = 1, @minute INT = NULL
SELECT SMALLDATETIMEFROMPARTS( @year, @invalidmonth, @day, 
		@hour , @minute) AS 'SMALLDATETIME'

Result:
SMALLDATETIME
———————–
NULL

You may like to read the below new built-in function’s introduced in Sql Server 2012:

New Built in Functions introduced in Sql Server
CONVERSION FUNCTIONS
PARSE TRY_PARSE
TRY_CONVERT
STRING FUNCTIONS
CONCAT FORMAT
LOGICAL FUNCTIONS
CHOOSE IIF
DATE AND TIME FUNCTIONS
EOMONTH
DATEFROMPARTS DATETIMEFROMPARTS
SMALLDATETIMEFROMPARTS DATETIME2FROMPARTS
TIMEFROMPARTS DATETIMEOFFSETFROMPARTS

DATEFROMPARTS FUNCTION IN SQL SERVER 2012

DATEFROMPARTS is one of the new built-in Date and Time Function introduced as a Part of Sql Server 2012. As name suggests it forms a DATE from it’s parts i.e. Year, Month and Day. It is a Sql Server native function not dependent on the .NET CLR.

SYNTAX: DATEFROMPARTS ( year, month, day )

WHERE: year, month and day are integer expressions representing valid year, month and day respectively.
Return Type: DATE

Example 1: DATEFROMPARTS – with valid date part integer constants

SELECT DATEFROMPARTS( 2013, 6, 23 ) AS 'DATE',
	DATEFROMPARTS( 1, 1, 1 ) AS 'MIN DATE',
	DATEFROMPARTS( 9999, 12, 31 ) AS 'MAX DATE'

Result:
DATE                    MIN DATE                 MAX DATE
———-                ———-                       ———-
2013-06-23       0001-01-01              9999-12-31

Example 2: DATEFROMPARTS – with valid date part integer variables

DECLARE @year INT = 2013, @month INT = 6, @day INT = 23
SELECT DATEFROMPARTS( @year, @month, @day) AS 'DATE'

Result:
DATE
———-
2013-06-23

Example 3: DATEFROMPARTS – with invalid month part

DECLARE @year INT = 2013, @invalidmonth INT = 15, @day INT = 23
SELECT DATEFROMPARTS( @year, @invalidmonth, @day) AS 'DATE'

Result:
Msg 289, Level 16, State 1, Line 2
Cannot construct data type date, some of the arguments have values which are not valid.

Example 4: DATEFROMPARTS – with one of the part as NULL

DECLARE @year INT = 2013, @month INT = 6, @day INT = NULL
SELECT DATEFROMPARTS( @year, @month, @day) AS 'DATE'

Result:
DATE
———-
NULL

You may like to read the below new built-in function’s introduced in Sql Server 2012:

New Built in Functions introduced in Sql Server
CONVERSION FUNCTIONS
PARSE TRY_PARSE
TRY_CONVERT
STRING FUNCTIONS
CONCAT FORMAT
LOGICAL FUNCTIONS
CHOOSE IIF
DATE AND TIME FUNCTIONS
EOMONTH
DATEFROMPARTS DATETIMEFROMPARTS
SMALLDATETIMEFROMPARTS DATETIME2FROMPARTS
TIMEFROMPARTS DATETIMEOFFSETFROMPARTS

FORMAT STRING FUNCTION IN SQL SERVER 2012

FORMAT is one of the new built-in String Function introduced as a Part of Sql Server 2012. It returns the value formatted in the specified format using the optional culture parameter value. It is not an Sql Server native function instead it is .NET CLR dependent function.

SYNTAX: FORMAT ( value, format [, culture ] )

Parameter Description
value: Value to be formatted
format: This parameter specifies the format in which the vlaue will be formatted.
culture: This parameter is optional, it specifies the culture in which the value is formatted. If it is not specified then the language of the current session is used.

RETURNS: Return value type is nvarchar.

Example 1: FORMAT DATE with Culture

DECLARE @date DATETIME = GETDATE() 
SELECT @date AS 'GETDATE()',
       FORMAT( @date, 'd', 'en-US') AS 'DATE IN US Culture',
       FORMAT( @date, 'd', 'en-IN') AS 'DATE IN INDIAN Culture',
       FORMAT( @date, 'd', 'de-DE') AS 'DATE IN GERMAN Culture'

Result:

FORMAT_FUNCTION_IN_SQL_SERVER_2012_1

Example 2: FORMAT CURRENCY with Culture

DECLARE @Price INT = 40
SELECT FORMAT(@Price,'c','en-US') 
         AS 'CURRENCY IN US Culture',       
    FORMAT(@Price,'c','de-DE')
         AS 'CURRENCY IN GERMAN Culture'

Result:

FORMAT_FUNCTION_IN_SQL_SERVER_2012_2

Example 3: FORMAT CURRENCY

DECLARE @Price DECIMAL(5,3) = 40.356
SELECT FORMAT( @Price, 'C') AS 'Default',
      FORMAT( @Price, 'C0') AS 'With 0 Decimal',
       FORMAT( @Price, 'C1') AS 'With 1 Decimal',
       FORMAT( @Price, 'C2') AS 'With 2 Decimal',
       FORMAT( @Price, 'C3') AS 'With 3 Decimal'

Result:
FORMAT_FUNCTION_IN_SQL_SERVER_2012_3

Example 4: FORMAT PERCENTAGE

DECLARE @Percentage float = 0.35674
SELECT FORMAT( @Percentage, 'P') AS '% Default',
       FORMAT( @Percentage, 'P0') AS '% With 0 Decimal',
       FORMAT( @Percentage, 'P1') AS '% with 1 Decimal',
       FORMAT( @Percentage, 'P2') AS '% with 2 Decimal',
       FORMAT( @Percentage, 'P3') AS '% with 3 Decimal'

Result:
FORMAT_FUNCTION_IN_SQL_SERVER_2012_4

Example 5: FORMAT NUMBER

DECLARE @Number AS DECIMAL(10,2) = 454545.389
SELECT FORMAT( @Number, 'N','en-US') AS 'Number Format in US',
    FORMAT( @Number, 'N','en-IN')  AS 'Number Format in INDIA'

SELECT FORMAT( @Number, '#.0')     AS 'With 1 Decimal',
    FORMAT( @Number, '#.00')    AS 'With 2 Decimal',
    FORMAT( @Number, '#,##.00') AS 'With Comma and 2 Decimal',
    FORMAT( @Number, '##.00')   AS 'Without Comma and 2 Decimal'

Result:
FORMAT_FUNCTION_IN_SQL_SERVER_2012_5

Example 6: CUSTOM DATE FORMATS

DECLARE @date DATETIME = GETDATE() 
SELECT @date AS 'GETDATE()',
    FORMAT ( @date, 'dd/MM/yyyy') AS 'dd/MM/yyyy',
    FORMAT ( @date, 'MM/dd/yyyy') AS 'MM/dd/yyyy',
    FORMAT ( @date, 'yyyy/MM/dd') AS 'yyyy/MM/dd' 

SELECT 
 FORMAT( @date,'dddd, MMMM dd, yyyy hh:mm:ss tt','en-US')
   AS 'US',
 FORMAT( @date,'dddd, MMMM dd, yyyy hh:mm:ss tt','hi-IN')
   AS 'Hindi',
 FORMAT( @date,'dddd, MMMM dd, yyyy hh:mm:ss tt','kn-IN')
  AS 'Kannada'

Result:

FORMAT_FUNCTION_IN_SQL_SERVER_2012_6

DECLARE @date DATETIME = GETDATE()
SELECT FORMAT ( @date, 'dd', 'en-US' ) AS 'US',
	FORMAT ( @date, 'ddd', 'en-US' )   AS 'US',
	FORMAT ( @date, 'dddd', 'en-US' )  AS 'US',
	FORMAT ( @date, 'dddd', 'kn-IN' )  AS 'Kannada',
	FORMAT ( @date, 'dddd', 'hi-IN' )  AS 'Hindi'

SELECT FORMAT ( @date, 'M', 'en-US' )  AS 'US',
	FORMAT ( @date, 'MM', 'en-US' )    AS 'US',
	FORMAT ( @date, 'MMM', 'en-US' )   AS 'US',
	FORMAT ( @date, 'MMMM', 'en-US' )  AS 'US',
	FORMAT ( @date, 'MMMM', 'kn-IN' )  AS 'Kannada',
	FORMAT ( @date, 'MMMM', 'hi-IN' )  AS 'Hindi'

SELECT FORMAT ( @date, 'y', 'en-US' )   AS 'US',
       FORMAT ( @date, 'y', 'kn-IN' )   AS 'Kannada',
       FORMAT ( @date, 'y', 'hi-IN' )   AS 'Hindi',
       FORMAT ( @date, 'yy', 'en-US' )  AS 'US',
       FORMAT ( @date, 'yyy', 'en-US' ) AS 'US'

Result:

FORMAT_FUNCTION_IN_SQL_SERVER_2012_7

Example 7: Invalid Culture

DECLARE @date DATETIME = GETDATE()
SELECT FORMAT(@date,'d','Test') AS 'Invalid Culture'

Result:
Msg 9818, Level 16, State 1, Line 2
The culture parameter ‘Test’ provided in the function call is not supported.

You may like to read the below new built-in function’s introduced in Sql Server 2012:

New Built in Functions introduced in Sql Server
CONVERSION FUNCTIONS
PARSE TRY_PARSE
TRY_CONVERT
STRING FUNCTIONS
CONCAT FORMAT
LOGICAL FUNCTIONS
CHOOSE IIF
DATE AND TIME FUNCTIONS
EOMONTH
DATEFROMPARTS DATETIMEFROMPARTS
SMALLDATETIMEFROMPARTS DATETIME2FROMPARTS
TIMEFROMPARTS DATETIMEOFFSETFROMPARTS