Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Patindex
Message
 
 
To
30/05/2013 13:37:59
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Title:
Environment versions
SQL Server:
SQL Server 2008
Application:
Desktop
Miscellaneous
Thread ID:
01575187
Message ID:
01575199
Views:
30
This message has been marked as a message which has helped to the initial question of the thread.
>Hi All,
>I am trying to create a pattern that will recognize any string, which starts with one or more alpha characters followed by one or more numbers, followed by '-' (dash), followed by one or more numbers only, e.g 'abc1-10' should return >0, but 'abc1-ab10' or 'abc1-10abc' should return 0
>I have tried and tried an failed. Here is my last attempt.
>
>      DECLARE @Tstr  TABLE (strvalue varchar(max),Seq int Identity)
>      INSERT INTO @Tstr (strvalue) VALUES 
>               ('a1-10'),('d5-7'),('abd9-15'),('b-10'),
>               ('cdf-jhsss'),('dd1-msdhe'),('egh2-xx6'),
>               ('egh2-egh12'),('ff4-8jj'),('asd122'),
>               ('U 50'),('For U9'),('1-12')
>      -- I have tried the following
>      -- I need to recognize item 1,2 an 3 only, but this code will also return 1 for the items number 7,8 an 9
>       select *, PATINDEX('%[a-zA-Z]%[0-9]%-%[0-9]%',strvalue) from @Tstr 
>
>
>
>Please help
>Thank you
>Yelena

T-SQL PATINDEX is limited, so I can adjust the above a bit
DECLARE @Tstr  TABLE (strvalue varchar(max),Seq int Identity)
      INSERT INTO @Tstr (strvalue) VALUES 
               ('a1-10'),('d5-7'),('abd9-15'),('b-10'),
               ('cdf-jhsss'),('dd1-msdhe'),('egh2-xx6'),
               ('egh2-egh12'),('ff4-8jj'),('asd122'),
               ('U 50'),('For U9'),('1-12')
      -- I have tried the following
      -- I need to recognize item 1,2 an 3 only, but this code will also return 1 for the items number 7,8 an 9
       select *, PATINDEX('%[a-zA-Z][0-9][-][0-9]%',strvalue) from @Tstr
But I don't see a way to express numbers only after dash. We may try using [-][^a-z] but this will exclude letters but doesn't guarantee numbers only.
You may want to use CLR RegEx instead. If so, you may want to check this article
http://social.technet.microsoft.com/wiki/contents/articles/17335.regex-class.aspx
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform