Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
I have a table (table PS) with 20 fields e.g. currentvalue1, currentvalue2 ...etc These fields are decimal(8,2) Null. I want to get the max value i.e. the last currentvalue with a non 0 value/null held. I want this added to my current select statemente.g. select name, age, sex ....etc where id = @idAny ideas
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts
Posted - 2011-06-09 : 07:08:31
Not quite clear to me what you mean by "max value", but one of these?
select name, age, sex, ..., coalesce(currentvalue20,currentvalue19,currentvalue18,...,currentvalue1) from ....
or
select name, age, sex, ..., coalesce(nullif(currentvalue20,0),nullif(currentvalue19,0),nullif(currentvalue18,0),...,nullif(currentvalue1,0)) from ....