# DATEDIFF() – Time Related Functions

I am constant in my creation of 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. Many times you will want to calculate the difference between two given dates using a particular unit of measure (date part) like hours, minutes, or seconds.

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

To summarize, the function takes a date part, a start date/time variable and a end date/time variable as input returns the calculated difference in the correct unit of measure.

I will be exploring the three time part parameters that determine the units of measure

 DATE PART PARAMETER TO USE HOUR HH MINUTE MI or N SECOND SS or S

The examples below are sample calls to the function. The inputs puts are valid date part parameters and date time variables.

The output from the test is listed below.

Like any good unit test, one should try both positive and negative test cases. I am not going to explore how an EMPTY string or NULL value effect the results provided by the function.

However, the following rules apply. Any invalid date parameter results in a raised error. Any NULL values passed as parameters result in a NULL result. Any EMPTY strings result in a default date.

To make a long story short, pass one of the three date part parameters (time) with two date/time variables to calculated the difference. Next time, I will be talking about how to use this function with Sub-Second related Parts.
{"ft_lang_label":"__label__en","ft_lang_prob":0.64915496,"math_prob":0.9490893,"size":2468,"snap":"2020-34-2020-40","text_gpt3_token_len":721,"char_repetition_ratio":0.12012987,"word_repetition_ratio":0.11325301,"special_character_ratio":0.345624,"punctuation_ratio":0.12922466,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9538839,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-09-27T15:33:32Z\",\"WARC-Record-ID\":\"<urn:uuid:b0c1b4db-3305-41a1-9e6d-6d1e58f90805>\",\"Content-Length\":\"59855\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ddab8e40-fcdd-46a9-b1ac-967e5df5671d>\",\"WARC-Concurrent-To\":\"<urn:uuid:0594cd89-6494-4c7e-8a45-26ca58be2734>\",\"WARC-IP-Address\":\"74.208.236.254\",\"WARC-Target-URI\":\"http://craftydba.com/?p=6732\",\"WARC-Payload-Digest\":\"sha1:6DQMPOLNDOKFRMGMESL5TBTVZM6KUCTD\",\"WARC-Block-Digest\":\"sha1:OVGSUS46XPN7UOAITVWD2WQORBS6NVFJ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-40/CC-MAIN-2020-40_segments_1600400283990.75_warc_CC-MAIN-20200927152349-20200927182349-00393.warc.gz\"}"}