?
Solved

How do I exclude null values from a user defined functionso they don't show up in radio button list?

Posted on 2008-10-25
11
Medium Priority
?
246 Views
Last Modified: 2010-04-21
Here is the code:
select  ID , Answer1 AS Answer
from yourTable
where  ID=1
UNION
select  ID , Answer2 AS Answer
from yourTable
where  ID=1
UNION
select  ID , Answer3 AS Answer
from yourTable
where  ID=1

Except it returns null values in the list if they are empty in the db. How do I EXCLUDE NULL values???
Thanks.
0
Comment
Question by:bolenka
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 37

Expert Comment

by:momi_sabag
ID: 22804041
what do you mean if they are empty? the answers?

try

select id, isnull(answer, 'UnSpecified') from
(select  ID , Answer1 AS Answer
from yourTable
where  ID=1
UNION
select  ID , Answer2 AS Answer
from yourTable
where  ID=1
UNION
select  ID , Answer3 AS Answer
from yourTable
where  ID=1) t
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22804081
what do you want to do when the values are null
want to show them in the radio list or what
the solution that we propose will depend on that
0
 

Author Comment

by:bolenka
ID: 22804117
I dont want them to show in the radiobuttonlist at all....yes, if the Answer field is empty...do not show them in the radiobuttonlist. Thanks.
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 37

Accepted Solution

by:
momi_sabag earned 1200 total points
ID: 22804123
select id, answer
(select  ID , Answer1 AS Answer
from yourTable
where  ID=1
UNION
select  ID , Answer2 AS Answer
from yourTable
where  ID=1
UNION
select  ID , Answer3 AS Answer
from yourTable
where  ID=1) t
where answer is not null
0
 
LVL 9

Assisted Solution

by:jamesgu
jamesgu earned 500 total points
ID: 22804131
select  ID , Answer1 AS Answer
from yourTable
where  ID=1 AND Answer1  IS NOT NULL
UNION
select  ID , Answer2 AS Answer
from yourTable
where  ID=1 AND Answer2  IS NOT NULL
UNION
select  ID , Answer3 AS Answer
from yourTable
where  ID=1 AND Answer3  IS NOT NULL
0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 100 total points
ID: 22804133
momi_sabag has given you the answer before i can even react ot it :)
0
 

Author Comment

by:bolenka
ID: 22807161
I don't think this is working the way I want it to...I have to spend some time trying each one of these before I assign points, what is the"t" for after the select statement momi sabag?

I would like it if it would NOT select the answer at all into the list if the value is NULL:

also, I have to assign an ID to each answer because each answer does not havean id...for example, this is what I have...


ALTER Function [dbo].[radiobuttonListDD] (@ID int)
Returns Table
As
Return
ALTER Function [dbo].[radiobuttonListDD] (@ID int)
Returns Table
As
Return
(select  '1' as IDValue , Answer1 AS Answer
from dbo.userAnswers1
where  ID=@ID
UNION
select  '2' as IDValue , Answer2 AS Answer
from dbo.userAnswers1
where  ID=@ID)
--Select * from dbo.radiobuttonListDD(15390)

I think is gives me results even if the Answer column is null since I am assigning and ID to each. How do I make them not show up AT ALL if the Answer column is NULL?

Thanks so much

0
 

Author Comment

by:bolenka
ID: 22807164
sorry...copied the alter function part 2 x. just ignore the first alter statement.
0
 
LVL 13

Assisted Solution

by:sm394
sm394 earned 200 total points
ID: 22807257
try that any issues let me know

ALTER FUNCTION fnTest
(
      -- Add the parameters for the function here
      @ID int
      
)
RETURNS
@Table_Var TABLE
(
      -- Add the column definitions for the TABLE variable here
      ID int IDENTITY,
      Answer nvarchar(255)
)
AS
BEGIN
      -- Fill the table variable with the rows for your result set


INSERT INTO @Table_Var (Answer)
select   Answer1 AS Answer
from yourTable
where  ID=@ID AND Answer1 IS NOT NULL
UNION
select   Answer2 AS Answer
from yourTable
where  ID=@ID AND Answer2 IS NOT NULL
      RETURN
END
0
 
LVL 37

Expert Comment

by:momi_sabag
ID: 22807661
the t has to be there because of syntax rules
when you use a derived table (that is, a select statement in a where clause surrounded by parenthesis ) you need to give it an alias
0
 

Author Closing Comment

by:bolenka
ID: 31509972
I actually had to change the IS NOT NULL to where Answer <> '' that is how I got the results that I wanted. I wanted the record to not show up in the results table, but the null wasnt working. I tried to split the points to be fair. thanks so much to everyone...I used momi sabag's answer first so that is who I gave the most points. thank you!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Suggested Courses

765 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