Search code examples
sql-serverunicodepatindex

Issue with patindex and unicode character '-'


I have a string called Dats which is either of the general appearence xxxx-nnnnn (where x is a character, and n is a number) or nnn-nnnnnn.

I want to return only the numbers.

For this I've tried:

SELECT Distinct dats, 
Left(SubString(artikelnr, PatIndex('%[0-9.-]%', artikelnr), 8000), PatIndex('%[^0-9.-]%', SubString(artikelnr, PatIndex('%[0-9.-]%', artikelnr), 8000) + 'X')-1)
FROM ThatDatabase

It is almost what I want. It removes the regular characters x, but it does not remove the unicode character -. How can I remove this as well? And also, it seems rather ineffective to have two PatIndex functions for every row, is there a way to avoid this? (This will be used on a big database where the result of this Query will be used as keys).

EDIT: Updated as a new database sometimes contained additional -'s or . together with -.

DECLARE @T as table
(
    dats nvarchar(10)
)

INSERT INTO @T VALUES
('111BWA30'),
('115-200-11')
('115-22.4-1')
('10.000.22')
('600F-FFF200')

Solution

  • I wasn't sure if you wanted the numbers before the - char as well, but if you do, here is one way to do it:

    Create and populate sample table (Please save us this step in your future questions)

    DECLARE @T as table
    (
        dats nvarchar(10)
    )
    
    INSERT INTO @T VALUES
    ('abcde-1234'),
    ('23-343')
    

    The query:

    SELECT  dats,
            case when patindex('%[^0-9]-[0-9]%', dats) > 0 then
                right(dats, len(dats) - patindex('%-[0-9]%', dats))
            else
                stuff(dats, charindex('-', dats), 1, '')
            end As NumbersOnly
    FROM @T
    

    Results:

    dats        NumbersOnly
    abcde-1234  1234
    23-343      23343
    

    If you want the only the numbers to the right of the - char, it's simpler:

    SELECT  dats,
            right(dats, len(dats) - patindex('%-[0-9]%', dats)) As RightNumbersOnly
    FROM @T
    

    Results:

    dats        RightNumbersOnly
    abcde-1234  1234
    23-343      343