Solved

SQL query help

Posted on 2011-03-15
3
210 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MS SQL Inner Join - Multiple Join Parameters 2 23
Error running stored procedure 11 19
SQL VIEW 7 24
SQL won't work after disabling SSL3 / TLS1 3 18
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.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
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…

863 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

23 Experts available now in Live!

Get 1:1 Help Now