Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

how to combine and convert two integer columns to datetime Expand / Collapse
Author
Message
Posted Friday, March 15, 2013 2:13 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, July 18, 2014 1:00 PM
Points: 178, Visits: 215
I have date stored in a column as integer type and time stored in a column as integer type. How do I combine and convert both columns into datetime type?
For Example: Col A has 20130314 (yyyymmdd) and Col B has 123000 (hhmmss). My Output should be 2013-03-14 12:30:00
Post #1431741
Posted Friday, March 15, 2013 2:20 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 12:25 PM
Points: 23,215, Visits: 31,896
naveen.pasupuleti (3/15/2013)
I have date stored in a column as integer type and time stored in a column as integer type. How do I combine and convert both columns into datetime type?
For Example: Col A has 20130314 (yyyymmdd) and Col B has 123000 (hhmmss). My Output should be 2013-03-14 12:30:00


Something like this:


declare @TestDate int = 20130314,
@TestTIme int = 123000;

select cast(cast(@TestDate as varchar(8)) as datetime),
cast(stuff(stuff(cast(@TestTime as varchar(6)),5,0,':'),3,0,':') as time(0)),
cast(cast(@TestDate as varchar(8)) as datetime) + cast(stuff(stuff(cast(@TestTime as varchar(6)),5,0,':'),3,0,':') as time(0));





Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1431746
Posted Friday, March 15, 2013 3:00 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, July 18, 2014 1:00 PM
Points: 178, Visits: 215
Thanks Lynn for your response.
The code you shared works for this example but sometimes integer time column or col B can have values like 93000 (9.30am) or 0 (12 am). The number of digits vary in the column.

SELECT last_run_date,last_run_time from msdb.dbo.sysjobservers
One closest example I can think of is from msdb database. Sysjobservers table has two columns that match exactly my requirement. Last_run_date column is a integer type having date number and Last_run_time is a integer type having time number. How to get datetime out of these two columns.
Post #1431771
Posted Friday, March 15, 2013 3:06 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 12:25 PM
Points: 23,215, Visits: 31,896
naveen.pasupuleti (3/15/2013)
Thanks Lynn for your response.
The code you shared works for this example but sometimes integer time column or col B can have values like 93000 (9.30am) or 0 (12 am). The number of digits vary in the column.

SELECT last_run_date,last_run_time from msdb.dbo.sysjobservers
One closest example I can think of is from msdb database. Sysjobservers table has two columns that match exactly my requirement. Last_run_date column is a integer type having date number and Last_run_time is a integer type having time number. How to get datetime out of these two columns.


Try this:


declare @TestDate int = 20130314,
@TestTIme int = 123000;

select cast(cast(@TestDate as varchar(8)) as datetime),
cast(stuff(stuff(cast(@TestTime as varchar(6)),5,0,':'),3,0,':') as time(0)),
cast(cast(@TestDate as varchar(8)) as datetime) + cast(stuff(stuff(right('000000' + cast(@TestTime as varchar(6)),6),5,0,':'),3,0,':') as time(0));





Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1431778
Posted Friday, March 15, 2013 3:22 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 4:28 PM
Points: 2,027, Visits: 3,023
Or, another possibility (with no character conversions/manipulation):

SELECT 
ColA, ColB,
DATEADD(SECOND, ColB % 100, DATEADD(MINUTE, ColB % 10000 / 100, DATEADD(HOUR, ColB / 10000,
DATEADD(DAY, ColA % 100 - 1, DATEADD(MONTH, ColA % 10000 / 100 - 1, DATEADD(YEAR, ColA / 10000 - 1900, 0)))))) AS Col_New
FROM (
SELECT 20130314 AS ColA, 123011 AS ColB UNION ALL
SELECT 20130314, 093015
) AS test_data



SQL DBA,SQL Server MVP('07, '08, '09)
"In America, every man is innocent until proven broke!" Brant Parker
Post #1431794
Posted Friday, March 15, 2013 3:32 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Tuesday, August 12, 2014 4:51 PM
Points: 7,097, Visits: 12,597
Are these SQL Agent date and time values? I used to have my own homegrown functions but I just picked up knowledge of this lttle gem on these forums the other day:

SELECT  msdb.dbo.agent_datetime(20130314, 123000);



__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1431802
Posted Friday, March 15, 2013 3:48 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Tuesday, August 12, 2014 4:51 PM
Points: 7,097, Visits: 12,597
PS Forgot to mention the function is undocumented so take that into consideration.

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1431814
Posted Monday, March 18, 2013 9:29 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, July 18, 2014 1:00 PM
Points: 178, Visits: 215
Thank you very much OPC.Three.
This builtin function msdb.dbo.agent_datetime did the job for me.
Post #1432231
Posted Monday, March 18, 2013 9:37 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Tuesday, August 12, 2014 4:51 PM
Points: 7,097, Visits: 12,597
You're welcome. Thanks for the feedback.

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1432234
Posted Monday, March 18, 2013 10:05 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:42 AM
Points: 7,175, Visits: 13,621
opc.three (3/15/2013)
PS Forgot to mention the function is undocumented so take that into consideration.

It's a cough scalar function too.
Combine the best bits of Lynn's and Scott's solutions and roll them into an inline TVF:

CREATE FUNCTION [dbo].[IF_Agent_Datetime] 
(
@Date INT,
@Time INT
)
RETURNS TABLE WITH SCHEMABINDING AS
RETURN
(

SELECT DatetimeValue =
DATEADD(SECOND,@Time % 100,
DATEADD(MINUTE,@Time % 10000 / 100,
DATEADD(HOUR,@Time/10000,
CAST(CAST(@Date AS CHAR(8)) AS DATETIME))))

)
GO
SELECT *
FROM dbo.IF_Agent_Datetime (20130314, 123011)

2013-03-14 12:30:11.000



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1432246
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse