How to Combine Time and Date in SQL SERVER? – Interview Question of the Week #285

Question: How to Combine Time and Date in SQL SERVER?

Answer: A very simple and popular question. Let us see how we can combine and date and time in SQL Server.

How to Combine Time and Date in SQL SERVER? - Interview Question of the Week #285 CombineTimeDate-800x201

Method 1: Using + Combine Time Date

Let us run the following command:

DECLARE @dt DATE = '2020-07-12'
DECLARE @tm TIME = '07:01:01.000'
SELECT CAST(@dt AS DATETIME) + CAST(@tm AS DATETIME) AS [CombineDateTime]

Method 2: Using DATEADD

Let us run the following command:

DECLARE @dt DATE = '2020-07-12'
DECLARE @tm TIME = '07:01:01.000'
SELECT DATEADD(DAY,DATEDIFF(DAY, 0, @dt),CAST(@tm AS DATETIME)) AS [CombineDateTime]

There is also one more method I often see people using in the real world is the function CONCAT. I would love to see your answer here in the comment section. Please leave a comment with your answer – How we can use the CONCAT functions to combine time and date in SQL Server?

Here are the blog posts in the series which you can read in order to learn more about this topic:

If you are CTO, CIO, or CXO who is responsible to move your organizations to the cloud you should read the blog posts listed above. If your organizations are not on the cloud, you should consider introspecting in your technology stack and applications as I am very confident that there will be set of application which will be more suitable for cloud and there will be set of tools which are best kept at on-premises.

Here is another interview like a puzzle which may interest you: SQL SERVER – Computed Column and Compute Scalar Operators. My client asked me a very interesting question about the computed column. The question is as follows: When any computed column is retrieved from a table, why are there two scalar operations in the execution plan?

Reference: Pinal Dave (https://blog.sqlauthority.com)

, , ,
Previous Post
What are Different Layers of Open Source Cloud? – Interview Question of the Week #284
Next Post
How to Generate Script for SQL Server 2005 from SQL Server 2019? – Interview Question of the Week #285

Related Posts

1 Comment. Leave new

  • DECLARE @dt DATE = ‘2020-07-12′
    DECLARE @tm TIME = ’07:01:01.000′
    SELECT concat(@dt,’ ‘, @tm) AS [CombineDateTime]

    Reply

Leave a Reply

Menu