T-SQL 101: #62 Calculating date values from day month and year in SQL Server T-SQL using DATEFROMPARTS

I mentioned in earlier posts that there's no standard way to write dates, so we end up having to write them as strings. Now that was a real problem in earlier versions where people would get that wrong.

SQL Server 2012 added an option to make that easier. DATEFROMPARTS allows you to specify a year, month, and a day to create a date, and always in that order.

Look at the following query:

You can see the result returned here:

Now apart from dates, of the date related data types have options for using FROMPARTS (e.g. DATETIMEFROMPARTS, DATETIMEOFFSETFROMPARTS, etc.)

One poor aspect of these in SQL Server Management Studio though, is that the Intellisense is currently lousy:

Surely that isn't necessary, and correctly named parameters could be provided.

Learning T-SQL

It's worth your while becoming proficient in SQL. If you'd like to learn a lot about T-SQL in a hurry, our Writing T-SQL Queries for SQL Server course is online, on-demand, and low cost.

Leave a Reply

Your email address will not be published.