Click here to Skip to main content
15,893,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi,
ihave a table with two columns minexperience and maxexperience

but iam passing the only one parameter that is @experience by using split function to get between values.

I am writing the query as follows
SQL
select e.jobid,e.jobtitle,e.jobposteddate,e.joblocation,e.minexperience,e.maxexperience,e.jobdescription, d.companyname from emppostingjobs e Inner join admregforemp d on e.comptypeid=d.comptypeid where minexperience and maxexperience between (select items from dbo.Split(@experience,','))

it shows the following error

An expression of non-boolean type specified in a context where a condition is expected, near 'and'.

or
if i write the query as

SQL
select e.jobid,e.jobtitle,e.jobposteddate,e.joblocation,e.minexperience,e.maxexperience,e.jobdescription, d.companyname from emppostingjobs e Inner join admregforemp d on e.comptypeid=d.comptypeid where minexperience >= (select items from dbo.Split('4,9',','))  and maxexperience <= (select items from dbo.Split(15,','))


then it shows the error as

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.


how can i write the query
Posted
Updated 21-Oct-11 1:09am
v2

Hi,

As per my understanding for your question, a user can select multiple check-boxes randomly and you are passing a single parameter @experience. In this case, you can create and execute a dynamic sql query and get desired result.

Given below solution, I have also used a single parameter @experience in this format.

C#
@experience = 
	minimum_experience_of_first_checkbox + "," + maximum_experience_of_first_checkbox + ";" + 
	minimum_experience_of_second_checkbox + "," + maximum_experience_of_second_checkbox + ";" + 
	..
	..
	..
	minimum_experience_of_n_th_checkbox + "," + maximum_experience_of_n_th_checkbox + ";"


Here, you can see that I have used two separators.
"," is used for minimum and maximum years and ";" is used to join checkboxes.

After this, We will just have to put a loop and prepare our dynamic sql & execute it.

SQL
DECLARE @MainSQLQuery VARCHAR(MAX)
SET @MainSQLQuery = 'select e.jobid,e.jobtitle,e.jobposteddate,e.joblocation,e.minexperience,e.maxexperience,e.jobdescription, d.companyname from emppostingjobs e Inner join admregforemp d on e.comptypeid=d.comptypeid where '

DECLARE @experience VARCHAR(MAX)
SET @experience = '1,2;4,5;8,10;'

DECLARE
	@whereQuery varchar(2000),
	@OrCondition varchar(5),
	@separatorCheckBox varchar(1),
	@separatorYear varchar(1),
	@remainingCheckBoxes varchar(2000),
	@splittedCheckBoxVal varchar(500),
	@maxYear varchar(200),
	@minYear varchar(50)
	
SELECT
	@whereQuery = '',
	@OrCondition = ' OR ',
	@separatorCheckBox = ';',
	@separatorYear = ','


IF CHARINDEX(@separatorCheckBox,@experience) <= 0
BEGIN
	SET @experience = @experience + @separatorCheckBox
END


SET @remainingCheckBoxes = @experience

--Create Dynamic SQL Where Query
WHILE ( LEN(@remainingCheckBoxes) > 0 )
BEGIN
	--Split Checkboxes
	SET @splittedCheckBoxVal = SUBSTRING(@remainingCheckBoxes,1,CHARINDEX(@separatorCheckBox,@remainingCheckBoxes)-1)
	SET @remainingCheckBoxes = SUBSTRING(@remainingCheckBoxes,CHARINDEX(@separatorCheckBox,@remainingCheckBoxes)+1,LEN(@remainingCheckBoxes)-1)
	
	--Spit to get minimum and maximum year
	SET @minYear = SUBSTRING(@splittedCheckBoxVal,1,CHARINDEX(@separatorYear,@splittedCheckBoxVal)-1)
	SET @maxYear = SUBSTRING(@splittedCheckBoxVal,CHARINDEX(@separatorYear,@splittedCheckBoxVal)+1,LEN(@splittedCheckBoxVal)-1)

	--Append OR condition
	SET @whereQuery = @whereQuery + @OrCondition + '(minexperience >= ' + @minYear + ' and maxexperience <= ' + @maxYear + ')'
END

--Remove Extra OR condition appended at first position due to while loop
SET @whereQuery = SUBSTRING(@whereQuery,LEN(@OrCondition)+1,LEN(@whereQuery))

--Append this with main query
SET @MainSQLQuery = @MainSQLQuery + @whereQuery

--Finally execute this dynamic sql to get value
EXEC(@MainSQLQuery)


Hope this will help to solve your problem.

Regards,
Ambicaprasad Maurya
 
Share this answer
 
Change your query and pass two values @minexp and @maxexp so your queries will be much simpler

SQL
select e.jobid,e.jobtitle,e.jobposteddate,e.joblocation,e.minexperience,e.maxexperience,e.jobdescription, d.companyname from emppostingjobs e Inner join admregforemp d on e.comptypeid=d.comptypeid where minexperience >= @minexp and maxexperience <= @maxexp
 
Share this answer
 
Comments
Member 7932936 21-Oct-11 7:26am    
i tried that also
even i have problem with split

because I have checkboxes
like

1 to 2 years
2 to 3 years
4 to 5 years
like that
the user can select multiple checkboxes
i have to pass minexp and maxexp values by seperating with ','
so my problem with split function and selecting multi chexkboxes
Mehdi Gholam 21-Oct-11 7:33am    
It's much easier to extract min and max values from your comboboxes in code.
Member 7932936 21-Oct-11 7:58am    
yes,i have to sent as minexp=1,3,4,...
and
maxexp=2,5,7..

even then i have to split in sqlserver
Mehdi Gholam 21-Oct-11 8:02am    
That makes no sense -> min (1,2,4..) = 1 max(2,5,7..) = 7
Member 7932936 21-Oct-11 8:13am    
ok.fine.
thank you

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