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
245 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 37

Accepted Solution

by:
momi_sabag earned 300 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 125 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 25 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 50 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

690 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