开发者

Convert varchar in different formats to date

I have a table that contains a Date column in varchar format. The date format varies since it's the date format of a computer that used our program. For example, I have the following formats:

  1. 10.7.2010
  2. 7/11/2010 7:36:55 AM

I've tried using:

SELECT        CONVERT(DateTime, Date, 103) AS Expr1
FROM            MyTable 

But I get an out-of-开发者_JAVA技巧range datetime value exception for the following line:

7/13/2010 3:33:45 PM

I need a way to convert those values to a unified format. Time and the output format are not important, as long as it's comparable (when copied to Excel or something. It's to be used externally, so I don't mind not changing the actual values in the DB).

The database is MSSQL 2005.


You need to read up on MSDN Books Online - topic: CAST and CONVERT.

This shows you all the possible, supported date formats, e.g.

CONVERT(VARCHAR(50), GETDATE(), 112)

will convert today's date into ISO format and you'll get 20110707 as the output.

For maximum compatibility, I would recommend the ISO-8601 date format:

YYYYMMDD

or

YYYY-MM-DDTHH:MM:SS

Those always work, can always be converted, regardless of your SQL Server's date, regional and language settings...

Update:

Converting your existing strings into DATE or DATETIME depends heavily on your language/regional settings:

DECLARE @string1 VARCHAR(25) = '10.7.2010'
DECLARE @string2 VARCHAR(25) = '7/11/2010 7:36:55 AM'
DECLARE @string3 VARCHAR(25) = '7/13/2010 3:33:45 PM'

SET DATEFORMAT MDY

SELECT 
    CAST(@string1 AS DATE),
    CAST(@string2 AS DATETIME),
    CAST(@string2 AS DATETIME)

Result here is:

2010-10-07  2010-07-11 07:36:55.000   2010-07-13 07:36:55.000

Dates are interpreted as 7th of October 2010, 11th of July 2011 and 13th of July 2010

SET DATEFORMAT dmy

SELECT 
    CAST(@string1 AS DATE),
    CAST(@string2 AS DATETIME)

Result here is:

2010-07-10  2010-11-07 07:36:55.000

Dates are interpreted as tenth of July 2010 and 7th of November 2011

And of course, converting @string3 to a date with these settings will fail, since it's trying to interpret it as 7th of the 13th month ..... and there is no 13th month.....

Because of such ambiguities, I would recommend to always use the ISO-8601 formats, which are clear, standardized, always work, and it's always defined what date is really represented.

Update #2:

Of course, you could also use the ISDATE() function check if a given string can be interpreted as a valid date:

SELECT DateStr, ISDATE(DateStr) FROM dbo.YourTable

In the case of SET DATEFORMAT DMY, the string value of '7/13/2010 3:33:45 PM' will be recognized as invalid:

SET DATEFORMAT DMY
SELECT ISDATE('7/13/2010 3:33:45 PM')

returns 0 as its value.


Try using CAST and str_to_date


I don't found any error with converting above values to datetime conversion. I think you have data with incorrect date format. Please share such data to get more help. mean while check query to get the date value..

Declare @Table Table
(
    DateTimeCol varchar(100)
)
insert into @Table
Select '10.7.2010' UNION ALL
Select '7/11/2010 7:36:55 AM'

Select Convert(varchar,DateTimeCol,101) As Date
From @Table
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