Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

select all columns that are not null

 
   Database Help (Home) -> Programming RSS
Next:  Newbie: Improving performance?  
Author Message
Billy

External


Since: May 14, 2008
Posts: 3



(Msg. 1) Posted: Wed May 14, 2008 5:46 am
Post subject: select all columns that are not null
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Is there a way to add a where clause to this statement

SELECT * FROM tableName

so that you only return columns that don't have a null in each row?
I don't want blank columns returned......but this will change each
month so I don't want to hard code the column names.

 >> Stay informed about: select all columns that are not null 
Back to top
Login to vote
Billy

External


Since: May 14, 2008
Posts: 3



(Msg. 2) Posted: Wed May 14, 2008 6:22 am
Post subject: Re: select all columns that are not null [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Yes I am working with data that is not normalized. That doesn't mean
I don't realize it. I have to work with the data the way it is.

On May 14, 8:00 am, dave ballantyne
wrote:
> Billy wrote:
> > Is there a way to add a where clause to this statement
>
> > SELECT * FROM tableName
>
> > so that  you only return columns that don't have a null in each row?
> > I don't want blank columns returned......but this will change each
> > month so I don't want to hard code the column names.
>
> Sounds as if you have made a fundemental error in your understanding of
> relational databases.
>
> It sounds as if you table is
>
> PKey Int,
> Month1 int,
> Month2 int,
> Month3 int,
> Month4 int,
> ....
> ...
> ..
>
> It Should be
>
> PKey Int
> MonthNum Int
> MonthValue int
>
> And then populated with rows not columns.
>
> Dave

 >> Stay informed about: select all columns that are not null 
Back to top
Login to vote
Rick Sawtell

External


Since: Jan 14, 2008
Posts: 104



(Msg. 3) Posted: Wed May 14, 2008 8:04 am
Post subject: Re: select all columns that are not null [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Billy" wrote in message

> Is there a way to add a where clause to this statement
>
> SELECT * FROM tableName
>
> so that you only return columns that don't have a null in each row?
> I don't want blank columns returned......but this will change each
> month so I don't want to hard code the column names.

You would need to do that with dynamic SQL.

DECLARE @sql varchar(8000)

SELECT @sql = 'SELECT ' + ISNULL(col1, col1_name) + ISNULL(col2, ', ' +
col2_name) + ISNULL(col3, ', ' + col3_name) + ' FROM tablename'

-- You should probably add some code to ensure that you have at least one
column in the column list.


EXECUTE (@sql)



Rick Sawtell
 >> Stay informed about: select all columns that are not null 
Back to top
Login to vote
billy.rogers

External


Since: Jan 10, 2008
Posts: 8



(Msg. 4) Posted: Wed May 14, 2008 10:56 am
Post subject: Re: select all columns that are not null [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I don't quite understand how to use this. Where do I put the table
name?

Here's the table

CREATE TABLE [dbo].[MRecordCount] (
[Platform] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
[TableName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
[FDMS] [bit] NOT NULL ,
[_20070301] [int] NULL ,
[_20070401] [int] NULL ,
[_20070501] [int] NULL ,
[_20070601] [int] NULL ,
[_20070701] [int] NULL ,
[_20070801] [int] NULL ,
[_20070901] [int] NULL ,
[_20071001] [int] NULL ,
[_20071101] [int] NULL ,
[_20071201] [int] NULL ,
[_20080101] [int] NULL ,
[_20080201] [int] NULL ,
[_20080301] [int] NULL ,
[_20080401] [int] NULL ,
[_20080501] [int] NULL ,
[_20080601] [int] NULL ,
[_20080701] [int] NULL ,
[_20080801] [int] NULL ,
[_20080901] [int] NULL
) ON [PRIMARY]
GO
 >> Stay informed about: select all columns that are not null 
Back to top
Login to vote
Alex Kuznetsov

External


Since: Jan 10, 2008
Posts: 640



(Msg. 5) Posted: Wed May 14, 2008 11:28 am
Post subject: Re: select all columns that are not null [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On May 14, 7:46 am, Billy wrote:
>.but this will change each
> month so I don't want to hard code the column names.

This indicates a poor database design. Can you describe your problem
so that we can design a better table for your needs?
 >> Stay informed about: select all columns that are not null 
Back to top
Login to vote
billy.rogers

External


Since: Jan 10, 2008
Posts: 8



(Msg. 6) Posted: Wed May 14, 2008 11:42 am
Post subject: Re: select all columns that are not null [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I know it's a poor design. I know the data should be going down and
not accross. I didn't design or populate this table.




On May 14, 1:28 pm, Alex Kuznetsov wrote:
> On May 14, 7:46 am, Billy wrote:
>
> >.but this will change each
> > month so I don't want to hard code the column names.
>
> This indicates a poor database design. Can you describe your problem
> so that we can design a better table for your needs?
 >> Stay informed about: select all columns that are not null 
Back to top
Login to vote
dave ballantyne

External


Since: Feb 04, 2008
Posts: 101



(Msg. 7) Posted: Wed May 14, 2008 2:00 pm
Post subject: Re: select all columns that are not null [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Billy wrote:
> Is there a way to add a where clause to this statement
>
> SELECT * FROM tableName
>
> so that you only return columns that don't have a null in each row?
> I don't want blank columns returned......but this will change each
> month so I don't want to hard code the column names.



Sounds as if you have made a fundemental error in your understanding of
relational databases.

It sounds as if you table is


PKey Int,
Month1 int,
Month2 int,
Month3 int,
Month4 int,
.....
....
...


It Should be

PKey Int
MonthNum Int
MonthValue int


And then populated with rows not columns.

Dave
 >> Stay informed about: select all columns that are not null 
Back to top
Login to vote
dave ballantyne

External


Since: Feb 04, 2008
Posts: 101



(Msg. 8) Posted: Wed May 14, 2008 3:13 pm
Post subject: Re: select all columns that are not null [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Billy wrote:
> Yes I am working with data that is not normalized. That doesn't mean
> I don't realize it. I have to work with the data the way it is.
>
> On May 14, 8:00 am, dave ballantyne
> wrote:
>> Billy wrote:
>>> Is there a way to add a where clause to this statement
>>> SELECT * FROM tableName
>>> so that you only return columns that don't have a null in each row?
>>> I don't want blank columns returned......but this will change each
>>> month so I don't want to hard code the column names.
>> Sounds as if you have made a fundemental error in your understanding of
>> relational databases.
>>
>> It sounds as if you table is
>>
>> PKey Int,
>> Month1 int,
>> Month2 int,
>> Month3 int,
>> Month4 int,
>> ....
>> ...
>> ..
>>
>> It Should be
>>
>> PKey Int
>> MonthNum Int
>> MonthValue int
>>
>> And then populated with rows not columns.
>>
>> Dave
>



If i understand you right, this should help. Ive done it as a two pass
operation to avoid multiple scans on the source table, that may or may
not bother you.

Dave


Create Table xxtestt1
(
col1 int null,
col2 int null,
col3 int null,
col4 int null)

insert into xxtestt1 values(Null,1,Null,NULL)
go


drop table xxskeltab
go

declare @colname varchar(20)
declare @SqlString nvarchar(2048)


declare tcursor cursor for
select name from syscolumns
where id = object_id('xxtestt1')
open tcursor
while(0=0) begin
fetch next from tcursor into @Colname

if(@@Fetch_status<>0) break

if @SqlString is null begin
Select @SqlString = 'SELECT '
end else begin
Select @SqlString = @SqlString+','
end
Select @SqlString = @SqlString +@ColName+'= max(case when
'+@Colname+' is not null then 1 else 0 end) '
end
close tcursor
deallocate tcursor

Select @SqlString = @SqlString +' into xxskeltab From xxtestt1'
select @SqlString
EXECUTE sp_executesql @SQLString;
go

Declare @SqlString nvarchar(2048)
declare @FullSql nvarchar(2048)
declare @Colname varchar(20)
declare @Colret integer

declare tcursor cursor for
select name from syscolumns
where id = object_id('xxskeltab')
open tcursor
while(0=0) begin
fetch next from tcursor into @Colname

if(@@Fetch_Status<>0) break
Select @SqlString = 'Select @ColRetout='+@Colname+' from xxskeltab '
exec sp_executesql @SqlString,N'@ColRetout integer
Output',@ColRetout =@ColRet OUTPUT

if(@ColRet=1) begin
if(@Fullsql is null) begin
Select @FullSql = 'Select '
end else begin
Select @FullSql = ','
end
Select @FullSql = @FullSql + @Colname
end
end
close tcursor
deallocate tcursor

select @FullSql = @Fullsql +' from foo'
select @FullSql
 >> Stay informed about: select all columns that are not null 
Back to top
Login to vote
Uri Dimant

External


Since: Aug 24, 2003
Posts: 1744



(Msg. 9) Posted: Wed May 14, 2008 4:06 pm
Post subject: Re: select all columns that are not null [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Billy

not tested

Open the cursor, run

SET @SQL = N'IF EXISTS (SELECT '+ @column +' FROM'') SET
@ALLNULL = ''N'' ELSE set @ALLNULL = ''Y'''
SET @ParamDefinition = N'@column sysname, @ALLNULL CHAR(1) OUTPUT'

EXECUTE sp_executesql
@SQL ,
@ParamDefinition,
@column= @column,
@ALLNULL = @ALLNULL OUTPUT

IF @ALLNULL = 'Y' PRINT @column+ ' has all rows NULL'


"Billy" wrote in message

> Is there a way to add a where clause to this statement
>
> SELECT * FROM tableName
>
> so that you only return columns that don't have a null in each row?
> I don't want blank columns returned......but this will change each
> month so I don't want to hard code the column names.
 >> Stay informed about: select all columns that are not null 
Back to top
Login to vote
dave ballantyne

External


Since: Feb 04, 2008
Posts: 101



(Msg. 10) Posted: Thu May 15, 2008 8:34 am
Post subject: Re: select all columns that are not null [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

billy.rogers.RemoveThis@chasepaymentech.com wrote:
> I don't quite understand how to use this. Where do I put the table
> name?
>
> Here's the table
>
> CREATE TABLE [dbo].[MRecordCount] (
> [Platform] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
> NULL ,
> [TableName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
> NULL ,
> [FDMS] [bit] NOT NULL ,
> [_20070301] [int] NULL ,
> [_20070401] [int] NULL ,
> [_20070501] [int] NULL ,
> [_20070601] [int] NULL ,
> [_20070701] [int] NULL ,
> [_20070801] [int] NULL ,
> [_20070901] [int] NULL ,
> [_20071001] [int] NULL ,
> [_20071101] [int] NULL ,
> [_20071201] [int] NULL ,
> [_20080101] [int] NULL ,
> [_20080201] [int] NULL ,
> [_20080301] [int] NULL ,
> [_20080401] [int] NULL ,
> [_20080501] [int] NULL ,
> [_20080601] [int] NULL ,
> [_20080701] [int] NULL ,
> [_20080801] [int] NULL ,
> [_20080901] [int] NULL
> ) ON [PRIMARY]
> GO

Hi Billy,

replace xxtestt1 and foo in the final select statement that is built
with MRecordCount.


Put just to restate , this is a truly horrible and wrong table design,
adding columns rather than rows is not good practice.

Dave
 >> Stay informed about: select all columns that are not null 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Adding two columns with SELECT and one is NULL - I just found out today that adding a NULL to any value results in a NULL. I have a SELECT statement that looks like this: SELECT WCAmount + WCHours AS Total FROM worktime WHERE WCid = 'WC11111' Now if either WCAmount or WCHours is NULL, then I get...

Null columns take memory? - Hi, Will columns made as null take memory if there is no value in that column? I have a couple of columns of type uniqueidentifier and varchar which can be null. Thanks in advance. Cheers, venkat

Avoid Null columns in Table - Hi Guys How should avoid null columns in a table. Ex I have Table called Test Emp_code Name DOB place dest add1 add2 DOJ 111 yesvanth 18-may-1984 null ..

select rows as columns - Table 1: col1 col2 col3 col4 col 5 1 a b c d result should be like 1 a 1 b 1 c 1 d qyuery pls

How to 'SELECT *' and get all non-computed columns? - Let's say I have a table with a computed column: CREATE MyTable ( Col_1 INT, Col_2 INT, Col_3 AS (Col_1 + Col_2) ) Is there any way of selecting all columns that are NOT computed columns without explicitly identifying each column i...
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]