Sunday 15 September 2013

sql - Numberpart of a string in specific range -



sql - Numberpart of a string in specific range -

i'm using sqlserve , need rows part of string within specific numberrange.

as illustration i've got in column uniquestringid:

be09 mytest be10 mytest ce101 mytest ce300 mytest

and want rows number within uniquestringid (the number has located before space) between (including) 10 , 101

so result be:

be10 mytest ce101 mytest

my question here possible sql or need stored procedure that? , if former how sql be?

drop table #t create table #t(id varchar(100)) insert #t values('be09 mytest'), ('be10 mytest'), ('ce101 mytest'), ('ce300 mytest'), ('ce450595 mytest') select id,cast(substring(id,patindex('%[0-9]%',id),patindex('%[a-z0-9] [a-z]%',id)-1) int) #t cast(substring(id,patindex('%[0-9]%',id),patindex('%[a-z0-9] [a-z]%',id)-1) int) between 10 , 101

fiddle demo

fiddle demo few more test cases

sql sql-server

No comments:

Post a Comment