Visa ett inlägg
Oläst 2013-05-11, 01:12 #9
Conny Westh Conny Westh är inte uppkopplad
Klarade millennium-buggen
 
Reg.datum: Aug 2005
Inlägg: 5 166
Conny Westh Conny Westh är inte uppkopplad
Klarade millennium-buggen
 
Reg.datum: Aug 2005
Inlägg: 5 166
Här hittade jag en funktion som splittar upp en textsträng i sql så man får varje ord som en egen rad (MS SQL-Server):

Kod:
Create function dbo.SplitString 
    (
        @str nvarchar(4000), 
        @separator char(1)
    )
    returns table
    AS
    return (
        with tokens(p, a, b) AS (
            select 
                1, 
                1, 
                charindex(@separator, @str)
            union all
            select
                p + 1, 
                b + 1, 
                charindex(@separator, @str, b + 1)
            from tokens
            where b > 0
        )
        select
            p-1 zeroBasedOccurance,
            substring(
                @str, 
                a, 
                case when b > 0 then b-a ELSE 4000 end) 
            AS token
        from tokens
      )
    GO
Så här använder man funktionen för att få fram alla orden:
Kod:
select * 
    from dbo.SplitString('Hello John Smith', ' ')
Så här använder man funktionen för att få fram ett specifikt ord:
Kod:
select * 
    from dbo.SplitString('Hello John Smith', ' ')
    where zeroBasedOccurance=1
Conny Westh är inte uppkopplad   Svara med citatSvara med citat