Tuesday, February 14, 2012

Converting text to date

I have a field that stores a date as text (121205). I need to convert this field to a date, but since it is text, I cannot figure out how to do it. Any ideas? Thanks! :Dwhich database? informix? sybase? db2? access? firebird? oracle? mysql? sql server? postgresql?|||SQL Server|||Moving this thread to Microsoft SQL Server forum, but I'd use:SELECT t, Convert(DATETIME, Stuff(Stuff(t, 5, 0, '/'), 3, 0, '/'))
FROM (SELECT '121205' AS t UNION SELECT '111105' UNION SELECT '101005') AS z
-PatP|||As you can tell, Pat likes to STUFF things...

CREATE PROC mySproc99 @.x text
AS
SELECT CONVERT(datetime, CONVERT(varchar(25),@.x))
GO

EXEC mySproc99 '121205'
GO

DROP PROC mySproc99
GO

So who's to say it's not 2012?|||Brett is correct, you don't need the slashes in the US, and maybe not in the UK either... They assume a string of digits are MMDDYY. As far as I know, the slashes work in any locale.

-PatP

No comments:

Post a Comment