如何分割像#ABC; ABCxxz'进入大写' ABC'和小写' xxz'?

时间:2014-06-12 21:26:41

标签: sql sql-server sql-server-2008

我的数据如下:

ABCAbbz 
XXZxxz
ZAAAZa
Xaaaab

我需要将它们分成两列:

ABCA   bbz 
XXZ    xxz
AAAZ   a
X      aaaab

我尝试使用COLLATE Latin1_General_BIN LIKE '%[a-z]',但我不知道如何拆分。

AAxAx永远不会发生。)

3 个答案:

答案 0 :(得分:2)

您可以使用PATINDEX()功能执行此操作,并强制它区分大小写:

SELECT
    SUBSTRING(
        t.MyColumn
        ,0
        ,PATINDEX('%[a-z]%', t.MyColumn COLLATE Latin1_General_BIN))
    AS uppercase
    ,SUBSTRING(
        t.MyColumn
        ,PATINDEX('%[a-z]%', t.MyColumn COLLATE Latin1_General_BIN)
        ,LEN(t.MyColumn))
    AS lowercase
FROM MyTable t

SQL Fiddle Demo

答案 1 :(得分:2)

试试这个:

http://sqlfiddle.com/#!3/4b857a/7

SELECT
LEFT(ColumnA, PATINDEX('%[abcdefghijklmnopqrstuvwxyz]%'
COLLATE Latin1_General_CS_AS,ColumnA)-1),
SUBSTRING(ColumnA, PATINDEX('%[abcdefghijklmnopqrstuvwxyz]%'
COLLATE Latin1_General_CS_AS,ColumnA),LEN(ColumnA))
FROM Table1

答案 2 :(得分:0)

with t as (
select 'ZXXabc' as ColumnToScrub
union
select 'Abdsfsdf'
union
select 'YYYSKHFDSbc')

select 
distinct
    ColumnToScrub,
    left(
        ColumnToScrub,
        max(
            case 
                when ASCII( substring( ColumnToScrub , P.number , 1 ) ) between 65 and 90 then P.number 
                else 0 
            end
        ) over (partition by ColumnToScrub)  
    ) as UpperString,
    right(
        ColumnToScrub,
        len(columnToScrub) - 
        max( 
            case 
                when ASCII( substring( ColumnToScrub , P.number , 1 ) ) between 65 and 90 then P.number 
                else 0 
            end
        ) over (partition by ColumnToScrub) 
    ) as LowString
from t
left join master..spt_values P
            on P.number between 1 and len(t.ColumnToScrub)
            and type ='P'