Author |
Topic |
Dasman
Yak Posting Veteran
79 Posts |
Posted - 2011-06-08 : 13:22:46
|
Dear All,I have the following data in ONE COLUMN:9/14/2005 1:32:52 AM9/14/2005 1:33:19 AM9/14/2005 1:33:56 AM9/14/2005 1:34:37 AM9/14/2005 1:35:16 AM9/14/2005 2:38:23 AMI would like to split this into a DATE and a TIME column.I believe I should use an Update command and it has to do with varchar.I would appreciate help on this.Thanks in Advance,DasmanSQL Should be Fun! |
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2011-06-08 : 13:30:14
|
If it's a VARCHAR data type:SELECT LTRIM(RTRIM(LEFT(dateColumn, charindex(' ' , dateColumn)))) as dt, LTRIM(RTRIM(RIGHT(dateColumn, len(dateColumn) - charindex(' ' , dateColumn)))) as tmFROM YourTable |
|
|
Dasman
Yak Posting Veteran
79 Posts |
Posted - 2011-06-08 : 13:56:36
|
Hello RussellBy dateColumn I assume u mean the column which has the date and time. My columnname for the data posted earlier is StartTimeSo I ran this:SELECT LTRIM(RTRIM(LEFT(StartTime, charindex(' ' , StartTime)))) as dt, LTRIM(RTRIM(RIGHT(StartTime, len(StartTime) - charindex(' ' , StartTime)))) as tmFROM TableNameand got this in TWO Columns:dt tm Sep| 14 2005 1:32AMSep| 14 2005 1:33AMSep| 14 2005 1:33AMSep| 14 2005 1:34AMSep| 14 2005 1:35AMSep| 14 2005 2:38AMI actually wanted Sept 14 2005 in one column and 2:38 am in the other column.Thanks,DasmanSQL Should be Fun! |
|
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2011-06-08 : 14:27:48
|
So the sample data you posted in the first place isn't what is actually in the table? |
|
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2011-06-08 : 14:32:37
|
You said you have this: 9/14/2005 1:32:52 AMBut apparently, you have this: Sep 14 2005 1:32AMYou can convert it to DATETIME, then use what I posted, or just use DatePart/DateAdd futonctions to parse it into date and time. |
|
|
Dasman
Yak Posting Veteran
79 Posts |
Posted - 2011-06-08 : 15:15:42
|
Hey russell,Sorry if i miscommunicated - here is the data column before query9/14/2005 1:31:52 AMthen i ran ur query and got this in Two SEPARATE columns:Sep 14 2005 1:31AMYour query separated the month and converted it into letters.SQL Should be Fun! |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-06-08 : 21:08:01
|
[code]select date_in_datetime = dateadd(day, datediff(day, 0, StartTime), 0), time_in_str = convert(varchar(8), StartTime, 108)[/code] KH[spoiler]Time is always against us[/spoiler] |
|
|
Dasman
Yak Posting Veteran
79 Posts |
Posted - 2011-07-14 : 11:37:53
|
Hello All,It was in fact Mr. Tan's code that gave me almost what I was looking for.This did the deed:Select Table1.StartTime,date_in_datetime = convert(varchar(10), StartTime, 102),time_in_str = convert(varchar(8), StartTime, 108)From Table1and I get this in TWO columns:2005.09.14 01:31:52from 2005-09-14 01:31:52.383Thanks a lot everyone!Best,Dasman==========================Pain is Weakness Leaving the Body. |
|
|
|