Reputation: 41
I need to convert date format in SQL The current format is yyyy/mm/dd-hh:mm:ss:sss and I need to convert it to yyyy-mm-dd hh:mm:ss CST I don't really know SQL but did some research and found that I can use instr to find the string and replace it, however, no matter what I try,there is always something off :( Could anyone here help me with it? thanks in advance.
By the way, it's in oracle sql developer so syntax are different from previous answer. Thanks again
Upvotes: 1
Views: 4018
Reputation: 41
thank you so much for your guys help! I got this sorted out by other user's help, the command is,
update b
set first= to_char(substr(FIRST,1,4)||'-'||substr(FIRST, 6, 2)||'-'||substr(FIRST, 9, 2)||' '||substr(FIRST, 12, 8))
Thanks :) Mylie
Upvotes: 1
Reputation: 7093
declare @ds varchar(23)='2012/02/16 12:57:03:002'; --your current format
select convert(varchar(30),cast(@ds as datetime),120)+' CST' --new format you specified
Upvotes: 0
Reputation: 247650
If your current column is stored as a varchar (which it looks like it is based on your example) you can convert it the following way to a datetime.
declare @date varchar(25)
set @date = '2012/02/16-09:40:30:000'
select Convert(datetime, Left(@date, 4) + '-' +
substring(@date, 6, 2) + '-' +
substring(@date, 9, 2) + ' ' +
substring(@date, 12, 8)) As NewDate
And the result would be 2012-02-16 09:40:30.000
There are lots of sites that have details on converting datetime:
If the date is stored as a datetime, then you can get your format by doing:
SELECT convert(varchar, getdate(), 120)
Upvotes: 1
Reputation: 70638
If the date is stored as a DATETIME
, then you can convert it to a string in the format you want by doing the following:
SELECT CONVERT(VARCHAR(19),YourColumn,120)
FROM YourTable
Upvotes: 0
Reputation: 497
http://msdn.microsoft.com/en-us/library/ms187928.aspx
If you're using SQL Server and it's a date field you can do:
convert(varchar,getdate(),120)
Replacing getdate() with the column name
Upvotes: 0