Archive

Posts Tagged ‘time from parts’

SQL Server – Denali CTP3 – DATEFROMPARTS() and TIMEFROMPARTS()

18.07.2011 No comments

These are new functions in SQL Server "Denali". DATEFROMPARTS() returns a DATE value with specified day, month and year.

It takes three arguments:

DATEFROMPARTS (@Year, @Month, @Day)

If the arguments are invalid, it raises an error.

— © 2011 – Vishal (http://SqlAndMe.com)

 

SELECT DATEFROMPARTS (2011, 07, 18) AS 'Today'

Result Set:

Today

———-

2011-07-18

 

(1 row(s) affected)

 

Similarly, we can use DATETIMEFROMPARTS() to generate a DATETIME value.

It takes the form:

DATETIMEFROMPARTS (@Year, @Month, @Day, @Hour, @Minute, @Seconds, @Milliseconds)

If the arguments are invalid, an error is raised.

SELECT DATETIMEFROMPARTS (2011, 07, 18, 21, 01, 20, 700) AS 'Today'

Result Set:

Today

———————–

2011-07-18 21:01:20.700

 

(1 row(s) affected)

 

There is a function for TIME also, TIMEFROMPARTS(). It takes below arguments:

TIMEFROMPARTS (@Hour, @Minute, @Seconds, @Fractions, @Precision)

SELECT TIMEFROMPARTS ( 21, 01, 20, 070, 3) AS 'Now'

Result Set:

Now

—————-

21:01:20.070

 

(1 row(s) affected)

Also, there are similar functions for DATETIME2, DATETIMEOFFSET and SMALLDATETIME.

Hope This Helps!

Vishal