Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have a table with 2 columns minexperience and maxexperience as varchar(max)
those values are
minexperience   maxexperience 
1                   4
2                   5
1                   3
8                   9


when i am retrieving by passing two parameter values those are @minexp and @maxexp

I wrote the query as follows
SQL
select *from table where minexperience between @minexp and @maxexp and maxexperience between @minexp and @maxexp

then I am getting wrong output.

when iam passing 1 and 3 getting correct output.
but when iam passing 8 and 9 getting no output

how can i write the query
Posted
Updated 19-Oct-11 1:08am
v2

The problem is probably that your data is not what you think: never store numbers as strings if you intend to do any match with them - use a number format instead. The chances are that your "8" or "9" is not just "8" or "9" - it may have leading spaces for example.

Use an Int datatype instead.
 
Share this answer
 
Comments
Reiss 19-Oct-11 6:07am    
Good catch
Member 7932936 19-Oct-11 6:11am    
haven't spaces but all large and small no it's working fine but problem with only 8 and 9 only
You only need this surely
SQL
select *from table where minexperience >= @minexp and maxexperience <= @maxexp
 
Share this answer
 
Comments
Member 7932936 19-Oct-11 6:02am    
I write like above query it's working.
but when iam passing minexp >=8 and maxexp <=9
then i have to get one row

but my result is null
This works fine

SQL
create table yourTableName(minexperience nvarchar(max),   maxexperience nvarchar(max))
insert into yourTableName
select 1,                           4
union all select 2,                           5
union all select 1,                           3
union all select 8,                           9

declare @minexp nvarchar(max) = 8, 
		@maxexp nvarchar(max) = 9
select *from yourTableName where minexperience >= @minexp and maxexperience <= @maxexp

we get the output as

HTML
minexperience    maxexperience
---------------- ----------------
8                9
 
Share this answer
 
v2

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