我有一个列需要解析并插入到新表中。我非常接近获取我需要的数据,但我似乎无法正确获取语法。以下是我需要解析的数据格式:
包装单#195,UID = Pkg-15094-195
包装单#112,UID = Pkg-41251-241
我只需要装箱单编号。当然,它并不总是2个字符。
看起来比较简单,只需得到字符索引[#]和[,] 然后从[#] + 1的索引处开始[], - [#] - 1的长度。这是一个额外的减法,它搞砸了我的语法:
SELECT substring(IMG_FILE_DESCRIPTION,
CHARINDEX('#', IMG_FILE_DESCRIPTION) + 1,
CHARINDEX(',', IMG_FILE_DESCRIPTION) - CHARINDEX('#', IMG_FILE_DESCRIPTION)
)
AS PKL
FROM MASTER_SCAN_IMAGE
where IMG_SCT_PKEY = '21'
这有效,但给了我太多。如果我尝试添加另一个减法,我会收到语法错误:
SELECT substring(IMG_FILE_DESCRIPTION,
CHARINDEX('#', IMG_FILE_DESCRIPTION) + 1,
(CHARINDEX(',', IMG_FILE_DESCRIPTION) - CHARINDEX('#', IMG_FILE_DESCRIPTION)) -1
)
AS PKL
FROM MASTER_SCAN_IMAGE
where IMG_SCT_PKEY = '21'
Msg 537, Level 16, State 2, Line 1 Invalid length parameter passed to the LEFT or SUBSTRING function.
所以我认为我应该将长度值包装成单个变量,但它也会产生语法错误:
SELECT *
FROM MASTER_SCAN_IMAGE
DECLARE @length int = CHARINDEX(',', IMG_FILE_DESCRIPTION) - CHARINDEX('#', IMG_FILE_DESCRIPTION);
Msg 207, Level 16, State 1, Line 3
Invalid column name 'IMG_FILE_DESCRIPTION'.
Msg 207, Level 16, State 1, Line 3
Invalid column name 'IMG_FILE_DESCRIPTION'.
实际上我似乎无法使用CHARINDEX分配变量,我总是以相同的“无效列名”错误结束。
DECLARE @length bigint = CHARINDEX(',', IMG_FILE_DESCRIPTION);
Msg 207, Level 16, State 1, Line 3
Invalid column name 'IMG_FILE_DESCRIPTION'.
我是否使用int或bigint是charindex可以发送的两种返回类型。
答案 0 :(得分:1)
使用此:
declare @column varchar(200) = 'Packing Slip #195, UID = Pkg-15094-195'
select RIGHT(LEFT(@column, CHARINDEX(',', @column)-1), CHARINDEX('#', REVERSE(LEFT(@column, CHARINDEX(',', @column)-1)))-1)
运行代码here。
答案 1 :(得分:1)
如果您对TVF开放......请考虑以下事项。
厌倦了提取字符串,我修改了一个解析函数来加入两个非类似分隔符。
示例强>
Declare @YourTable table (ID int,IMG_FILE_DESCRIPTION varchar(max))
Insert Into @YourTable values
(1,'Packing Slip #195, UID = Pkg-15094-195')
,(2,'Packing Slip #112, UID = Pkg-41251-241')
,(3,'Packing Slip #222, UID = Pkg-41251-241 and Slip #999') -- Notice two Packing Slips
Select A.ID
,SlipNr = B.RetVal
From @YourTable A
Cross Apply [dbo].[udf-Str-Extract](A.IMG_FILE_DESCRIPTION+',','Slip #',',') B
<强>返回强>
ID SlipNr
1 195
2 112
3 222 <-- Notice multiple slips
3 999 <-- Notice multiple slips
感兴趣的UDF
CREATE FUNCTION [dbo].[udf-Str-Extract] (@String varchar(max),@Delimiter1 varchar(100),@Delimiter2 varchar(100))
Returns Table
As
Return (
with cte1(N) As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
cte2(N) As (Select Top (IsNull(DataLength(@String),0)) Row_Number() over (Order By (Select NULL)) From (Select N=1 From cte1 N1,cte1 N2,cte1 N3,cte1 N4,cte1 N5,cte1 N6) A ),
cte3(N) As (Select 1 Union All Select t.N+DataLength(@Delimiter1) From cte2 t Where Substring(@String,t.N,DataLength(@Delimiter1)) = @Delimiter1),
cte4(N,L) As (Select S.N,IsNull(NullIf(CharIndex(@Delimiter1,@String,s.N),0)-S.N,8000) From cte3 S)
Select RetSeq = Row_Number() over (Order By N)
,RetPos = N
,RetVal = left(RetVal,charindex(@Delimiter2,RetVal)-1)
From (
Select *,RetVal = Substring(@String, N, L)
From cte4
) A
Where charindex(@Delimiter2,RetVal)>1
)
/*
Max Length of String 1MM characters
Declare @String varchar(max) = 'Dear [[FirstName]] [[LastName]], ...'
Select * From [dbo].[udf-Str-Extract] (@String,'[[',']]')
*/
答案 2 :(得分:0)
提取单据ID
declare @column varchar(200) = 'Packing Slip #234234295, UID = Pkg-15094-195'
select substring(@column,charindex('#',@column)+1,charindex(',',@column)-charindex('#',@column)-1)