Link to home
Start Free TrialLog in
Avatar of RecipeDan
RecipeDan

asked on

Variable in SQL Select Statement

I am trying to show a list of teams and individuals. What I want to do is show a Person's Name that has been entered in a textbox when the Person's Name is null in the database. I do not want to use the Update Statement because it will only be temporary.  
string varPerson = Request.Form["PersonName"];
SELECT NumGames, PersonTeam, CASE WHEN PersonName IS NULL THEN varPerson END AS PersonName
FROM
(
Select NumGames, PersonTeam, PersonName FROM TeamData
)
TABLE1
Order by PersonTeam

Open in new window

Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

Try using the ISNULL function:
ISNULL(PersonName, varPerson)
Avatar of RecipeDan
RecipeDan

ASKER

I tried this and nothing happens:
SELECT NumGames, PersonTeam, ISNULL(PersonName, varPerson) as PersonName
FROM
(
Select NumGames, PersonTeam, PersonName FROM TeamData
)
TABLE1
Order by PersonTeam

Open in new window

I get an invalid column name varPerson
ASKER CERTIFIED SOLUTION
Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you that works. Will Not Null be the same way?
When you use NOT NULL in a query, it is used for comparison. (Does this field have a value?) The ISNULL function uses the first parameter in the list that is not null. The CASE statement in your original query would have worked if you had included an ELSE condition. The ISNULL function is a little cleaner to use.