如果日期为空(SQL Server CE)
我正在尝试查询一个包含如果日期为空(SQL Server CE),sql,sql-server-ce,Sql,Sql Server Ce,我正在尝试查询一个包含status列和DateSent列的表。在我的查询中,我想将这两列合并为一个sent列-这样,如果状态为c(关闭/发送),则显示DateSent-但如果不是,则显示相应的状态 任何未发送的行,都将日期发送设为空 预期输出如下: ID Sent ----------------- 1 Queued 2 Failed 3 2013-02-28 4 Queued 我当前的SQL Server CE查询: select I
status
列和DateSent
列的表。在我的查询中,我想将这两列合并为一个sent
列-这样,如果状态为c
(关闭/发送),则显示DateSent
-但如果不是,则显示相应的状态
任何未发送的行
,都将日期发送
设为空
预期输出如下:
ID Sent
-----------------
1 Queued
2 Failed
3 2013-02-28
4 Queued
我当前的SQL Server CE查询:
select
ID, DateAdded,
case when DateSent is null then
(case when Status='W' then 'Waiting' else
case when Status='O' then 'Uploaded' else
case when Status='F' then 'Failed' else
case when Status='E' then 'Expired' else
'Unknown' end end end end) else DateSent
end as Sent
from table
但是第7行的else-DateSent
部分抛出了一个错误:
日期格式中存在语法错误。[0,0,0,表达式:失败,,]
有人能帮我解决这个问题吗
编辑:一个更容易阅读的问题片段是:
select case when DateSent is null
then 'null' else DateSent --<---- problem is here
end as Sent from table
选择DateSent为空时的大小写
然后“null”else DateSent--试试这个;(您可能需要将DateSent转换为nvarchar)
是的,我的错。我看错了。但你为什么要把它弄得这么复杂?您可以对一个CASE
表达式执行相同的操作。@ypercube我大约在一个月前开始学习SQL,因此我不太熟悉最简单/最有效的方法:)通过此查询,我得到错误:无效参数to date encode
-当您说“convert DateSent to varchar”-您的意思是转换查询中的输入吗?或者转换列的数据类型?(我真的不想做后者)不是字段数据类型:)。在案例的else部分的datesent输出中。在SQLServer中,它是“convert(varchar(30),datesent)”,不确定CE语法。啊哈!成功了!对于CE,它需要是“nvarchar”而不是“varchar”。请添加答案,我将接受/投票:)完成!我在手机上,打字有点不一样。tnx
select ID,
DateAdded,
case when DateSent is null
then (case status when 'W' then 'Waiting'
when 'O' then 'Uploaded'
when 'F' then 'Failed'
when 'E' then 'Expired'
else 'Unknown'
end)
else convert(nvarchar(30), DateSent)
end as Sent
from table