簡體   English   中英

從字符串轉換日期和/或時間時轉換失敗

[英]Conversion failed when converting date and/or time from character string

我有這個問題:

set IDENTITY_INSERT dbo.OtherData1 ON


INSERT INTO OtherData1 (OtherDataID, EmployeeID, OtherDate, OType, OSubject, StatementNo, StatementDate, Info, OtherAs, FolderSerial)

    SELECT OtherDataID, EmployeeID, 
                 CONVERT(DATE, OtherDate, 103), 
                     OType, OSubject, StatementNo, 
                     CONVERT(DATE, StatementDate), Info,
                     CASE OtherAs
           WHEN  'f' THEN 1
              WHEN  's' THEN 2
              WHEN 't' THEN 3
              WHEN 'f' THEN 4
              WHEN 'p' THEN 5
              WHEN 'o' THEN 6
           ELSE NULL END
           , FolderSerial
    FROM OtherData

我執行它時遇到此錯誤:

Msg 241, Level 16, State 1, Line 5
Conversion failed when converting date and/or time from character string.

在轉換之前,StatementDate,OtherDate的值采用此格式"31/1/1994"作為字符串

問題是什么?

編輯

如果我添加這行代碼:

CASE WHEN ISDATE(OtherDate) = 1 THEN 
  CONVERT(DATE, OtherDate, 103)
ELSE NULL END

我試過它並且它不起作用,我需要一些類似的東西來檢查vaild是否執行轉換,如果沒有插入NULL值,我得到相同的錯誤消息

ISDATE將無法工作,因為您甚至無法使用CONVERT指定日期格式。 以下是一個非常精細的測試,將有效日期重新格式化為YYYYMMDD,ISDATE可以預測。

with otherdata(StatementDate) as (
select convert(varchar(10),'1/10/2011') union all
select '28/2/2911' union all
select '8/12/2011' union all
select '13/13/2011' union all
select '13/12/2011' union all
select '12/13/2011' union all
select '2/29/2011' union all
select '29/2/2011' union all
select '29/2/2012' union all
select '2011-02-01' union all
select '1/1/11' union all
select '1/1/99' union all
select '' union all
select null)

-- THE QUERY YOU NEED is below this line. The above virtually sets up a table
-- without having to physically create it
select
    statementdate,
    YYYYMMDDToTest,
    ISDate(YYYYMMDDToTest)
from otherdata
cross apply
       (
       select TheYear = case
       when not statementdate like '%[0-9]/%[0-9]/%[0-9][0-9]' then null
       when convert(int,replace(statementdate,'/','')) != replace(statementdate,'/','') then null
       when statementdate like '%[0-9]/%[0-9]/[0-9][0-9]' then
           case when RIGHT(statementdate,2) >=50
           then '19'+RIGHT(statementdate,2)
           else '20'+RIGHT(statementdate,2)
           end
       when statementdate like '%[0-9]/%[0-9]/[1-9][0-9][0-9][0-9]' then
           RIGHT(statementdate,4)
       end
       ) A
cross apply
       (
       select YYYYMMDDToTest = case
       when TheYear is not null then
           TheYear
           + -- month
           right(100+SUBSTRING(statementdate, charindex('/',statementdate) +1,
           charindex('/',statementdate,charindex('/',statementdate)+1)-
           charindex('/',statementdate)-1),2)
           + -- day
           right(100+LEFT(statementdate, charindex('/', StatementDate) -1),2)
       end
       ) B
WHERE ISDate(YYYYMMDDToTest) = 0

注釋掉最后一行WHERE ISDate(YYYYMMDDToTest) = 0 ,看看它對每個日期的作用。


編輯

您可以將其轉換為替換ISDATE的函數 - 但是對於SPECIFIC格式[d] d / [m] m / yyyy。

create function dbo.superIs103Date(@any varchar(50))
returns bit as begin
declare @theyear varchar(10)
set @TheYear = case
       when not @any like '%[0-9]/%[0-9]/%[0-9][0-9]' then null
       when convert(int,replace(@any,'/','')) != replace(@any,'/','') then null
       when @any like '%[0-9]/%[0-9]/[0-9][0-9]' then
           case when RIGHT(@any,2) >=50
           then '19'+RIGHT(@any,2)
           else '20'+RIGHT(@any,2)
           end
       when @any like '%[0-9]/%[0-9]/[1-9][0-9][0-9][0-9]' then
           RIGHT(@any,4)
       end
declare @YYYYMMDDToTest varchar(50)
set @YYYYMMDDToTest = case
       when @TheYear is not null then
           @TheYear
           + -- month
           right(100+SUBSTRING(@any, charindex('/',@any) +1,
           charindex('/',@any,charindex('/',@any)+1)-
           charindex('/',@any)-1),2)
           + -- day
           right(100+LEFT(@any, charindex('/', @any) -1),2)
       end
return ISDate(@YYYYMMDDToTest)
end
GO

-- example usage
select dbo.superIs103Date('33/1/1700')
-- returns 0

嘗試使用103進行第二次轉換

這不會得到所有可能的無效日期(例如2月30日),但可能會幫助您找到需要修復的日期。

SELECT StatementDate 
 FROM OtherData
 WHERE StatementDate NOT LIKE '[0-3][0-9]/[0-1][0-9]/[1-2][0-9][0-9][0-9]'
  AND  StatementDate NOT LIKE '[1-9]/[0-1][0-9]/[1-2][0-9][0-9][0-9]'
  AND  StatementDate NOT LIKE '[0-3][0-9]/[1-9]/[1-2][0-9][0-9][0-9]'
  AND  StatementDate NOT LIKE '[1-9]/[1-9]/[1-2][0-9][0-9][0-9]'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM