Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 string validation

Author  Topic 

chadbryant5
Starting Member

32 Posts

Posted - 2008-05-23 : 11:52:03
Is there a way in TSQL to validate a string to ensure it has only numbers or letters (no special characters like @,%, etc)?

I want to write a function that validates a number that should only contain letters and numbers. The function should return 1 if valid 0 if not. If it contains anything other than 0-9 or A-Z or a-z then it should return 0.

Any ideas on how to do this in TSQL? I could do this in C# easily with regular expressions, but I don't want to use SQLCLR or any other external resource like that...just straight TSQL.

Thanks!


Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2008-05-23 : 12:52:16
Try this:
DECLARE @Yak TABLE (Val VARCHAR(50))

INSERT @Yak
SELECT '12345678'
UNION ALL SELECT '123AB456'
UNION ALL SELECT 'ABCDEFG'
UNION ALL SELECT 'ABCD123EFG'
UNION ALL SELECT '@#ad123'
UNION ALL SELECT '@#ad123^$'
UNION ALL SELECT 'ad*@123'

SELECT *
FROM @Yak
WHERE Val NOT LIKE '%[^0-9,a-z]%'
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-23 : 12:55:18
See this:-
http://blogs.msdn.com/khen1234/archive/2005/05/11/416392.aspx
Go to Top of Page
   

- Advertisement -