简体   繁体   中英

Troubleshoot conversion failed when converting date and /or time from character string

I know this has been addressed 1000 times but something screwy is going on and I need help with ideas to troubleshoot.

Using MS SQL Server 2012 I have a date stored as INT in the YYYYMMDD format.

I need to turn that into a date and I always use this: CONVERT(DATE,CONVERT(VARCHAR,YYYYMMDD),101) *I know not specifying the length of the varchar is bad form but where I'm doing this has never caused an issue and just to be thorough I tried it anyway to no avail.

That conversion always works. Always, until today.

Today, this conversion doesn't work and I get the above mentioned error in title.

One thing I've done is run part of the query to look at the values to make sure I don't find something dumb like this in my values:

   20170102
   20170304
-->2017ABCD
   20170704

What else can I do?

To expand on my initial comment

Example

Declare @YourTable table (SomeCol varchar(25))
Insert Into @YourTable values
('20170102'),
('20170304'),
('2017ABCD'),
('20170704')

Select SomeCol
      ,AsDate  = try_convert(date,SomeCol)
 From  @YourTable

Returns

SomeCol     AsDate
20170102    2017-01-02
20170304    2017-03-04
2017ABCD    NULL
20170704    2017-07-04

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM