Solved

Help With Query

Posted on 2013-11-13
7
128 Views
Last Modified: 2013-12-23
Hi,

I have table like this:

char_id
object_id
char_value
number_value
date_value
blob_value


The rules to this are per row only 1 out of:
char_value
number_value
date_value
blob_value

will ever be populated. There will never be circumstances where all 4 are null

Can a select statement (or function) be created that will only ever return the not null value?

At the moment I’m returning 4 columns, 3 of which are blank.

TIA
0
Comment
Question by:andyw27
7 Comments
 
LVL 15

Expert Comment

by:Ess Kay
ID: 39645301
give an example of what u want and what u get
0
 
LVL 15

Expert Comment

by:Ess Kay
ID: 39645313
Something like this:



SELECT "char_value" AS NAME, char_value AS Value FROM yOURtaBLE where char_value  isnot NULL
UNION
SELECT "date_value" AS NAME, date_value  AS Value FROM yOURtaBLE where date_value isnot NULL
UNION
SELECT "number_value" AS NAME, number_value AS Value FROM yOURtaBLE where number_value isnot NULL
UNION
SELECT "blob_value" AS NAME, blob_value AS Value FROM yOURtaBLE where blob_value isnot NULL
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39645314
>Can a select statement (or function) be created that will only ever return the not null value?
Sure.  

Something like this if we're talking 'all columns must be non-NULL..
SELECT yada, yada, yada
FROM your_table
WHERE (char_value IS NOT NULL AND number_value IS NOT NULL AND date_value IS NOT NULL AND blob_value IS NOT NULL)

Open in new window

0
[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

 
LVL 15

Expert Comment

by:Ess Kay
ID: 39645319
Sorry, in my comment IS NOT NULL should have spaces between them


--enjoy
0
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 500 total points
ID: 39645361
SELECT CASE
   WHEN char_value IS NOT NULL THEN 'char_value'
   WHEN number_value IS NOT NULL THEN 'number_value'
   WHEN date_value IS NOT NULL THEN 'date_value'
   WHEN blob_value IS NOT NULL THEN 'blob_value'
   END AS [PopulatedValue],
COALESCE(char_value, number_value, date_value, blob_value) AS [Value]
FROM myTable
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 39646141
SELECT
    COALESCE(char_value, CAST(number_value AS varchar(30)), CONVERT(varchar(30), date_value, 120), CAST(blob_value AS varchar(1000))) AS Value
FROM ...
WHERE ...
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39681311
So .. how's it going?  Lots of good comments here..
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

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

18 Experts available now in Live!

Get 1:1 Help Now