Saturday , April 27 2024

DATEFROMPARTS, DATETIMEFROMPARTS, DATETIME2FROMPARTS, SMALLDATETIMEFROMPARTS Function in SQL Server(TSQL)

 

We can get a date by combining the values such as year, month and day with the help of DATEFROMPARTS, DATETIMEFROMPARTS, DATETIME2FROMPARTS, SMALLDATETIMEFROMPARTS functions.

DATEFROMPARTS:

It takes year, month and day values as parameters and returns a value in the date data type.

Sample Usage:

DATETIMEFROMPARTS:

It takes year, month, day, hour, minute, seconds, and milliseconds values as parameters and returns a value in the datetime data type.

Sample Usage:

DATETIME2FROMPARTS:

It takes year, month, day, hour, minute, seconds, fractions and precision values as parameters and returns a value in the datetime2 data type.

Sample Usage:

If you try to use it as follows, you will receive the below error:

Msg 289, Level 16, State 5, Line 1

Cannot construct data type datetime2, some of the arguments have values which are not valid.

SMALLDATETIMEFROMPARTS:

It takes year, month, day, hour and minute values as parameters and returns a value in the smalldatetime data type.

Sample Usage:

Loading

About dbtut

We are a team with over 10 years of database management and BI experience. Our Expertises: Oracle, SQL Server, PostgreSQL, MySQL, MongoDB, Elasticsearch, Kibana, Grafana.

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories