Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
im passing user value in c# to sql
it may be null or any value.
eg; @city='chennai' or ''

My query : select FirstName from AdmissionTbl where City=@city
if @city='' then it should retrieve all records else only 'chennai' records.
it works only for selected('chennai') value. if it is null it doesn't retrieve any records.
how to query in sql?
Posted

SQL
SELECT FirstName FROM AdmissionTbl (NOLOCK) WHERE (City=@City OR @City IS NULL OR @City = '')
 
Share this answer
 
Since you have C# code in the front end, simply check if you need to pass in a city,
If you don't have to, don't add the parameter to the code and also skip the where clause.
 
Share this answer
 
SQL
SELECT FirstName from AdmissionTbl where City=@city OR CITY IS NULL ;

SELECT CASE WHEN FirstName  IS NULL THEN 'NO VALUE' ELSE  FirstName   where City=@city OR CITY IS NULL ;
 
Share this answer
 
Comments
[no name] 16-Feb-13 1:07am    
For checking Null values in a column, case is not better, try using isnull() function. And use parenthesis () when using OR in your queries.

SELECT isnull(FirstName, 'No-Name') as FirstName from mytable where ( City=@city OR CITY IS NULL )
Prince P Ravi 16-Feb-13 6:16am    
thanks Mr.sheik

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