Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a requirement wherein I need to put true or false in to the database when the user entered value lies in between "AA" and "Ä9".
here AA to A9 means.. AA, AB, AC, AD.... AZ, A1, A2,.... A9.

How to achieve this in Tsql instead of writing all the possible values with the IN statement?



Thanks in advance.

What I have tried:

Tried between operator with with AA and A9, but it didn't work out.
Posted
Updated 18-Feb-16 22:45pm

Quote:
Tried between operator with with AA and A9, but it didn't work out.

Of course it doesn't work because you have to find if your string is in the union of two different sets, namely {'AA', 'AB', .. 'AZ'} and {'A1', 'A2', .., 'A9'}.
 
Share this answer
 
That's without using the IN statement:
SQL
DECLARE @InputValue AS NVARCHAR(50) = 'AZ'

SELECT CASE
		WHEN @InputValue LIKE 'A_' THEN 1
		ELSE 0
	   END


Other way if you want use the IN statement there are only 2 option:
1.Specify all value
2.Use IN with a sub query

SQL
SELECT 1
FROM [tabelname] 
WHERE [columns] IN (SELECT SalesReasonKey FROM DimSalesReason);
 
Share this answer
 
This solution has been marked down, not sure why this would be be case.
Maybe it is due to the solution using a pattern matching technique to find your desired result.

In any case this will get you your desired result using pattern matching technique and not a range condition.
-----
In your question you have stated you want to find out if two character are between two ranges.
But you have stated 3 different ranges:
aa to a9
AA to Ä9
AA to A9

You are mixing your case, accent and sensitivity.
Collation and Unicode Support[^]

To answer your question lets consider AA to A9.
You can use PATINDEX with collation Latin1_General_CS_AS to get you desired result.

Here is a bit of sql to demonstrate (also includes results for some other collation):
SQL
with ATab as (
--setup dummy data
	select 'A1' input
	union all select 'a1'
	union all select 'Ä1'
	union all select 'b1'
	union all select 'ba'
	union all select 'AZ'
	union all select 'A9'
	union all select 'A9A'
	union all select 'BA1'
)
select 
	Input,
	patindex('A[A-Z,0-9]', Input COLLATE Latin1_General_CS_AS) PatternPosition,
	'Latin1_General_CS_AS' CollationUsed
from ATab

union all
select 
	Input,
	patindex('A[A-Z,0-9]', Input COLLATE Latin1_General_CS_AI) PatternPosition,
	'Latin1_General_CS_AI' CollationUsed
from ATab
union all
select 
	Input,
	patindex('A[A-Z,0-9]', Input COLLATE Latin1_General_CI_AS) PatternPosition,
	'Latin1_General_CI_AS' CollationUsed
from ATab
union all
select 
	Input,
	patindex('A[A-Z,0-9]', Input COLLATE Latin1_General_CI_AI) PatternPosition,
	'Latin1_General_CI_AI' CollationUsed
from ATab
;

Hope that helps you out.

Also have a read of these for PATINDEX:
PATINDEX (Transact-SQL)[^]
Using SQL Server's CHARINDEX and PATINDEX[^]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900