473,388 Members | 1,209 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

selecting rows from a table

raj
Hi,
can any one help me in solving this issue

i have a table test with 3 columns (city,state,country) of datatype
varchar and notnull
a b c
x y z
d e NULL
NULL b c

i need to select the values from the table which are not null.
on selecting from the table i need to get the output as

a b c
x y z
d e
b c

i should not return me the null column
Thanks in Advance
Sep 15 '08 #1
7 1450
I am not sure I understand what is needed, since you start with
statement that the columns are not NULL and then provide sample data
with NULL values. Also, what do you mean not to return a column if it is
NULL? You can use COALESCE to change NULL values to something else, in
the example below to empty string:

SELECT COALESCE(city, '') AS city,
COALESCE(state, '') AS state,
COALESCE(country, '') AS country
FROM Test;

--
Plamen Ratchev
http://www.SQLStudio.com
Sep 15 '08 #2
raj
On Sep 15, 12:33*pm, Plamen Ratchev <Pla...@SQLStudio.comwrote:
I am not sure I understand what is needed, since you start with
statement that the columns are not NULL and then provide sample data
with NULL values. Also, what do you mean not to return a column if it is
NULL? You can use COALESCE to change NULL values to something else, in
the example below to empty string:

SELECT COALESCE(city, '') AS city,
* * * * COALESCE(state, '') AS state,
* * * * COALESCE(country, '') AS country
FROM Test;

--
Plamen Ratchevhttp://www.SQLStudio.com
Hi,
Thanks for the reply.
when we use this query
SELECT COALESCE(city, '') AS city,
COALESCE(state, '') AS state,
COALESCE(country, '') AS country
FROM Test;

we get the result as

a b c
x y z
d e
b c

in 3rd row the 3rd column is empty and 4th row 1st column is empty .

my requirement is for example say i will select only one row

select * from Test where city="d";

it returns me a row like this
d e NULL

if i use COALESCE it will return me

d e (emptycolumn)

i need to return only two columns which has values ,i dont want the
column which is null to be returned.

city state
d e

this should be my result not

city state country
d e null
i think this requirement is clear.

Regards
Raj
Sep 15 '08 #3
raj wrote:
On Sep 15, 12:33 pm, Plamen Ratchev <Pla...@SQLStudio.comwrote:
>I am not sure I understand what is needed, since you start with
statement that the columns are not NULL and then provide sample data
with NULL values. Also, what do you mean not to return a column if it is
NULL? You can use COALESCE to change NULL values to something else, in
the example below to empty string:

SELECT COALESCE(city, '') AS city,
COALESCE(state, '') AS state,
COALESCE(country, '') AS country
FROM Test;

--
Plamen Ratchevhttp://www.SQLStudio.com

Hi,
Thanks for the reply.
when we use this query
SELECT COALESCE(city, '') AS city,
COALESCE(state, '') AS state,
COALESCE(country, '') AS country
FROM Test;

we get the result as

a b c
x y z
d e
b c

in 3rd row the 3rd column is empty and 4th row 1st column is empty .

my requirement is for example say i will select only one row

select * from Test where city="d";

it returns me a row like this
d e NULL

if i use COALESCE it will return me

d e (emptycolumn)

i need to return only two columns which has values ,i dont want the
column which is null to be returned.

city state
d e

this should be my result not

city state country
d e null
i think this requirement is clear.
Not when considering multiple rows, as in your original example (where
one row had null country, another had null city).

SQL returns data in regular patterns. You should accept returned data
containing nulls, and interpret them as "no data", because that's what
nulls are for.
Sep 15 '08 #4
raj
On Sep 15, 3:01*pm, Ed Murphy <emurph...@socal.rr.comwrote:
raj wrote:
On Sep 15, 12:33 pm, Plamen Ratchev <Pla...@SQLStudio.comwrote:
I am not sure I understand what is needed, since you start with
statement that the columns are not NULL and then provide sample data
with NULL values. Also, what do you mean not to return a column if it is
NULL? You can use COALESCE to change NULL values to something else, in
the example below to empty string:
SELECT COALESCE(city, '') AS city,
* * * * COALESCE(state, '') AS state,
* * * * COALESCE(country, '') AS country
FROM Test;
--
Plamen Ratchevhttp://www.SQLStudio.com
Hi,
Thanks for the reply.
when we use this query
*SELECT COALESCE(city, '') AS city,
* * * * *COALESCE(state, '') AS state,
* * * * *COALESCE(country, '') AS country
*FROM Test;
we get the result as
a * * * * *b * * * * * * * c
x * * * * *y * * * * * * * z
d * * * * *e
* *b * * * * * * * c
in 3rd row the 3rd column is empty and 4th row 1st column is empty .
my requirement is for example say i will select only one row
select * from Test where city="d";
it returns me a row like this
d * * * * *e * * * * * * * NULL
if i use COALESCE it will return me
d * * * * *e * * * * * * * (emptycolumn)
i need to return only two columns which has values ,i dont want the
column which is null to be returned.
city state
d * * *e
this should be my result not
city state country
d * * e * * * null
i think this requirement is clear.

