October 25, 2017 at 3:45 am
Dear Friends,
I m validate one alphanumeric character string for learning purpose My character set is
vat='33AAACV'
Declare @vat varchar(10)
set vat='33AAACV'
select 'VALID' where left(@gstno,2) like '%[0-9]' and substring(@gstno,3,5) like '%[A-Z]'
The first two digits number only and the next 5 digits should contains only Alphabets
my query is working fine but if i changed from 5th(AA5CV) digit is replace into number its showing "Valid" kindly help me out how to restrict its should only alphabets?
October 25, 2017 at 4:08 am
raghuldrag - Wednesday, October 25, 2017 3:45 AMmy query is working fine
No it isn't, because it won't even parse. Please post DDL in the form of CREATE TABLE statements, sample data in the form of INSERT statements, your actual query and the results you expect based on your sample data.
John
October 25, 2017 at 7:49 am
John Mitchell-245523 - Wednesday, October 25, 2017 4:08 AMraghuldrag - Wednesday, October 25, 2017 3:45 AMmy query is working fineNo it isn't, because it won't even parse.
:laugh:
Raghuldrag, even when you get the syntax to parse, your use of the LIKE operator with wildcards is not correct for what you're trying to accomplish. Only the % character represents a string. Other wildcard characters - like [ ] - represent single characters.
October 25, 2017 at 7:59 am
SQLPirate - Wednesday, October 25, 2017 7:49 AMJohn Mitchell-245523 - Wednesday, October 25, 2017 4:08 AMraghuldrag - Wednesday, October 25, 2017 3:45 AMmy query is working fineNo it isn't, because it won't even parse.
:laugh:
Raghuldrag, even when you get the syntax to parse, your use of the LIKE operator with wildcards is not correct for what you're trying to accomplish. Only the % character represents a string. Other wildcard characters - like [ ] - represent single characters.
Actually, I think he's got that bit right. LIKE '%[A-Z]' means any string ending in a character from A to Z.
Edit: oh, I see what you mean - the "[A-Z]" portion represents one single character. Yes.
John
October 25, 2017 at 8:07 am
Maybe this is the LIKE syntax you are looking for as in this example:
DECLARE @String AS VARCHAR(7)
SET @String = '33AA5CV'
SELECT CASE WHEN @String LIKE '[0-9][0-9][A-Z][A-Z][A-Z][A-Z][A-Z]' THEN 'Yes' ELSE 'No' END
October 26, 2017 at 8:43 am
Paulo de Jesus - Wednesday, October 25, 2017 8:07 AMMaybe this is the LIKE syntax you are looking for as in this example:
DECLARE @String AS VARCHAR(7)SET @String = '33AA5CV'
SELECT CASE WHEN @String LIKE '[0-9][0-9][A-Z][A-Z][A-Z][A-Z][A-Z]' THEN 'Yes' ELSE 'No' END
I'd extend that one step further, given that the original query had VARCHAR(10) instead of VARCHAR(7), I'd go with:
DECLARE @String AS VARCHAR(10)
SET @String = '33AA5CV'
SELECT CASE WHEN LEFT(@String, 7) LIKE '[0-9][0-9][A-Z][A-Z][A-Z][A-Z][A-Z]' THEN 'Yes' ELSE 'No' END
Although, if this is something you plan on doing more frequently, you might want to consider using a RegEx instead. See This site for an example of how to implement RegEx in SQL Server.
October 26, 2017 at 9:03 am
kramaswamy - Thursday, October 26, 2017 8:43 AMI'd extend that one step further, given that the original query had VARCHAR(10) instead of VARCHAR(7), I'd go with:
DECLARE @String AS VARCHAR(10)SET @String = '33AA5CV'
SELECT CASE WHEN LEFT(@String, 7) LIKE '[0-9][0-9][A-Z][A-Z][A-Z][A-Z][A-Z]' THEN 'Yes' ELSE 'No' END
Although, if this is something you plan on doing more frequently, you might want to consider using a RegEx instead. See This site for an example of how to implement RegEx in SQL Server.
Funny that you add so many characters when one was enough.
DECLARE @String AS VARCHAR(10)
SET @String = '33AA5CV'
SELECT CASE WHEN @String LIKE '[0-9][0-9][A-Z][A-Z][A-Z][A-Z][A-Z]%' THEN 'Yes' ELSE 'No' END
October 26, 2017 at 12:03 pm
Luis Cazares - Thursday, October 26, 2017 9:03 AMkramaswamy - Thursday, October 26, 2017 8:43 AMI'd extend that one step further, given that the original query had VARCHAR(10) instead of VARCHAR(7), I'd go with:
DECLARE @String AS VARCHAR(10)SET @String = '33AA5CV'
SELECT CASE WHEN LEFT(@String, 7) LIKE '[0-9][0-9][A-Z][A-Z][A-Z][A-Z][A-Z]' THEN 'Yes' ELSE 'No' END
Although, if this is something you plan on doing more frequently, you might want to consider using a RegEx instead. See This site for an example of how to implement RegEx in SQL Server.
Funny that you add so many characters when one was enough.
DECLARE @String AS VARCHAR(10)SET @String = '33AA5CV'
SELECT CASE WHEN @String LIKE '[0-9][0-9][A-Z][A-Z][A-Z][A-Z][A-Z]%' THEN 'Yes' ELSE 'No' END
Your method is also SARGable, Luis.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply