{"id":5421,"date":"2023-12-04T06:55:00","date_gmt":"2023-12-04T05:55:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=5421"},"modified":"2024-02-22T15:35:32","modified_gmt":"2024-02-22T14:35:32","slug":"how-to-calculate-difference-between-two-dates-in-t-sql","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/","title":{"rendered":"How to Calculate Difference Between Two Dates in T-SQL ?"},"content":{"rendered":"\n<h4 class=\"wp-block-heading has-text-align-left\"><em><span class=\"zip-ai-highlighted\">Learn 4 different methods to get the difference between two dates using SQL Server time functions.<\/span><\/em><\/h4>\n\n\n\n<p>With the SQL Server time functions, it is possible to calculate the difference between two dates in hours, minutes, and seconds. But this calculation is done only in one unit at a time. For example, only seconds, or only minutes or only hours. So its not very useful for one time calculations, because you need to combine them to get the full difference. This SQL Server scripts uses the DATEDIFF, CAST, Modulo or DATEADD functions and calculate difference in hours, minutes, and seconds in one or multiple queries.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#1-get-the-time-difference-between-two-timestamps-in-t-sql\" >1. Get the time difference between two timestamps in T-SQL<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#2-difference-between-two-dates-using-sql-server-cast-function\" >2. Difference between two dates using SQL Server CAST function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#3-calculate-the-time-difference-using-dateadd-function\" >3. Calculate the time difference using DATEADD function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#4-get-difference-between-two-dates-using-t-sql-and-datepart\" >4. Get difference between two dates using T-SQL and DATEPART<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1-get-the-time-difference-between-two-timestamps-in-t-sql\"><\/span>1. Get the time difference between two timestamps in T-SQL<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>First, as an example, these 3 simple SQL Server queries uses the DATEDIFF functions. And the two timestamps stored in variables have exactly one hour of difference. However, each query only returns one unit, which is not enough for our purpose. To get the complete result in hours, minutes, and seconds, we need to concatenate multiple queries together. Indeed, the queries below show 3 different specific time unit as results.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>First, calculate the difference in <strong><em>seconds<\/em><\/strong><\/li>\n\n\n\n<li>Then in <strong><em>minutes<\/em><\/strong><\/li>\n\n\n\n<li>The last one in <strong><em>hours<\/em><\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">-- Declare and initialize two variables with exactly one hour of difference: 9 - 8 = 1h \nDECLARE @DateStart \tDATETIME\nDECLARE @DateEnd \tDATETIME\nSET @DateStart \t= '2022-03-10 08:00:00'\nSET @DateEnd \t= '2022-03-10 09:00:00'\n\n-- SQL Query returns difference in seconds : 3600 seconds\nSELECT DATEDIFF(SECOND, @DateStart, @DateEnd) AS [Difference in Seconds]\n\n-- SQL Query returns difference in minutes : 60 minutes\nSELECT DATEDIFF(MINUTE, @DateStart, @DateEnd) AS [Difference in Minutes]\n\n-- SQL Query returns difference in hours : 1 hour\nSELECT DATEDIFF(HOUR, @DateStart, @DateEnd) AS [Difference in Hours]\n<\/pre>\n\n\n\n<p>Or in a more concise format, using only 3 lines of code, but calculating only the difference in hours:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">-- Declare and initialize two variables with exactly one hour of difference: 9 - 8 = 1h \nDECLARE @DateStart DATETIME = '2022-03-10 08:00:00',\n        @DateEnd   DATETIME = '2022-03-10 09:00:00';\n\n-- SQL Query returns difference in hours : 1 hour\nSELECT DATEDIFF(HOUR, @DateStart, @DateEnd) AS [Difference in Hours];\n<\/pre>\n\n\n\n<p>Find below a screenshot after the execution of the query using <strong><a href=\"https:\/\/expert-only.com\/en\/dba\/create-sql-server-database-with-ssms\/\">SSMS to query our database<\/a><\/strong>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"720\" height=\"360\" src=\"https:\/\/expert-only.com\/wp-content\/uploads\/2023\/01\/t-sql_difference-between-two-dates-in-hours.jpg\" alt=\"T-SQL query to Calculate the difference between two dates\" class=\"wp-image-22213\" srcset=\"https:\/\/expert-only.com\/wp-content\/uploads\/2023\/01\/t-sql_difference-between-two-dates-in-hours.jpg 720w, https:\/\/expert-only.com\/wp-content\/uploads\/2023\/01\/t-sql_difference-between-two-dates-in-hours-300x150.jpg 300w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><figcaption class=\"wp-element-caption\"><em>How to Calculate Difference Between Two Dates in T-SQL ?<\/em><\/figcaption><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-2-difference-between-two-dates-using-sql-server-cast-function\"><span class=\"ez-toc-section\" id=\"2-difference-between-two-dates-using-sql-server-cast-function\"><\/span>2. Difference between two dates using SQL Server CAST function<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this second method, to get the time difference in hours, minutes, and seconds, use this query with CAST and the <a href=\"https:\/\/realpython.com\/python-modulo-operator\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Modulo mathematic operator<\/a>. However, it works only if the number of hours are smaller than 99. Moreover, the query changes the total in seconds to Hours, Minutes and Seconds, for example with 3 hours = 3600 * 3 = 14400 seconds.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">DECLARE @TimeInSeconds INT\nSET \t@TimeInSeconds = 14400\n\nSELECT\nRIGHT('0' + CAST(@TimeInSeconds \/ 3600 AS VARCHAR),2) + ':' +\nRIGHT('0' + CAST((@TimeInSeconds \/ 60) % 60 AS VARCHAR),2) + ':' +\nRIGHT('0' + CAST(@TimeInSeconds % 60 AS VARCHAR),2)\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-3-calculate-the-time-difference-using-dateadd-function\"><span class=\"ez-toc-section\" id=\"3-calculate-the-time-difference-using-dateadd-function\"><\/span>3. Calculate the time difference using DATEADD function<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this third approach, these queries computes the <a href=\"https:\/\/help.gnome.org\/users\/gthumb\/stable\/gthumb-date-formats.html.en\" target=\"_blank\" rel=\"noreferrer noopener\">time<\/a> difference using first the seconds (multiplied by one hour in seconds is 60 * 60 = 3600. Then using the milliseconds (multiplied by one hour in milliseconds as 60 * 60 * 1000 = 3600 * 1000). And it finally displays the result without the milliseconds using the <a href=\"https:\/\/www.w3schools.com\/sql\/func_sqlserver_convert.asp\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">CONVERT<\/a> function and the data date format 108 which is as the following: <strong>hh:mi:ss<\/strong>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">-- Using the seconds as a base for conversion\nselect CONVERT(varchar, dateadd(s, 3 * 3600 , getdate()), 108) AS [Hours Minutes Seconds]\n\n-- Using the milliseconds as a base for conversion\nselect CONVERT(varchar, dateadd(ms, 3 * 3600 * 1000, getdate()), 108) AS [Hours Minutes Seconds]\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4-get-difference-between-two-dates-using-t-sql-and-datepart\"><\/span>4. Get difference between two dates using T-SQL and DATEPART<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Another option to calculate the difference in minutes and seconds is to use the T-SQL DATEPART function. This built-in MS SQL function is made on purpose to extract a specific part of the date format.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">DECLARE @DateStart DATETIME = '2022-03-10 08:00:00',\n        @DateEnd   DATETIME = '2022-03-10 09:00:00';\n\nSELECT \n    DATEPART(HOUR, @DateEnd)   - DATEPART(HOUR, @DateStart)   as [Difference in Hours],\n    DATEPART(MINUTE, @DateEnd) - DATEPART(MINUTE, @DateStart) as [Difference in Minutes],\n    DATEPART(SECOND, @DateEnd) - DATEPART(SECOND, @DateStart) as [Difference in Seconds];<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Four different ways to calculate time differences using SQL Server<\/h3>\n\n\n\n<p>In this tutorial, we discussed 4 different ways how to calculate the difference between two SQL Server dates using T-SQL code. By using built-in time functions, you can easily determine the difference in hours, minutes, and seconds. To optimize the solution you have chosen, we suggest combining multiple functions. For more information on the SQL Server DATEADD() time function, you can refer to the <a href=\"https:\/\/learn.microsoft.com\/en-us\/sql\/t-sql\/functions\/dateadd-transact-sql?view=sql-server-ver15\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">official<\/a> Microsoft documentation.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">FAQ about SQL Server time functions<\/h4>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1649323152606\"><strong class=\"schema-faq-question\">How to calculate a time difference in hours with a SQL Server query?<\/strong> <p class=\"schema-faq-answer\">To calculate a time difference between datetime in hours, take the two date and times and do the difference by using the HOUR option combined with the DATEDIFF time function in SQL Server : <br\/>&#8220;SELECT DATEDIFF(HOUR, @DateStart, @DateEnd) AS [Difference in Hours]&#8221;<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1649323313066\"><strong class=\"schema-faq-question\">How to calculate a SQL time difference in minutes?<\/strong> <p class=\"schema-faq-answer\">To calculate a time difference between two dates in minutes, use the two date and times and calculate the difference with the MINUTE option of the T-SQL DATEDIFF function :<br\/>&#8220;SELECT DATEDIFF(MINUTE, @DateStart, @DateEnd) AS [Difference in Minutes]&#8221;<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1649323417056\"><strong class=\"schema-faq-question\">How to display a time difference in seconds with SQL Server?<\/strong> <p class=\"schema-faq-answer\">In order to calculate the exact time difference between two timestamps to the seconds, do the difference using the SECOND option provided by the SQL Server database :<br\/>&#8220;SELECT DATEDIFF(SECOND, @DateStart, @DateEnd) AS [Difference in Seconds]&#8221;<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1649323575817\"><strong class=\"schema-faq-question\">How to calculate a time difference in milliseconds with MS SQL?<\/strong> <p class=\"schema-faq-answer\">To calculate precisely the time difference in milliseconds, use the T-SQL built-in time function and the MILLISECOND keyword : <br\/>&#8220;SELECT DATEDIFF(MILLISECOND, @DateStart, @DateEnd) AS [Difference in Milliseconds]&#8221;<br\/><\/p> <\/div> <\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><em> Useful T-SQL queries and tips that might interest you<\/em><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/expert-only.com\/en\/t-sql\/display-month-name-sql-server\/\">Query to display the month name in SQL Server<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/expert-only.com\/en\/t-sql\/last-time-table-was-updated-sql-server\/\">Last time table was updated in SQL Server<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/expert-only.com\/en\/sql-server-error\/arithmetic-overflow-error-converting-expression-to-data-type-int\/\">Avoid the SQL Server Arithmetic overflow error converting expression<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/expert-only.com\/en\/t-sql\/check-if-table-exists-in-sql-server\/\">Check if table exists in SQL Server<\/a><\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-embed aligncenter is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"TAlX0L6VuG\"><a href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-string-longer-than-8000-chars\/\">SQL Server string longer than 8000 characters<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;SQL Server string longer than 8000 characters&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-string-longer-than-8000-chars\/embed\/#?secret=lavhkhaQDH#?secret=TAlX0L6VuG\" data-secret=\"TAlX0L6VuG\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Learn 4 different methods to get the difference between two dates using SQL Server time functions. With the SQL Server time functions, it is possible to calculate the difference between two dates in hours, minutes, and seconds. But this <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/\" title=\"How to Calculate Difference Between Two Dates in T-SQL ?\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":5525,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[454],"tags":[],"class_list":{"0":"post-5421","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-t-sql"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.7 (Yoast SEO v26.2) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Calculate Difference Between Two Dates in T-SQL ? SQL<\/title>\n<meta name=\"description\" content=\"Calculate the difference between two dates in T-SQL with hours, minutes, seconds and milliseconds using different SQL Server time functions.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Calculate Difference Between Two Dates in T-SQL ?\" \/>\n<meta property=\"og:description\" content=\"Calculate the difference between two dates in T-SQL with hours, minutes, seconds and milliseconds using different SQL Server time functions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL and IT Tutorials\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-12-04T05:55:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-22T14:35:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/02\/coffee-631767_1920.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Expert-Only\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@expert_only\" \/>\n<meta name=\"twitter:site\" content=\"@expert_only\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Expert-Only\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/\"},\"author\":{\"name\":\"Expert-Only\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\"},\"headline\":\"How to Calculate Difference Between Two Dates in T-SQL ?\",\"datePublished\":\"2023-12-04T05:55:00+00:00\",\"dateModified\":\"2024-02-22T14:35:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/\"},\"wordCount\":736,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/02\/coffee-631767_1920.jpg\",\"articleSection\":[\"T-SQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#respond\"]}]},{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/\",\"url\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/\",\"name\":\"How to Calculate Difference Between Two Dates in T-SQL ? SQL\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/02\/coffee-631767_1920.jpg\",\"datePublished\":\"2023-12-04T05:55:00+00:00\",\"dateModified\":\"2024-02-22T14:35:32+00:00\",\"description\":\"Calculate the difference between two dates in T-SQL with hours, minutes, seconds and milliseconds using different SQL Server time functions.\",\"breadcrumb\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#breadcrumb\"},\"mainEntity\":[{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#faq-question-1649323152606\"},{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#faq-question-1649323313066\"},{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#faq-question-1649323417056\"},{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#faq-question-1649323575817\"}],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#primaryimage\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/02\/coffee-631767_1920.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/02\/coffee-631767_1920.jpg\",\"width\":1920,\"height\":1080},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"en\",\"item\":\"https:\/\/expert-only.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Calculate Difference Between Two Dates in T-SQL ?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/expert-only.com\/en\/#website\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"name\":\"SQL and IT Tutorials\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/expert-only.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/expert-only.com\/en\/#organization\",\"name\":\"Expert-Only\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"width\":381,\"height\":174,\"caption\":\"Expert-Only\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\",\"https:\/\/x.com\/expert_only\",\"https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\",\"name\":\"Expert-Only\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"caption\":\"Expert-Only\"}},{\"@type\":\"Question\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#faq-question-1649323152606\",\"position\":1,\"url\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#faq-question-1649323152606\",\"name\":\"How to calculate a time difference in hours with a SQL Server query?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"To calculate a time difference between datetime in hours, take the two date and times and do the difference by using the HOUR option combined with the DATEDIFF time function in SQL Server : <br\/>\\\"SELECT DATEDIFF(HOUR, @DateStart, @DateEnd) AS [Difference in Hours]\\\"\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#faq-question-1649323313066\",\"position\":2,\"url\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#faq-question-1649323313066\",\"name\":\"How to calculate a SQL time difference in minutes?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"To calculate a time difference between two dates in minutes, use the two date and times and calculate the difference with the MINUTE option of the T-SQL DATEDIFF function :<br\/>\\\"SELECT DATEDIFF(MINUTE, @DateStart, @DateEnd) AS [Difference in Minutes]\\\"\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#faq-question-1649323417056\",\"position\":3,\"url\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#faq-question-1649323417056\",\"name\":\"How to display a time difference in seconds with SQL Server?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"In order to calculate the exact time difference between two timestamps to the seconds, do the difference using the SECOND option provided by the SQL Server database :<br\/>\\\"SELECT DATEDIFF(SECOND, @DateStart, @DateEnd) AS [Difference in Seconds]\\\"\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#faq-question-1649323575817\",\"position\":4,\"url\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#faq-question-1649323575817\",\"name\":\"How to calculate a time difference in milliseconds with MS SQL?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"To calculate precisely the time difference in milliseconds, use the T-SQL built-in time function and the MILLISECOND keyword : <br\/>\\\"SELECT DATEDIFF(MILLISECOND, @DateStart, @DateEnd) AS [Difference in Milliseconds]\\\"<br\/>\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Calculate Difference Between Two Dates in T-SQL ? SQL","description":"Calculate the difference between two dates in T-SQL with hours, minutes, seconds and milliseconds using different SQL Server time functions.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/","og_locale":"en_US","og_type":"article","og_title":"How to Calculate Difference Between Two Dates in T-SQL ?","og_description":"Calculate the difference between two dates in T-SQL with hours, minutes, seconds and milliseconds using different SQL Server time functions.","og_url":"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/","og_site_name":"SQL and IT Tutorials","article_publisher":"https:\/\/www.facebook.com\/ExpertOnlyCom\/","article_published_time":"2023-12-04T05:55:00+00:00","article_modified_time":"2024-02-22T14:35:32+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/02\/coffee-631767_1920.jpg","type":"image\/jpeg"}],"author":"Expert-Only","twitter_card":"summary_large_image","twitter_creator":"@expert_only","twitter_site":"@expert_only","twitter_misc":{"Written by":"Expert-Only","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#article","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/"},"author":{"name":"Expert-Only","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef"},"headline":"How to Calculate Difference Between Two Dates in T-SQL ?","datePublished":"2023-12-04T05:55:00+00:00","dateModified":"2024-02-22T14:35:32+00:00","mainEntityOfPage":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/"},"wordCount":736,"commentCount":0,"publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"image":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/02\/coffee-631767_1920.jpg","articleSection":["T-SQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#respond"]}]},{"@type":["WebPage","FAQPage"],"@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/","url":"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/","name":"How to Calculate Difference Between Two Dates in T-SQL ? SQL","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#primaryimage"},"image":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/02\/coffee-631767_1920.jpg","datePublished":"2023-12-04T05:55:00+00:00","dateModified":"2024-02-22T14:35:32+00:00","description":"Calculate the difference between two dates in T-SQL with hours, minutes, seconds and milliseconds using different SQL Server time functions.","breadcrumb":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#breadcrumb"},"mainEntity":[{"@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#faq-question-1649323152606"},{"@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#faq-question-1649323313066"},{"@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#faq-question-1649323417056"},{"@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#faq-question-1649323575817"}],"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#primaryimage","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/02\/coffee-631767_1920.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/02\/coffee-631767_1920.jpg","width":1920,"height":1080},{"@type":"BreadcrumbList","@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"en","item":"https:\/\/expert-only.com\/en\/"},{"@type":"ListItem","position":2,"name":"How to Calculate Difference Between Two Dates in T-SQL ?"}]},{"@type":"WebSite","@id":"https:\/\/expert-only.com\/en\/#website","url":"https:\/\/expert-only.com\/en\/","name":"SQL and IT Tutorials","description":"","publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/expert-only.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/expert-only.com\/en\/#organization","name":"Expert-Only","url":"https:\/\/expert-only.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","width":381,"height":174,"caption":"Expert-Only"},"image":{"@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ExpertOnlyCom\/","https:\/\/x.com\/expert_only","https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA"]},{"@type":"Person","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef","name":"Expert-Only","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","caption":"Expert-Only"}},{"@type":"Question","@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#faq-question-1649323152606","position":1,"url":"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#faq-question-1649323152606","name":"How to calculate a time difference in hours with a SQL Server query?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"To calculate a time difference between datetime in hours, take the two date and times and do the difference by using the HOUR option combined with the DATEDIFF time function in SQL Server : <br\/>\"SELECT DATEDIFF(HOUR, @DateStart, @DateEnd) AS [Difference in Hours]\"","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#faq-question-1649323313066","position":2,"url":"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#faq-question-1649323313066","name":"How to calculate a SQL time difference in minutes?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"To calculate a time difference between two dates in minutes, use the two date and times and calculate the difference with the MINUTE option of the T-SQL DATEDIFF function :<br\/>\"SELECT DATEDIFF(MINUTE, @DateStart, @DateEnd) AS [Difference in Minutes]\"","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#faq-question-1649323417056","position":3,"url":"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#faq-question-1649323417056","name":"How to display a time difference in seconds with SQL Server?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"In order to calculate the exact time difference between two timestamps to the seconds, do the difference using the SECOND option provided by the SQL Server database :<br\/>\"SELECT DATEDIFF(SECOND, @DateStart, @DateEnd) AS [Difference in Seconds]\"","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#faq-question-1649323575817","position":4,"url":"https:\/\/expert-only.com\/en\/t-sql\/how-to-calculate-difference-between-two-dates-in-t-sql\/#faq-question-1649323575817","name":"How to calculate a time difference in milliseconds with MS SQL?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"To calculate precisely the time difference in milliseconds, use the T-SQL built-in time function and the MILLISECOND keyword : <br\/>\"SELECT DATEDIFF(MILLISECOND, @DateStart, @DateEnd) AS [Difference in Milliseconds]\"<br\/>","inLanguage":"en-US"},"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/5421","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/comments?post=5421"}],"version-history":[{"count":27,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/5421\/revisions"}],"predecessor-version":[{"id":29952,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/5421\/revisions\/29952"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/5525"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=5421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=5421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=5421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}