DATEPART() Function – Subseconds

I am firm in my decision to write another short articles or tidbits on Transaction SQL date/time functions.

Most, if not all, modern day computer systems collect data which contains dates and times.

Not only is it important to know when a sales order came in, but we should fulfill the request using some type of queue (LIFO). If if was a stack (FIFO) implementation, the first person may never get his order!

Today, I will be exploring the DATEPART() function. This function has 15 different input parameters that can be passed to change the resulting output.

In short, the function takes a date part and date time variables as input returns the part that you want as an integer. I will be concentrating on how to extract time as sub-seconds in this article.

We can extract milliseconds (MS), microseconds (MCS) and nanoseconds (NS) from the date time variable given the correct date part parameter.

The examples below are sample calls to the function. The inputs puts are all valid dates.

You can validate these results on your own. A null value as input will return a null value as output. A empty string as an input will return a 0 as an output.

The output from the above test is listed below.

In as nutshell, pass the correct date part parameters to extract the chosen sub-second, as an integer, from the date time variable.

Related posts

Leave a Comment