December 1, 2014 at 9:28 am
Hi All,
I'm looking to put a script together to flag the format of a string as (true/False)
example;
String must equal the below format
NNNN/AAAAAAAA
(N= Numeric + '/' + A = Alpha)
E.g.
1234/ABCDEFGH Flag as True - as meets criteria
123D/ABCDEFGH Flag as False - as there is an Alpha in the numeric section of the string
1234ABCDEFGH Flag as False - as the string is missing '/'
Would you anyone be able to supply any pointers in the best way to approach this task.
Many Thanks in advance.
December 1, 2014 at 9:37 am
This is one way of doing it.
SELECT String,
CASE WHEN String LIKE '[0-9][0-9][0-9][0-9]/[a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z]'
THEN 'True' ELSE 'False' END
FROM( VALUES
('1234/ABCDEFGH'), --Flag as True - as meets criteria
('123D/ABCDEFGH'), --Flag as False - as there is an Alpha in the numeric section of the string
('1234ABCDEFGH' ) --Flag as False - as the string is missing '/'
)x(String)
If you have any questions, feel free to ask.
December 1, 2014 at 9:37 am
You can use ranges for pattern-matching, such as [0-9] and [A-Z]. See the LIKE topic in Books Online for more information.
John
December 2, 2014 at 1:24 am
Thank you for your feedback - very helpful and much appreciated.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply