从字符串转换日期和/或时间会导致错误

时间:2016-01-05 09:33:38

标签: sql sql-server

我在下面有一个更新语句,它在执行proc时自动生成:

BEGIN TRAN
UPDATE Core.Config
SET    Value = 'Insert',
       ModifiedBy = 'HARROGATE\Name',
       DeletedDate = '0'
WHERE  ConfigID = '1'
       AND Value = 'Update'
       AND ModifiedBy = 'HARROGATE\Name'
       AND DeletedDate = '0'
IF @@ROWCOUNT <> 1
  BEGIN;
      THROW 99999, 'Error - Single Row not updated, rollback terminated', 1;
  END;
COMMIT TRAN

-- now update the [Core].[TaskChangeLog] to confirm rollback....

UPDATE Tcl
SET    [Hasbeenrolledback] = 1,
       [Rollbackdate] = 'Jan  5 2016  9:21AM',
       tcl.Comments = Tcl.comments + ' Rollback by HARROGATE\Name'
FROM   [Core].[TaskChangeLog] Tcl
       INNER JOIN Core.TaskLog Tl
               ON Tcl.TaskLogID = Tl.TaskLogID
WHERE  Tl.TaskID = 'ABC123_New'
       AND Tcl.TaskChangeLogID = Isnull(58, Taskchangelogid)

但是,当我尝试执行更新说明时,我收到以下错误:

  

从字符转换日期和/或时间时转换失败   字符串。

我认为导致问题的是'DeletedDate',但我不太确定。我的代码中的位置和原因导致此错误?

代码:

MERGE Core.Config AS Tgt
USING (SELECT @ConfigKey,
              @Value,
              @Deleted) AS Src (ConfigKey, Value, Deleted)
ON (Tgt.ConfigKey = Src.ConfigKey)
WHEN MATCHED THEN
  UPDATE SET Value = Src.Value,
             DeletedDate = Iif (@Deleted = 1, GETUTCDATE(), NULL),
             ModifiedBy = SUSER_SNAME(),
             ModifiedDate = GETUTCDATE()
WHEN NOT MATCHED THEN
  INSERT (ConfigKey,
          Value,
          AddedBy,
          ModifiedBy)
  VALUES (@ConfigKey,
          @Value,
          SUSER_SNAME(),
          SUSER_SNAME())
OUTPUT 'Core.Config',
       'ConfigID',
       inserted.ConfigID,
       Iif($action = 'Update', Core.Updxmlfragment('Value', inserted.Value, deleted.Value) + Core.Updxmlfragment('ModifiedBy', inserted.ModifiedBy, deleted.ModifiedBy) + Core.Updxmlfragment('DeletedDate', inserted.DeletedDate, deleted.DeletedDate ), Core.Insxmlfragment('ConfigID') + Core.Addnlxmlfragment ('ModifiedBy', inserted.ModifiedBy))

更新 - 按照用户的要求,粘贴下面的updXMLfragment:

IF (@InsertedValue = '')
  BEGIN
      SET @InsertedValue = ' '
  END

IF(@DeletedValue = '')
  BEGIN
      SET @DeletedValue = ' '
  END

IF(@FieldName IS NULL)
  --if(@InsertedValue IS NULL and @DeletedValue IS NULL or @FieldName IS NULL) -- pre v2.1 logic
  BEGIN
      -- This "errors" the function and ensures the script doesn't get any further than the update
      RETURN CONVERT(INT, 'NULL Params passed for XML Fragment Generation')

  END

-- Validate the date has the correct "time" element associated - i.e HH:MM:SS:MSS
IF (@FieldName LIKE '%date%')
  BEGIN

    IF((TRY_PARSE(@InsertedValue as datetime) IS NOT NULL) OR (TRY_PARSE(@DeletedValue AS datetime) IS NOT NULL))
        BEGIN
            IF((LEN(REPLACE(@DeletedValue, ':', '')) +3 <> LEN(@deletedvalue)) OR (LEN(REPLACE(@InsertedValue, ':', '')) +3 <> LEN(@InsertedValue)))
                BEGIN
                    -- This "errors" the function and ensures the script doesn't get any further than the update
                    RETURN CONVERT(DATETIME, 'Invalid Date Params passed for XML Fragment Generation')
                END
        END
  END

1 个答案:

答案 0 :(得分:2)

我认为DeletedDateDATETIME类型,而更新DeletedDate='0'将在转换时失败。

如果您打算更新为NULL,请使用DeletedDate = NULL

同样在条件下你不应该使用DeletedDate='0'。如果搜索空值,请使用DeletedDate IS NULL