Not when considering multiple rows, as in your original example (where
one row had null country, another had null city).

SQL returns data in regular patterns. *You should accept returned data
containing nulls, and interpret them as "no data", because that's what
nulls are for.- Hide quoted text -

- Show quoted text -
Thanks alot..:-)
Sep 15 '08 #5
>I should not return me the NULL column <<

A table is made of rows; all rows have the same structure. Each row
has a fixed number of columns of a fixed data type and with known
constraints. The rows ALWAYS have the same structure. A row is not a
record; a record can be variant, which is what you want.

I think that you need to read a book on RDBMS so you will not post
silly things on a newsgroup.
Sep 15 '08 #6
raj (2r*******@gmail.com) writes:
my requirement is for example say i will select only one row

select * from Test where city="d";

it returns me a row like this
d e NULL

if i use COALESCE it will return me

d e (emptycolumn)

i need to return only two columns which has values ,i dont want the
column which is null to be returned.

city state
d e

this should be my result not

city state country
d e null
i think this requirement is clear.
For a single row this is possible by using dynamic SQL, but I am not
going to show it, because while the requirement might be clear, it
does not make sense. You simply don't use SQL this way. It's like
taking the vaccum cleaner to the grocery store to carry the produce back
home with you. You can do it, but everyone would consider you crazy if you
did.

I don't know what your real business requirment is, but you should
probably look for a client-side solution.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

Sep 15 '08 #7
"Erland Sommarskog" <es****@sommarskog.sewrote in message
news:Xn**********************@127.0.0.1...
raj (2r*******@gmail.com) writes:
>i think this requirement is clear.

For a single row this is possible by using dynamic SQL, but I am not
going to show it, because while the requirement might be clear, it
does not make sense. You simply don't use SQL this way. It's like
taking the vaccum cleaner to the grocery store to carry the produce back
home with you. You can do it, but everyone would consider you crazy if you
did.
Oh wow. You owe me a keyboard! That is great and to the point.

It reminds me of the phrase used by some scientists talk about certain
theories: "Not even wrong."

--
Greg Moore
Ask me about lily, an RPI based CMC.
Sep 16 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Axial | last post by:
Question: How to select columns from Excel-generated XML when some cells are empty. I've found examples where rows are to be selected, but I can't seem to extrapolate from that to selecting...
2
by: Robert | last post by:
Hi All, I'm trying to solve this for a few days now and I just can't figure it out... I have three tables set up, I'll simplify them for this question: Table 1: HOTELS Columns: HOTEL_ID,...
4
by: remote89 | last post by:
Hi experts, I have been trying to limit the table rows in the following situation, any suggestions will be appreciated. we have table called tempTb has columns id, c_id, c_name, rating, date...
5
by: jamie | last post by:
Hi, I have a table with 1 million rows that I need to delete. Is there a way to delete them quickly considering I get errors if I try to select them all and then hit delete? What I'm...
6
by: aaj | last post by:
Hi all I use a data adapter to read numerous tables in to a dataset. The dataset holds tables which in turn holds full details of the records i.e. keys, extra colums etc.. In some cases I...
1
by: Jay | last post by:
Hi All, My users are complaining about the page refreshing when they are selecting multiple rows in a datagrid. Has anyone tried to manage this using javascript? I tried smartnavigation but that...
2
by: Mike Kelly | last post by:
Hi. I have a data table where rows are grouped according to a certain criteria and I want to be able to display all the rows that belong to the same group together on the screen. In addition, I...
48
by: phillip.s.powell | last post by:
MySQL 3.23.58 - 4.0.17 (yep, several database server instances, don't ask) I have database Spring with table Students I have database Summer with table Students I am tasked to produce a...
1
by: Andy | last post by:
Hi Gang I have a simple single table that has many duplicate rows in them. The distinctrows only works on more than one table. Is there a quick and easy way to select only the distict rows...
5
by: Mahesh S | last post by:
Hi I would like to write a SQL select statement that would fetch rows numbered 50 to 100. Let me clarify, if i say "fetch first 10 rows only", it returns the first 10 rows from the resultset....
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.