Solved

Variable in SQL Select Statement

Posted on 2014-10-13
6
110 Views
Last Modified: 2014-10-15
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

0
Comment
Question by:RecipeDan
  • 3
  • 3
6 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40377546
Try using the ISNULL function:
ISNULL(PersonName, varPerson)
0
 
LVL 1

Author Comment

by:RecipeDan
ID: 40377634
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

0
 
LVL 1

Author Comment

by:RecipeDan
ID: 40377639
I get an invalid column name varPerson
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 26

Accepted Solution

by:
Shaun Kline earned 500 total points
ID: 40377663
Are you using a stored procedure for you SQL, or are you building your query in your .NET code?
If it is a stored procedure, you need to pass your .NET variable into it. If you are building the query in .NET, you need to "add" it to your query.

...PersonTeam, ISNULL(PersonName, '" + varPerson + "') as PersonName...

Open in new window

0
 
LVL 1

Author Comment

by:RecipeDan
ID: 40378417
Thank you that works. Will Not Null be the same way?
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40381957
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.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now