Solved

Help With Query

Posted on 2013-11-13
7
131 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

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.​
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.
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 INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

679 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