【问题标题】:Handle NULL when using substring function in where clause在 where 子句中使用子字符串函数时处理 NULL
【发布时间】:2013-12-22 05:51:35
【问题描述】:

我必须检查 Col1 的内容,它的值类似于 somestring (someotherstring) 与 Col2(somestring 部分)和 Col3(someotherstring 部分)。 Col2 的长度始终为 10 个字符。 Col1 的值为 NULL。

这就是我现在拥有的:

select * from MyTable
where
substring(Col1,1,10) != Col2
OR substring(Col1,13,LEN(Col1)-13) != Col3

我收到了错误:

Invalid length parameter passed to the LEFT or SUBSTRING function.

我猜 Col1 中的空值是我的问题。我已经尝试过 ISNULL 和 COALESCE 但它仍然无法正常工作。我一定做错了什么。我该如何解决这个问题?

【问题讨论】:

  • 我觉得你的问题是LEN(Col1) - 13返回一个负数
  • 你能展示一些示例数据吗??
  • 您使用的是哪个 DBMS?甲骨文? Postgres?
  • @a_horse_with_no_name SQL Server 2008

标签: sql sql-server-2008 substring


【解决方案1】:

我认为你的问题是LEN(Col1) - 13 返回一个负数

试试这个:

select * from MyTable
where
substring(Col1,1,10) != Col2
OR substring(Col1,13,case when LEN(Col1)-13 >= 0 then LEN(Col1)-13 else 0 end) != Col3

【讨论】:

    猜你喜欢
    • 2013-10-17
    • 1970-01-01
    • 1970-01-01
    • 2013-08-24
    • 2021-12-29
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 2016-11-12
    相关资源
    最近更新 更多