Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

how to ignore null columns Expand / Collapse
Author
Message
Posted Wednesday, February 27, 2013 1:06 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, November 6, 2014 12:25 PM
Points: 36, Visits: 301
Hi, I need help again.
I have a table with five approval dates. The columns could have a value but some could only have one, two or three. Out of all the columns, I need to get the last approval date.
For example, in my sample data, I need to retrieve timestamp4 in row 1, timestamp3 in row 2 and 3, and timestamp2 in row 4.


DDL and sample data.
CREATE TABLE [Dates](
ID [int] NULL,
PO [varchar](50) NULL,
timestamp1 [datetime] NULL,
timestamp2 [datetime] NULL,
timestamp3 [datetime] NULL,
timestamp4 [datetime] NULL
)

Insert into [Dates]
([ID], [PO], timestamp1, timestamp2, timestamp3, timestamp4)
SELECT 1, '12345', 'Jul 3 2012 4:48PM', 'Jul 3 2012 4:48PM', 'Jul 3 2012 4:48PM', 'Jul 3 2012 4:48PM' UNION ALL
SELECT 1, '12345', 'Jul 3 2012 4:48PM', 'Jul 3 2012 4:48PM', 'Jul 3 2012 4:48PM', null UNION ALL
SELECT 1, '12345', 'Jul 3 2012 4:48PM', 'Jul 3 2012 4:48PM', 'Jul 3 2012 4:48PM', null UNION ALL
SELECT 1, '12345', 'Jul 3 2012 4:48PM', 'Jul 3 2012 4:48PM', null, null


Thanks.
Post #1424720
Posted Wednesday, February 27, 2013 1:19 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 10:20 AM
Points: 12,927, Visits: 32,332
great job on giving us the setup DDL and Data.

Your data has all the same datetimes, so it'll probably look better when you compare the example with real data.

this seems to work for me, is this what you are after?

SELECT
ID,
PO,
CASE WHEN ISNULL(timestamp1,'1900-01-01') >= ISNULL(timestamp2,'1900-01-01')
AND ISNULL(timestamp1,'1900-01-01') >= ISNULL(timestamp3,'1900-01-01')
AND ISNULL(timestamp1,'1900-01-01') >= ISNULL(timestamp4,'1900-01-01')
THEN ISNULL(timestamp1,'1900-01-01')
WHEN ISNULL(timestamp2,'1900-01-01') >= ISNULL(timestamp1,'1900-01-01')
AND ISNULL(timestamp2,'1900-01-01') >= ISNULL(timestamp3,'1900-01-01')
AND ISNULL(timestamp2,'1900-01-01') >= ISNULL(timestamp4,'1900-01-01')
THEN ISNULL(timestamp2,'1900-01-01')
WHEN ISNULL(timestamp3,'1900-01-01') >= ISNULL(timestamp1,'1900-01-01')
AND ISNULL(timestamp3,'1900-01-01') >= ISNULL(timestamp2,'1900-01-01')
AND ISNULL(timestamp3,'1900-01-01') >= ISNULL(timestamp4,'1900-01-01')
THEN ISNULL(timestamp3,'1900-01-01')
WHEN ISNULL(timestamp4,'1900-01-01') >= ISNULL(timestamp1,'1900-01-01')
AND ISNULL(timestamp4,'1900-01-01') >= ISNULL(timestamp2,'1900-01-01')
AND ISNULL(timestamp4,'1900-01-01') >= ISNULL(timestamp3,'1900-01-01')
THEN ISNULL(timestamp4,'1900-01-01')
END As MaxDate
FROM [Dates]



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1424723
Posted Wednesday, February 27, 2013 1:23 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 10:20 AM
Points: 12,927, Visits: 32,332
or did you mean the last non-null column, regardless of the datetime value? my example above finds the highest date, regardless of which column.
in that case, assuming your app files the columns up in left to right order(timestamp1,imestamp2,imestamp3,imestamp4) it would be like this:
SELECT 
ID,
PO,
COALESCE(timestamp4,timestamp3,timestamp2,timestamp1) AS MaxDate
FROM [Dates]



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1424726
Posted Wednesday, February 27, 2013 1:41 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, November 6, 2014 12:25 PM
Points: 36, Visits: 301
I need to get the last value regardless of the actual date.

I tried both solutions and both work.
Thank you.
Post #1424732
Posted Wednesday, February 27, 2013 10:19 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 5:34 PM
Points: 35,609, Visits: 32,200
josetur12 (2/27/2013)
I need to get the last value regardless of the actual date.

I tried both solutions and both work.
Thank you.


You might also want to consider changing the structure of the table. The way it is, it'll be interesting when they come up with a 5th date.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1424898
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse