截断SQL单元格中的特定字符数

时间:2018-06-14 20:26:43

标签: sql sql-server

我在表格中有Dim CopyRng, pasteRng, Outnum, compsht As String Dim myRng, PstRng As Range For j = 5 To 10 For i = 1 To tot_centers Outnum = "out" & j CopyRng = "ThisWorkbook.Sheets(""" & Outnum & """).Cells(" & i & ", 2), ThisWorkbook.Sheets(""" & Outnum & """).Cells(" & i & ", " & tot_days + 2 & ")" Set myRng = Range(CopyRng) Sheets(Outnum).Range(myRng).Copy 'THIS IS WHERE DEBUG HITS ERROR pasteRng = "ThisWorkbook.Sheets(""" & "Compiled Data" & """).Cells(" & ((tot_days * (i - 1)) + (i + 1)) & ", " & j - 2 & ")" Set PstRng = Range(pasteRng) 'AND I'LL PROBABLY GET THE SAME ERROR HERE PstRng.PasteSpecial Transpose:=True Next i Next j

Description

所需的输出

   Description 
  ---------------
    AA Check 
    B1 Check
    RD/AA Check 
    WD_FA Examine 
    FF Examine 
    AA Pass 
    B2 Check 
    Examine  

基本上是一个case语句,它从单元格的左侧截断。注意,列表继续使用不同的名称和条件,并且一些单元格不需要修改(如最后一行),所以类似于case语句但是如果我没有指定条件则不返回NULL 谢谢

6 个答案:

答案 0 :(得分:2)

按照您拥有的空白区域并使用DISTINCT,这会获取空格后面的最后一个单词,或者如果该单词是唯一的单词则返回该单词。

declare @Description table (Description varchar(64))
insert into @Description
values
('AA Check'),
('B1 Check'),
('RD/AA Check'),
('WD_FA Examine'),
('FF Examine'),
('AA Pass'),
('B2 Check'),
('Examine')

select distinct
    right([Description],len([Description]) - charindex(' ',[Description]))
from @Description

答案 1 :(得分:2)

这将数据删除到第一个空格

case when x like '% %' 
     then substring(x, charindex(' ', x) +1, 8000)
     else x
end

'AA Examine Device' -> 'Examine Device'

这提取了最后一句话:

case when x like '% %' 
     then right(x, charindex(' ', reverse(x)) -1)
     else x
end

'AA Examine Device' -> 'Device'

答案 2 :(得分:1)

你可以这样做:

select distinct substring(Description, charindex(' ', Description) + 1, len(Description))

答案 3 :(得分:1)

问题太宽泛了,但你可以使用类似的东西

DECLARE @Product TABLE (
    [Description] nvarchar(255),
    Condition int
);

INSERT @Product ([Description], Condition)
select 'AA Check', 1 union all
select 'B1 Check', 2 union all
select 'RD/AA Check', 3 union all
select 'WD_FA Examine', 3 union all
select 'FF Examine', 4 union all
select 'AA Pass', 1 union all
select 'B2 Check', 5 union all
select 'Examine', 6



select
[Description],
case 
when Condition = 1 then replace([Description],'AA ','')
when Condition = 2 then replace([Description],'B1 ','')
when Condition = 3 then ltrim(rtrim(right([Description],len([Description]) - 5)))
when Condition = 5 then replace([Description],'B2 ','')
when Condition = 6 then [Description]
else 
''
end as 'NewDescription'
from @Product

我希望它有所帮助

答案 4 :(得分:0)

CREATE TABLE [A] ( [Description] VARCHAR(100));

INSERT [A]
VALUES
---------------
( 'AA Check' )
,( 'B1 Check' )
,( 'RD/AA Check' )
,( 'WD_FA Examine' )
,( 'FF Examine' )
,( 'AA Pass' )
,( 'B2 Check' )
,( 'Examine' )
,( 'Examine Device' );

SELECT DISTINCT
    CASE
        WHEN [Description] COLLATE Latin1_General_BIN LIKE '[A-Z][a-z]%' THEN
            [Description]
        WHEN [Description] COLLATE Latin1_General_BIN LIKE '[A-Z][A-Z0-9]%' THEN
            SUBSTRING([Description], CHARINDEX(' ', [Description]) + 1, 8000)
    END AS [Description]
FROM
[A];

DROP TABLE [A];

答案 5 :(得分:0)

也许是这样的:

;WITH CTE (Column1) AS (
    SELECT * FROM (
        VALUES
            ('    AA Check '), ('    B1 Check'), ('    RD/AA Check '), ('    WD_FA Examine '), ('    FF Examine '), 
            ('    AA Pass '), ('    B2 Check '), ('    Examine  ')
        ) AS A (Column1)
    )

SELECT 
    CASE WHEN CHARINDEX('Check', Column1) > 0 THEN SUBSTRING(Column1, CHARINDEX('Check', Column1), 5)
    WHEN CHARINDEX('Examine', Column1) > 0 THEN SUBSTRING(Column1, CHARINDEX('Examine', Column1), 7)
    WHEN CHARINDEX('Pass', Column1) > 0 THEN SUBSTRING(Column1, CHARINDEX('Pass', Column1), 4)
    END AS Results
FROM CTE

如果你有很多变数会很痛苦。