T-SQL: PatIndex is a good thing

 

Following are the TSQL scripts which you can use to solve these type of problems. You can convert them to user defined functions as per your requirements:

—Checks that input only contains numbers

if PatIndex(‘%[^0-9]%’,‘11′) > 0
Begin
Print ‘Not all numbers’
End
Else
Begin

Print ‘All numbers’
End

—Checks that input only contains letters
if PatIndex(‘%[^a-z]%’,‘aaaaa’) > 0
Begin
Print ‘Not all letters’
End
Else
Begin

Print ‘All letters’
End

–Checking for mixed input
If PatIndex(‘%[^0-9][0-9]%’,‘abc’) > 0
Begin
Print ‘Alpha numeric data’
End
Else
Begin

Print ‘Either all numbers or all letters’
End

–Checks that value must start with a letter and a number
If PatIndex(‘[^0-9][0-9]%’,‘A1anamwar11′) > 0
Begin
Print ‘Starts with a letter and a number’
End
Else
Begin

Print ‘Does not start with a letter and a number’
End

–Checks that value must End with a letter and a number
If PatIndex(‘%[^0-9][0-9]‘,‘A1anamwar11a1′) > 0
Begin
Print ‘Ends with a letter and a number’
End
Else
Begin

Print ‘Does not End with a letter and a number’
End

–Checks that value must Start with a letter and Ends with a number
If PatIndex(‘[^0-9]%[0-9]‘,‘namwar1′) > 0
Begin
Print ‘Starts with a letter and ends with a number’
End
Else
Begin

Print ‘Does not start with a letter and ends with a number’
End

 
 
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s