select CAST((CAST(2014 AS VARCHAR) + '-' + CAST(9 AS VARCHAR) + '-' + CAST(31 AS VARCHAR)) AS DATE)
从字符串转换日期和/或时间时转换失败。
如何检查上述问题的条件......
答案 0 :(得分:3)
使用ISDATE()函数检查字符串是否为有效日期
答案 1 :(得分:0)
使用查询:使用ISDate()
IF ISDATE((CAST(2014 AS VARCHAR) + '-' + CAST(9 AS VARCHAR) + '-'
+ CAST(31 AS VARCHAR))) = 1
SELECT CAST(CAST(2014 AS VARCHAR) + '-' + CAST(9 AS VARCHAR) + '-' +
CAST(31 AS VARCHAR) AS DATETIME)
ELSE
SELECT NULL
答案 2 :(得分:0)
使用DateTime ..也是第9个月没有第31个
IF ISDATE(CAST((CAST(2014 AS VARCHAR) + '-' + CAST(9 AS VARCHAR) + '-' + CAST(30 AS VARCHAR)) AS DATETIME)) = 1
PRINT 'VALID'
ELSE
PRINT 'INVALID';