SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How can I find the first column having NULL values in a table


How can I find the first column having NULL values in a table

Author
Message
only4mithunc
only4mithunc
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1804 Visits: 2803
Hi Friends,

I have a file reader function that will read the file and load the data into a temp table. This function is designed to read 100 columns, but in some cases I will have only 30 columns data so that the remaining columns will be having value as <unknown>.

Later this table will be called through another SP/Function (for some functionality), so if I can get to know the depth (the first column in the table which is having this <unknown> value, then I can pass that position (depth) to my SP so that I don’t have to execute my SP/Function for these columns where there is no data at all.

So is there any way to know the first column of table which has the NULL or (in my case <Unknown>Wink value?

Thanks & Regards,
MC
dwain.c
dwain.c
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18421 Visits: 6431
This may help.


DECLARE @T TABLE
(Col1 INT, Col2 INT, Col3 INT, Col4 INT
,Col5 INT, Col6 INT, Col7 INT, Col8 INT
,Col9 INT, Col10 INT, Col11 INT, Col12 INT)

INSERT INTO @T (Col1, Col2, Col3, Col4)
SELECT 1, 2, 3, 4
UNION ALL SELECT 5, 6, 7, 8

SELECT * FROM @T

SELECT TOP 1 n
FROM (SELECT TOP 1 * FROM @T) a
CROSS APPLY (
VALUES (1, Col1), (2, Col2)
,(3, Col3), (4, Col4)
,(5, Col5), (6, Col6)
,(7, Col7), (8, Col8)
,(9, Col9), (10, Col10)
,(11, Col11), (12, Col12)) b(n, Col)
WHERE Col IS NULL
ORDER BY n




The number returned is the first column containing a NULL in the first row of the table.

This is basically an UNPIVOT that is using the technique described here: http://www.sqlservercentral.com/articles/CROSS+APPLY+VALUES+UNPIVOT/91234/

Somewhere in that article I believe it warns you that you need to CAST each column to the same (compatible) type if there are differences.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
only4mithunc
only4mithunc
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1804 Visits: 2803
Thanks dwain.c,

I'm getting the answer with that code. I'm just trying to see if I can rewrite this some way so that I don't have to hard code my 90+ columns ( agreed it is one time activity but still )

Thanks & Regards,
MC
dwain.c
dwain.c
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18421 Visits: 6431
only4mithunc (9/30/2012)
Thanks dwain.c,

I'm getting the answer with that code. I'm just trying to see if I can rewrite this some way so that I don't have to hard code my 90+ columns ( agreed it is one time activity but still )



I don't see any way around hardcoding the 90+ columns. Sorry.

I wouldn't like that any more than you. :-)


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
vikingDBA
vikingDBA
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1284 Visits: 929
Try this little script I wrote to get the column names of a table in comma-delimited form. Code can be changed to do other formats. Check the Messages tab on the results after run for the text version. This has come in handy SOOOOOOO many times.


USE <dbname>
GO
-- Simple column listing with brackets
DECLARE @tablename varchar(100)
DECLARE @colline varchar(4000)

SET @tablename = '<TableName>' -- set to the table name you are interested in
SET @colline = '' -- leave blank

SELECT @colline=@colline + COLUMN_NAME + ',' FROM information_schema.columns
WHERE TABLE_NAME = @tablename
ORDER BY ORDINAL_POSITION

SELECT SUBSTRING(@colline,1,len(@colline)-1)
PRINT SUBSTRING(@colline,1,len(@colline)-1)
ScottPletcher
ScottPletcher
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20342 Visits: 7427
In this case ;-), I think a simple CASE will do exactly the same thing:


SELECT TOP 1
CASE WHEN Col1 IS NULL THEN 1 WHEN Col2 IS NULL THEN 2 WHEN Col3 IS NULL THEN 3 WHEN Col4 IS NULL THEN 4
WHEN Col5 IS NULL THEN 5 WHEN Col6 IS NULL THEN 6 WHEN Col7 IS NULL THEN 7 WHEN Col8 IS NULL THEN 8
WHEN Col9 IS NULL THEN 9 WHEN Col10 IS NULL THEN 10 WHEN Col11 IS NULL THEN 11 WHEN Col12 IS NULL THEN 12 END
FROM @T



SQL DBA,SQL Server MVP(07, 08, 09) Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial:
If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search