Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.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
Sql 值为null时使用Charindex和right函数_Sql_Sql Server_String - Fatal编程技术网

Sql 值为null时使用Charindex和right函数

Sql 值为null时使用Charindex和right函数,sql,sql-server,string,Sql,Sql Server,String,基本上,我试图从捕获整个路径的列中获取文件名。下面的select语句在数据库刷新之前实际工作。现在它失败了,因为最近没有上传任何文件 以下是我得到的错误: 错误:传递给正确函数的长度参数无效 SELECT right(batch_ID, CHARINDEX('\', REVERSE(batch_ID)) - 1) AS [File Name] from table1 请帮助我找出如何更改语句,以便在存在空值或charindex找不到“\”时仍能正常工作 谢谢 一种选择是使用case语句,类似于

基本上,我试图从捕获整个路径的列中获取文件名。下面的select语句在数据库刷新之前实际工作。现在它失败了,因为最近没有上传任何文件

以下是我得到的错误: 错误:传递给正确函数的长度参数无效

SELECT right(batch_ID, CHARINDEX('\', REVERSE(batch_ID)) - 1) AS [File Name]
from
table1
请帮助我找出如何更改语句,以便在存在空值或charindex找不到“\”时仍能正常工作


谢谢

一种选择是使用case语句,类似于:

declare @path as varchar(100);
set @path = '';

select case when CHARINDEX('\', coalesce(@path, ''),1) > 0 
            then right(@path, CHARINDEX('\', REVERSE(@path)) - 1) 
            else null end

将反斜杠放在批次id之前:
SELECT right(批次id,CHARINDEX('\',reverse('\'+批次id))-1)
谢谢!这正是我要找的!