2条sql语句,第一条substring 放到where后面报错
提示“传递到 SUBSTRING 函数的长度参数无效。”
第2条放到select后面没有提醒,执行成功,这是为什么?
1.select * from xx
where DXC<>''
and substring(DXC,3,len(DXC)-2)='117' order by GJID desc
2.
select substring(DXC,3,len(DXC)-2),* from XX
where DXC<>''
order by GJID desc
------解决方案--------------------
1.select * from xx
where DXC<>''
and substring(DXC,3,nullif(len(DXC),0)-2)='117' order by GJID desc
------解决方案--------------------
select * from xx
where DXC<>''
and substring(DXC,3,isnull(len(DXC),0)-2)='117' order by GJID desc
------解决方案--------------------
select * from xx
where substring(DXC,3,len(isnull(DXC,'')-2)='117' order by GJID desc
------解决方案--------------------
报错是因为执行顺序不同,放在条件里,在过滤时处理null值或者字段长度小于2的数据,参数就会变成负的。
第二条不报错,因为过滤掉了空值后再去截取数据。