question

genuiForex avatar image
genuiForex asked

Converting separated column Date & time into datetime format

Can someone can help me on this, i have two columns 1 date and col 2 is time can i join these two fields to be a datetime field. I have attached sample db data for your reference. Thank you in advance.![alt text][1] [1]: /storage/temp/3655-img.jpg
datedatesdate-formatdate-and-time-functionsdatetime-formats
img.jpg (20.9 KiB)
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

GPO avatar image
GPO answered
Assuming the ph_time column is actually the number of minutes after midnight, you might want to do something like this: --======= ================================================================================================= --======= Create 100000 rows of random dates from the next 12 months. DECLARE @mln as datetime = dateadd(dd,datediff(dd,0,getdate()),0); --midnight last night IF OBJECT_ID('tempdb..#dates_and_minutes') IS NOT NULL DROP TABLE #dates_and_minutes; SELECT TOP 100000 dateadd(dd,abs(checksum(newid())) % 365,@mln) as ph_date --create some random dates ,abs(checksum(newid())) % 1440 as ph_time --This might be a misnomer I think. Should it say minutes from midnight? INTO #dates_and_minutes FROM sys.columns t1 CROSS JOIN sys.columns t2 ; --------- I think this next line is what you're really after. SELECT dateadd(mi,dm.ph_time,dm.ph_date) as ph_date_time ,dm.ph_date ,dm.ph_time FROM #dates_and_minutes dm ;
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

genuiForex avatar image
genuiForex answered
Thank you for your response.
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.