August 15, 2010 at 2:31 am
I have a table with 5 columns. My requirement is if record in each column is equal to one 1 then it has to print true or it should print false.
August 15, 2010 at 8:41 am
Is this what you require?
CREATE TABLE #T(C1 INT, C2 INT, C3 INT, C4 INT, C5 INT)
INSERT INTO #T
SELECT 1,1,1,1,0 UNION ALL
SELECT 1,1,1,0,1 UNION ALL
SELECT 1,1,0,1,1 UNION ALL
SELECT 1,0,1,1,1 UNION ALL
SELECT 0,1,1,1,1 UNION ALL
SELECT 1,1,1,1,1
SELECT C1 + C2 + C3 + C4 + C5 AS 'Sum',
CASE C1 + C2 + C3 + C4 + C5 WHEN 5 THEN 'True' ELSE 'False'
END AS Ans
FROM #T
Results:
SumAns
4 False
4 False
4 False
4 False
4 False
5 True
Or if you need to return the column values you could:
SELECT C1,C2,C2,C4,C5,C1 + C2 + C3 + C4 + C5 AS 'Sum',
CASE C1 + C2 + C3 + C4 + C5 WHEN 5 THEN 'True' ELSE 'False'
END AS Ans
FROM #T
Which returns:
C1C2C2C4C5SumAns
111104False
111014False
111114False
100114False
011114False
111115True
August 15, 2010 at 9:41 pm
bitbucket-25253 (8/15/2010)
Is this what you require?
CREATE TABLE #T(C1 INT, C2 INT, C3 INT, C4 INT, C5 INT)
INSERT INTO #T
SELECT 1,1,1,1,0 UNION ALL
SELECT 1,1,1,0,1 UNION ALL
SELECT 1,1,0,1,1 UNION ALL
SELECT 1,0,1,1,1 UNION ALL
SELECT 0,1,1,1,1 UNION ALL
SELECT 1,1,1,1,1
SELECT C1 + C2 + C3 + C4 + C5 AS 'Sum',
CASE C1 + C2 + C3 + C4 + C5 WHEN 5 THEN 'True' ELSE 'False'
END AS Ans
FROM #T
Results:
SumAns
4 False
4 False
4 False
4 False
4 False
5 True
Or if you need to return the column values you could:
SELECT C1,C2,C2,C4,C5,C1 + C2 + C3 + C4 + C5 AS 'Sum',
CASE C1 + C2 + C3 + C4 + C5 WHEN 5 THEN 'True' ELSE 'False'
END AS Ans
FROM #T
Which returns:
C1C2C2C4C5SumAns
111104False
111014False
111114False
100114False
011114False
111115True
Still think that fills the requirement? 🙂
CREATE TABLE #T(C1 INT, C2 INT, C3 INT, C4 INT, C5 INT)
INSERT INTO #T
SELECT 1,2,1,1,0 UNION ALL
SELECT 1,2,1,0,1 UNION ALL
SELECT 1,2,0,1,1 UNION ALL
SELECT 1,0,2,1,1 UNION ALL
SELECT 0,2,1,1,1 UNION ALL
SELECT 1,1,1,1,1
SELECT C1 + C2 + C3 + C4 + C5 AS 'Sum',
CASE C1 + C2 + C3 + C4 + C5 WHEN 5 THEN 'True' ELSE 'False'
END AS Ans
FROM #T
--Jeff Moden
Change is inevitable... Change for the better is not.
August 15, 2010 at 9:43 pm
vrkn82 (8/15/2010)
I have a table with 5 columns. My requirement is if record in each column is equal to one 1 then it has to print true or it should print false.
There's some confusion in my mind about what you want. Are you saying you really only want to return a single row with 5 columns in it that say True or False with "True" meaning the number "1" only appeared in that column a single time no matter how many rows are store in the table?
--Jeff Moden
Change is inevitable... Change for the better is not.
August 16, 2010 at 10:53 pm
What I understand if there is value in each column then return true if not then return false.
select Col1, case when coalesce(COl1,'')='' then false else true as Status from Table_Name
Thanks,
Malay
August 17, 2010 at 8:22 am
malaytech2008 (8/16/2010)
What I understand if there is value in each column then return true if not then return false.select Col1, case when coalesce(COl1,'')='' then false else true as Status from Table_Name
Thanks,
Malay
That's not what I got out of the OP at all. Guess we need to wait and see if we hear from the OP again.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 25, 2010 at 2:48 am
CREATE TABLE #T(C1 INT, C2 INT, C3 INT, C4 INT, C5 INT)
INSERT INTO #T
SELECT 1,1,1,1,0 UNION ALL
SELECT 1,1,1,0,1 UNION ALL
SELECT 1,1,0,1,1 UNION ALL
SELECT 1,0,1,1,1 UNION ALL
SELECT 0,1,1,1,1 UNION ALL
SELECT 1,1,1,1,1
SELECT C1 & C2 & C3 & C4 & C5 AS 'CheckCol',
CASE C1 & C2 & C3 & C4 & C5 WHEN 1 THEN 'True' ELSE 'False'
END AS Ans
FROM #T
August 26, 2010 at 12:46 pm
vrkn82 (8/15/2010)
I have a table with 5 columns. My requirement is if record in each column is equal to one 1 then it has to print true or it should print false.
I assume OP wants 'True' if a 1 and 'False' if anything else.
Here is another way to look at it.
CREATE TABLE #T(C1 INT, C2 INT, C3 INT, C4 INT, C5 INT)
INSERT INTO #T
SELECT 1,2,1,1,0 UNION ALL
SELECT 1,2,1,0,1 UNION ALL
SELECT 1,2,0,1,1 UNION ALL
SELECT 1,0,2,1,1 UNION ALL
SELECT 0,2,1,1,1 UNION ALL
SELECT 1,1,1,1,1
Select case C1
when 1 then 'True'
else 'False'
end as C1,
case C2
when 1 then 'True'
else 'False'
end as C2,
case C3
when 1 then 'True'
else 'False'
end as C3,
case C4
when 1 then 'True'
else 'False'
end as C4,
case C5
when 1 then 'True'
else 'False'
end as C5
from #T
Which returns:
C1C2C3C4C5
TrueFalseTrueTrueFalse
TrueFalseTrueFalseTrue
TrueFalseFalseTrueTrue
TrueFalseFalseTrueTrue
FalseFalseTrueTrueTrue
TrueTrueTrueTrueTrue
"When in danger or in doubt. Run in circles, scream and shout!" TANSTAAFL
"Robert A. Heinlein"
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply