[英]SQL Error : Conversion failed when converting datetime from character string
I've got the following piece of code which exectues within no problem 我有以下代码可以毫无问题地执行
SELECT (
CASE WHEN
(SELECT DateDiff (Day, (
SELECT ChildDOB1 FROM MatterDataDef Where ptMatter = $Matter$),
GETDATE()))>6574 THEN '(over 18)'
ELSE '(' + ChildDOB1 + ')'
END)
FROM dbo.MatterDataDef WHERE ptMatter = $Matter$
Howeveer, when i attempt to wrap the code in brackets (so i can use it as part of a longer equation) i get the following error 'Conversion failed when converting datetime from character string' 但是,当我尝试将代码包装在方括号中(以便将其用作更长的方程式的一部分)时,出现以下错误“从字符串转换日期时间时转换失败”
any help appreciated :) 任何帮助表示赞赏:)
where you have 你在哪里
ELSE '(' + ChildDOB1 + ')'
you'll need to convert it to a character type.. ie varchar 您需要将其转换为字符类型。即varchar
ELSE '(' + cast(ChildDOB1 as varchar(50)) + ')'
SELECT CASE
WHEN Datediff (DAY, childdob1, Getdate()) > 6574
THEN '(over 18)'
ELSE '(' + convert(varchar,childdob1) + ')'
END
FROM dbo.matterdatadef
WHERE ptmatter = $matter$
This is equivalent to your code. 这等效于您的代码。 The extra subquery and brackets are all unnecessary.
多余的子查询和方括号都是不必要的。 The problem is that you are adding childdob1 (datetime) to the brackets without converting to varchar.
问题是您将childdob1(日期时间)添加到了括号中,而没有转换为varchar。 But did you actually want the DOB in a particular format, or the age?
但是,您实际上是否希望DOB采用特定格式或年龄?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.