Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assigning Boolean based on return value 2

Status
Not open for further replies.

Bene1

Programmer
Aug 17, 2001
7
US
I am trying to assign a value to a variable based on the results of a view. For example, if the value of a column comes back as NULL then I want my variable to equal 0. If the column returns something other than NULL, then I want the variable to be assigned a 1. Is this even possible within SQL? I am not very familiar with SQL, but I would be able to do this in other languages, and considering the popularity of SQL, I have to believe this is possible. Any help would be greatly appreciated... Here is the code that I am currently working with...

SELECT a.*, b.user_name
FROM groups As a
LEFT OUTER JOIN
(SELECT User_Name, Group_Name
FROM user_access
WHERE User_Name = 'name') As b
ON a.name=b.group_name


And if the value of User_Name comes back as NULL then I need a variable to equal 0. If the value comes back other than NULL, then I need the variable set to 1. Thanks in advance...

Bene1
 
SELECT a.*,
Coalesce(Convert(bit, b.user_name), 0)
FROM groups As a
LEFT OUTER JOIN (SELECT User_Name,
Group_Name
FROM user_access
WHERE User_Name = 'name') As b
ON a.name=b.group_name

 

Here is one way to set the variable in SQL.

Declare @var int

Select @var =
CASE
WHEN user_name IS NULL 0
ELSE 1
END
FROM (
SELECT a.*, b.user_name
FROM groups As a
LEFT OUTER JOIN
(SELECT User_Name, Group_Name
FROM user_access
WHERE User_Name = 'name') As b
ON a.name=b.group_name) As x Terry L. Broadbent
faq183-874 contains some tips and ideas for posting questions in these forums. Please review it and comment if you have time.
NOTE: Reference to the FAQ is part of my signature and is not directed at any individual.
 
Thanks both of you. I got it working now...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top