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


how to determine the first column with null value


how to determine the first column with null value

Author
Message
MadRazzi
MadRazzi
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 52
Hello

I have a table with 7 columns like Cumulative_cnt, AX1, AX2, AX3, AX4, AX5, POS

i want to find the first AX column with a 'null' and assign that column number to POS as a value

for e.g. if AX3 has the first null value then POS is assigned a value 3.

table is as follows

AX1-AX5 are varchar
all columns after the first column with a Null is Null
all columns preceding the first column with Null has a value
at least one column has a value and is not null which is usually AX1
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25845 Visits: 17509
MadRazzi (2/22/2013)
Hello

I have a table with 7 columns like Cumulative_cnt, AX1, AX2, AX3, AX4, AX5, POS

i want to find the first AX column with a 'null' and assign that column number to POS as a value

for e.g. if AX3 has the first null value then POS is assigned a value 3.

table is as follows

AX1-AX5 are varchar
all columns after the first column with a Null is Null
all columns preceding the first column with Null has a value
at least one column has a value and is not null which is usually AX1


This screams of poor design but the query is pretty simple.


Update YourTable
set POS =
case
when AX1 is not null then 1
when AX2 is not null then 2
when AX3 is not null then 3
when AX4 is not null then 4
when AX5 is not null then 5
end



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
MadRazzi
MadRazzi
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 52
what if ax2 through ax5 is null for a record

wouldn't the case statement assign value 5 to POS?
Lowell
Lowell
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27964 Visits: 39922
MadRazzi (2/22/2013)
what if ax2 through ax5 is null for a record

wouldn't the case statement assign value 5 to POS?


no.
the case statement stops executing as soon as a match is found in the criteria is met.

other items are not evaluated. and they are evaluated in the order you put in the order that the case statement is created.


so it would return 2 in your example where 2-5 are null.

Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

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