Solved

SQL query help

Posted on 2011-03-15
3
209 Views
Last Modified: 2012-05-11
Hi Experts,

I need to create a dataset from a query to show me what the last non blank value in 5 fields within a record.    The fields are TID1, TID2, TID3, TID4 and TID5.

They will be populated in this way - either just the the first one, the first 2, first 3, first 4 or all 5.   There are never any gaps so I so only need to return the last non-blank value.

For example.

TID1 TID2 TID3 TID4 TID5
14    232   244                          (this sholod return 244)
13    231                                   (this should return 231)
12    238  238   256                  (this should return 256)
14    237  250   279  410          (this should return 410)


Hope I have explained this well enough.

Thanks
Jon
0
Comment
Question by:JonYen
3 Comments
 

Expert Comment

by:JoelDev
ID: 35143604
On SQL Server, using T-SQL that would look like this:

SELECT COALESCE(TID5, TID4, TID3, TID2, TID1) AS Field
FROM SYS_Customer

Open in new window


Using ANSI SQL it would look more like this.
SELECT CASE WHEN TID5IS NOT NULL THEN 
				TID5
			WHEN TID4 IS NOT NULL THEN
				TID4
			WHEN TID43IS NOT NULL THEN
				TID3
			WHEN TID2 IS NOT NULL THEN
				TID2
			ELSE
				TID1
			END AS Field
FROM SYS_Customer

Open in new window


The basics of this is your are looking at the fields in reverse until you find one that isn't NULL. If you're comparing against an empty string, just change "IS NOT NULL" to compare for any empty string: "<> ''"
0
 
LVL 23

Accepted Solution

by:
OP_Zaharin earned 500 total points
ID: 35143815
adding to what JoleDev have prepared, you must also consider that empty field sometimes contain spaces instead of NULL therefore if the last field contain spaces instead of NULL, it will take the field that contain spaces as the last field. i would use LEN() function to test the spaces field. you can add ltrim and rtrim too.

i'm suggesting the following SQL:

SELECT
CASE WHEN TID1 IS NOT NULL AND LEN(TID1) > 0 THEN TID1
WHEN TID2 IS NOT NULL AND LEN(TID2) > 0 THEN TID2
WHEN TID3 IS NOT NULL AND LEN(TID3) > 0 THEN TID3
WHEN TID4 IS NOT NULL AND LEN(TID4) > 0 THEN TID4
WHEN TID5 IS NOT NULL AND LEN(TID5) > 0 THEN TID5
ELSE 'No Value Found'  END AS TIDS
FROM TABLENAME

0
 
LVL 2

Expert Comment

by:MTillett
ID: 35146520
You say "non blank".  Are we to assume that the columns are character datatype?  Are they nullable?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now