我希望分手,例如'黑色花岗岩壁炉形状为54 x 26',分别为单独的t-sql代码 - 不是复杂的逻辑。
with C1 as
(
select ProductName, SUBSTRING(ProductName, 1, CHARINDEX(' ', ProductName)) as firstword,
SUBSTRING(ProductName, CHARINDEX(' ', ProductName), LEN(ProductName)) as secondword
from [dbo].[AProducts]
),
C2 AS
(
select firstword, secondword
from C1
)
select firstword, SUBSTRING(secondword, 1, CHARINDEX(' ', secondword)) as z from C2
这是PRODUCTNAME EXTRACT:
To credit against Invoice 104 - overcharged in error
Black Granite Hearth to t-shape, 54 x 26
Black Granite Header 38 x 5, Slips 32 x 5
Nero Zimbabwe 30mm worktops, supplied and fitted
答案 0 :(得分:0)
奇怪的要求横向与纵向相对。如果没有动态,你就会冒着丢失文字的风险,但如果有必要,以下内容很容易扩展。
示例强>
Declare @YourTable table (PRODUCTNAME varchar(max))
Insert Into @YourTable values
('To credit against Invoice 104 - overcharged in error')
,('Black Granite Hearth to t-shape, 54 x 26')
,('Black Granite Header 38 x 5, Slips 32 x 5')
,('Nero Zimbabwe 30mm worktops, supplied and fitted')
Select A.*
,C.*
From @YourTable A
Cross Apply ( Select CleanString = replace(replace(replace(A.ProductName,' x ','x'),',',''),' - ',' ') ) B
Cross Apply (
Select Pos1 = ltrim(rtrim(xDim.value('/x[1]' ,'varchar(max)')))
,Pos2 = ltrim(rtrim(xDim.value('/x[2]' ,'varchar(max)')))
,Pos3 = ltrim(rtrim(xDim.value('/x[3]' ,'varchar(max)')))
,Pos4 = ltrim(rtrim(xDim.value('/x[4]' ,'varchar(max)')))
,Pos5 = ltrim(rtrim(xDim.value('/x[5]' ,'varchar(max)')))
,Pos6 = ltrim(rtrim(xDim.value('/x[6]' ,'varchar(max)')))
,Pos7 = ltrim(rtrim(xDim.value('/x[7]' ,'varchar(max)')))
,Pos8 = ltrim(rtrim(xDim.value('/x[8]' ,'varchar(max)')))
,Pos9 = ltrim(rtrim(xDim.value('/x[9]' ,'varchar(max)')))
From (Select Cast('<x>' + replace((Select replace(B.CleanString,' ','§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml) as xDim) as X
) C
<强>返回强>