Solved

Help With Query

Posted on 2013-11-13
7
127 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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:ScottPletcher
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article I will describe the Detach & Attach 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.
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.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

762 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