Solved

Help With Query

Posted on 2013-11-13
7
129 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
always on switch back after failover 2 35
Find results from sql within a time span 11 32
SQL view 2 27
SQL Server Error 21 8 25
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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

809 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