How to find the alphanumeric string of 9 character in sql server with or without special charatcer (other than alphanumeric charatcer like _ or /) e.g. I have column name USER. In this column I have to select values having only alphanumeric characters like 123abc , 234def and not 23*kl or 25/bz.
Asked
Active
Viewed 7,316 times
-4
-
Are you talking about random generated string? – Ankit Bajpai Jun 18 '14 at 06:39
-
pls add some more details.. – vhadalgi Jun 18 '14 at 06:40
-
No. I have to select the string from column having only 9 alphanumeric characters. – user3751203 Jun 18 '14 at 06:41
-
do you have a table which have alphanumeric string and you want that string to be searched ? – Friyank Jun 18 '14 at 06:41
2 Answers
0
try this,
select * from [tbkname] where len([columnname]) = 9 AND columnname like '%[^a-zA-Z0-9]%'

Friyank
- 469
- 3
- 8
-
Thanks Friayank! This is seems to be exact answer. But if I want the values having other characters than alphanumeric values........ not like '%[^a-zA-Z0-9]%' will work or we have to add some more into it? – user3751203 Jun 18 '14 at 06:58
-
-
-
its simple just change the regular expression from alphanumeric to special character expression i.e '%[^.*?[!#$%&()*+,\-./:;<=>?@[\\\]^`{|}~]+.*?$]%' – Friyank Jun 18 '14 at 07:21
0
Are you expecting something like the following,
DECLARE @InputString NVARCHAR(MAX) = 'D!E#$M&*O_@&1@&'
DECLARE @SrchExpr VARCHAR(255)
SET @SrchExpr = '%[^a-z0-9]%'
WHILE PatIndex(@SrchExpr, @InputString) > 0
SET @InputString = Stuff(@InputString, PatIndex(@SrchExpr, @InputString), 1, '')
SELECT @InputString
EDIT: For more generalized solution refer here.
-
Excellent! But I wonder whether we can present it into simple.sql server query instead of T-Sql. Also I have to check if the number of characters in string are 9 – user3751203 Jun 18 '14 at 06:56