Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“从”转换的问题;nvarchar";至;日期“;MSSQL_Sql_Sql Server - Fatal编程技术网

“从”转换的问题;nvarchar";至;日期“;MSSQL

“从”转换的问题;nvarchar";至;日期“;MSSQL,sql,sql-server,Sql,Sql Server,我有以下Select语句,这让我发疯: select (Case when Buchungskreis in ('0001', '0002', '0003', '0004') then (Case when Kostenstelle is null then Buchungskreis else Buchungskreis + '.' + Kostenstelle end) else Buchungskreis end), 'SAP-USER Typ ' + Typ , 1, convert(d

我有以下Select语句,这让我发疯:

select
(Case when Buchungskreis in ('0001', '0002', '0003', '0004') then (Case when Kostenstelle is null then Buchungskreis else Buchungskreis + '.' + Kostenstelle end) else Buchungskreis end),
'SAP-USER Typ ' + Typ ,
1,
convert(date, '01.01.2014', 104),
SAPUser
from Z_SAP_USER
where Buchungskreis is not null
and flagmehrfachanmeldung = 0
and BIS not like '00.00.0000'
and convert(date,convert(varchar,bis),104)
between convert(date, '01.01.2014', 104)
and convert(date, '31.01.2014', 104)
他说

Error: Conversion failed when converting date and/or time from character string.
SQLState:  22007
ErrorCode: 241
当我去掉最后三行时,它就起作用了,所以麻烦制造者就在最后三行之内

BIS
是nvarchar,有些行包含值
00.00.0000
,当然不能是convertet do date格式

以下声明有效:

select convert(date,convert(varchar,bis),104) from Z_SAP_USER
where BIS not like '00.00.0000'
这与我在查询中使用的完全相同

我也试过这个,但没有成功:

select
(Case when s1.Buchungskreis in ('0001', '0002', '0003', '0004') then (Case when s1.Kostenstelle is null then s1.Buchungskreis else s1.Buchungskreis + '.' + s1.Kostenstelle end) else s1.Buchungskreis end),
'SAP-USER Typ ' + s1.Typ ,
1,
convert(date, '01.01.2014', 104),
s1.SAPUser
from Z_SAP_USER s1
inner join Z_SAP_USER s2 on (s1.sapuser=s2.sapuser and s2.bis not like '00.00.0000')
where s1.Buchungskreis is not null
and s1.flagmehrfachanmeldung = 0
and convert(date,convert(varchar,s1.bis),104)
between convert(date, '01.01.2014', 104)
and convert(date, '31.01.2014', 104)
当然,我试着用cast代替convert,但那也不起作用。。。
有人知道我的选择有什么问题吗???

因为正如你提到的,你的代码的最后三行给出了问题, 您可以尝试将“00.00.0000”替换为空

and convert(date,convert(varchar,replace(s1.bis,'00.00.0000',''),104)
between convert(date, '01.01.2014', 104)
and convert(date, '31.01.2014', 104)

问题是在SQLWHERE子句中没有逻辑谓词的捷径。因此,即使您通过此方法筛选出了有问题的行

where BIS not like '00.00.0000'
它仍然试图计算出这一点

and convert(date,convert(varchar,bis),104)
这会导致一个错误。 尝试使用
Try\u convert
功能。对于这样的行,它将返回
NULL
,但我认为它不会破坏您的逻辑

and try_convert(date,convert(varchar,bis),104)

该值实际上是日期吗?如果值不是真实日期,则无论您做什么转换和强制转换都无法工作。SQL Server中没有短路或保证的求值顺序-仅仅因为您编写了
,其中a和B
并不意味着系统不会首先尝试求值
B
。我正在考虑缩短的事情…但没有解决这个问题的想法…但这是可行的!!!!谢谢!!!也应该有效。但另一个解决方案更快。无论如何:也谢谢!