Check format of String

  • 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.

  • 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.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • 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

  • 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