March 9, 2010 at 9:39 pm
hi,
i have a table like below :
Day1 Day2 Day3 Day4 Day5
12 3 NULL 43 11
i want to select all field except field wich have NULL
i want query return table like below,
Day1 Day2 Day4 Day5
12 3 43 11
can somebody help me???
thanks before :]
March 9, 2010 at 9:48 pm
What would be the expected result if you have data similar to this???
DECLARE@tblTable TABLE
(
Day1 INT,
Day2 INT,
Day3 INT,
Day4 INT,
Day5 INT
)
INSERT INTO @tblTable
SELECT 12, 3, NULL, 43, 11
UNION ALL
SELECT 12, NULL, 3, 43, 11
UNION ALL
SELECT 12, 3, 43, NULL, 11
SELECT * FROM @tblTable
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
March 10, 2010 at 1:24 am
Whatever the requirements turn out to be, you can remove NULLs and get the data into an easy format with UNPIVOT:
DECLARE @T
TABLE (
Day1 INTEGER NULL,
Day2 INTEGER NULL,
Day3 INTEGER NULL,
Day4 INTEGER NULL,
Day5 INTEGER NULL
)
INSERT @T
(Day1, Day2, Day3, Day4, Day5)
SELECT 12, 3, NULL, 43, 11 UNION ALL
SELECT 12, NULL, 3, 43, 11 UNION ALL
SELECT 12, 3, 43, NULL, 11;
SELECT U.Name,
U.Value
FROM @T T1
UNPIVOT (
Value FOR
Name IN (Day1, Day2, Day3, Day4, Day5)
) U
ORDER BY
U.Name,
U.Value;
Output:
Day1 12
Day1 12
Day1 12
Day2 3
Day2 3
Day3 3
Day3 43
Day4 43
Day4 43
Day5 11
Day5 11
Day5 11
Paul
March 10, 2010 at 2:41 am
below will give you those column names and values which have not any null value
;with cte as
(
SELECT ColumnNames, Value, COUNT(ColumnNames) AS cnt
FROM
(
SELECT day1, day2, day3, day4, day5 FROM days
) p
UNPIVOT
( Value For ColumnNames IN (day1, day2, day3, day4, day5) ) AS U
GROUP BY ColumnNames, Value
)
, cte1 as
(
SELECT Max(cnt) mxcnt FROM cte
)
SELECT ColumnNames, Value FROM cte, cte1
WHERE cte.cnt = cte1.mxcnt
Vaibhav K Tiwari
To walk fast walk alone
To walk far walk together
March 10, 2010 at 2:52 am
sinamora (3/9/2010)
Day1 Day2 Day3 Day4 Day512 3 NULL 43 11
what would happen/expected result, if there will be more than one row?
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
March 10, 2010 at 3:03 am
Bhuvnesh (3/10/2010)
sinamora (3/9/2010)
Day1 Day2 Day3 Day4 Day512 3 NULL 43 11
what would happen/expected result, if there will be more than one row?
That is what Mr. Kingston had asked earlier.
Vaibhav K Tiwari
To walk fast walk alone
To walk far walk together
March 10, 2010 at 5:55 pm
vaibhav.tiwari (3/10/2010)
Bhuvnesh (3/10/2010)
sinamora (3/9/2010)
Day1 Day2 Day3 Day4 Day512 3 NULL 43 11
what would happen/expected result, if there will be more than one row?
That is what Mr. Kingston had asked earlier.
Yep... and it still hasn't been answered.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 14, 2010 at 10:28 pm
thanks for all. it's very help me 🙂
March 14, 2010 at 10:48 pm
sinamora (3/14/2010)
thanks for all. it's very help me 🙂
Looks like Jeff isn't getting his answer.
March 15, 2010 at 6:38 am
Paul White (3/14/2010)
sinamora (3/14/2010)
thanks for all. it's very help me 🙂Looks like Jeff isn't getting his answer.
Heh... Diodes, check valves, and one way streets are useful tools... just not on a forum. 😀 It happens.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 15, 2010 at 6:41 am
University flashback: Zener diode
March 16, 2010 at 1:40 am
Unpivot is the best way to achieve this task.
March 16, 2010 at 6:50 am
vijay.s (3/16/2010)
Unpivot is the best way to achieve this task.
It has certainly been suggested at least twice already, but I am intrigued to know how you can say it is 'best' without knowing what the requirements are?
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply