How can I easily convert two columns in a table to a single datetime column:
the columns look like this:
Date_In
19990220
20000114
19980524
Time_In
1304
0237
1503
I should also add that these columns are of varchar data type since the
source data is a text file.
Thanks!ALTER TABLE tablename ADD Date_Time_In SMALLDATETIME
GO
UPDATE tablename SET Date_Time_In = Date_In + ' ' +
LEFT(Time_In,2)+':'+RIGHT(Time_In,2)
WHERE IsDate(Date_In + ' ' + LEFT(Time_In,2)+':'+RIGHT(Time_In,2)) = 1
-- identify those with bad data:
SELECT Date_In,Time_In FROM tablename WHERE Date_Time_In IS NULL
"Patrice" <Patrice@.discussions.microsoft.com> wrote in message
news:49B879F1-42FA-41B0-8AA8-E22DFC4E2748@.microsoft.com...
> How can I easily convert two columns in a table to a single datetime
> column:
> the columns look like this:
> Date_In
> 19990220
> 20000114
> 19980524
>
> Time_In
> 1304
> 0237
> 1503
> I should also add that these columns are of varchar data type since the
> source data is a text file.
> Thanks!|||Hi
SELECT cast(convert(char(8),'19990220',112)+' '+ convert(char(5),
stuff('1304',3,2, ':'+right('1304',2)),108)as datetime)
"Patrice" <Patrice@.discussions.microsoft.com> wrote in message
news:49B879F1-42FA-41B0-8AA8-E22DFC4E2748@.microsoft.com...
> How can I easily convert two columns in a table to a single datetime
> column:
> the columns look like this:
> Date_In
> 19990220
> 20000114
> 19980524
>
> Time_In
> 1304
> 0237
> 1503
> I should also add that these columns are of varchar data type since the
> source data is a text file.
> Thanks!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment