SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to ignore null columns


how to ignore null columns

Author
Message
josetur12
josetur12
Valued Member
Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)

Group: General Forum Members
Points: 72 Visits: 392
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.
Lowell
Lowell
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28072 Visits: 39926
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

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Lowell
Lowell
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28072 Visits: 39926
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

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

josetur12
josetur12
Valued Member
Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)

Group: General Forum Members
Points: 72 Visits: 392
I need to get the last value regardless of the actual date.

I tried both solutions and both work.
Thank you.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)

Group: General Forum Members
Points: 85382 Visits: 41078
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search