Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Question about returning a boolean.

Author  Topic 

wndrboy2k3
Starting Member

37 Posts

Posted - 2008-04-30 : 00:12:55
Hello,

Brand new to the forum and I'm hoping I can get some help from you guys here.

I'm not sure how I would go abouts doing this but I have a table that has a list of my Inventory and it looks like this.

Product Inventory
Modems 10


I want a way (whether it be a stored procedure or view) to display instead of the value of 10, that if there Inventory has a value greater than 0 then to return a boolean of YES, TRUE, or 1. How would I go abouts doing that?

Any help would be greatly appreciated.

WNDRBOY.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-04-30 : 02:31:23
SELECT Product, CASE WHEN Inventory > 0 THEN 'Yes' ELSE 'No' END
FROM Table1



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-04-30 : 09:07:45
If you want 1 or 0,

SELECT Product, cast(Inventory as bit)
FROM Table1

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-04-30 : 09:12:21
Stick with BIT. It is more standard and doesn't force formatting into your data. Most front-end applications and clients that work with SQL Server will translate a BIT column to a boolean.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

wndrboy2k3
Starting Member

37 Posts

Posted - 2008-04-30 : 12:47:27
You guys are amazing! Both solutions worked, but it also brings up more questions. I tried to figure out a way to integrate both solutions (yeah newbie) and have done lots of research on google but have had no luck.

Let's say I want the ">0" to be a ">10" but want it to return a bit. No matter what i do i keep getting "INCORRECT SYNTAX NEAR AS" or "SELECT" or wherever the syntax moves to depending on my attempts. Now, let me bring in another layer of complications.

PRODUCT INVENTORY COLOUR
MODEM 10 black
MODEM2 5 WHITE

Can i set different rules for the colour column being returned? Like for black i want the rule to be >5 return as (1) and for white ">10" return as (1). I certainly don't want you guys to feel like i'm getting you to write code for me, i'm just learning so if you can point me in the right direction, I would really appreciate it.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-04-30 : 12:54:21
Add the extra condition also inside CASE WHEN
Go to Top of Page
   

- Advertisement -