简体   繁体   中英

T-SQL Wildcard search - namespace values

I need to satisfy a condition in a string that has "ns[0-9]:" where [0-9] can be any number even greater than 10.

Example:

DECLARE @test TABLE ( value VARCHAR(20))

INSERT INTO @test VALUES
( 'ns1:'),
( 'NOT OK'),
( 'ns7:'),
( 'ns8:'),
( 'ns9:'),
( 'ns10:'), 
( 'ns11:' )


SELECT *, PATINDEX( '%ns[0-9]:%', value ) passes
FROM @test

This only works on 1 to 9, not on 10 and above. I can use [0-9][0-9] but then it only works on 10 and above. I don't want a wild card between the number and the colon either.

I only want the following format to return a 1 with patindex

ns1:, ns2:, ns10:, ns11:, etc.

I also need a non-function solution. For performance reasons I want to use the string like functionality

Thanks

You can use:

select (case when value like 'ns[0-9]%:' and
                  value not like 'ns[0-9]%[^0-9]%:'
             then 1 else 0
        end) as passes_flag

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM