CASE WHEN PHONE = NULL then ID ELSE PHONE END

CASE WHEN PHONE = NULL then ID ELSE PHONE END)

what is the correct way to replce a null field with another field such as the ID column...


Thanks


Ray
LVL 7
5thcavAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh RetnakaranDatabase AdministratorCommented:
Hi 5thcav,

case when phone is null then [ID] else  PHONE

or

@phoneNo = ISNULL(Phone, ID)

or

@phoneNo = COALESCE(Phone,ID)


Cheers!

0
Aneesh RetnakaranDatabase AdministratorCommented:
ok, the second and the third won't work in this situation for you
0
dmitryz6Commented:
   CASE PHONE WHEN NULL THEN
          ID
     ELSE
          PHONE
     END
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Aneesh RetnakaranDatabase AdministratorCommented:
Sorry for my above post(its a wrong post ), you can use any of these based on the requirement.
> ok, the second and the third won't work in this situation for you
0
5thcavAuthor Commented:
Line 2: Incorrect syntax near ')'.

SELECT ( case when phone is null then [ID] else PHONE), sitename, SUM([1]), SUM([2]),SUM([3]),

Line 2: Incorrect syntax near ','.
SELECT case when phone is null then [ID] else PHONE,


:(


0
5thcavAuthor Commented:

Column '##temptable1.ID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

INSERT INTO ##temptable2
SELECT (case phone when null then ID else PHONE end), sitename, SUM([1]), SUM([2]),SUM([3]),SUM([4]),SUM([5]),SUM([6]),SUM([7]),SUM([8]),SUM([9]),SUM([10]),SUM([11]),SUM([12])
FROM       ##temptable1
GROUP BY PHONE, sitename
0
fruhjCommented:
Hi 5thcav,
> v            03/21/2006 08:07PM
>  Points: 500
>  Title: CASE WHEN PHONE = NULL then ID ELSE PHONE END
> ===================================================================
> CASE WHEN PHONE = NULL then ID ELSE PHONE END)

> what is the correct way to replce a null field with another field such
> as the ID column...


> Thanks


> Ray

> ===================================================================
>  Comment from aneeshattingal            03/21/2006 08:09PM
> ===================================================================
> Hi 5thcav,

> case when phone is null then [ID] else  PHONE

> or

> @phoneNo = ISNULL(Phone, ID)

> or

> @phoneNo = COALESCE(Phone,ID)


> Cheers!


> ===================================================================



try

SELECT ( case when phone is null then [ID] else PHONE) AS MYFIELD, sitename, SUM([1]), SUM([2]),SUM([3]), ***NOTE YOU"LL NEED ONE MORE FIELD HERE OR KILL THE LAST COMMA!****

not looking for points here, just trying to help

- Jack
0
fruhjCommented:
5thcav,
 our last post crossed.

 you cannot do a groupby like that.

  try

SELECT (case phone when null then ID else PHONE end) AS MYFIELD, sitename, SUM([1]), SUM([2]),SUM([3]),SUM([4]),SUM([5]),SUM([6]),SUM([7]),SUM([8]),SUM([9]),SUM([10]),SUM([11]),SUM([12])
FROM       ##temptable1
GROUP BY MYFIELD, sitename
0
5thcavAuthor Commented:
not working,,, even as phone or when i name the table in ##temptable2 to myfield
Server: Msg 207, Level 16, State 3, Line 1
Invalid column name 'MYFIELD'.
Server: Msg 207, Level 16, State 1, Line 1
Invalid column name 'MYFIELD'.
Server: Msg 207, Level 16, State 1, Line 1

when i use AS PHONE it goes back to Column '##temptable1.ID' is invalid in the select list

phone is nvarchar(11) and ID is int 4,, could this be it?


0
Aneesh RetnakaranDatabase AdministratorCommented:
5thcav,
> Line 2: Incorrect syntax near ')'.

> SELECT ( case when phone is null then [ID] else PHONE), sitename,
> SUM([1]), SUM([2]),SUM([3]),

> Line 2: Incorrect syntax near ','.
> SELECT case when phone is null then [ID] else PHONE,


I forgot to put 'END' for the case statement

SELECT ( case when phone is null then [ID] else PHONE END), sitename,
0
fruhjCommented:
just shooting from the hip (again not looking to take points from aneesh, just trying to help)

What about Select (case when phone is null then [ID] as MYFIELD Else PHONE AS MYFIELD end), ....
0
dmitryz6Commented:
INSERT INTO ##temptable2
SELECT (case phone when null then ID else PHONE end), sitename, SUM([1]), SUM([2]),SUM([3]),SUM([4]),SUM([5]),SUM([6]),SUM([7]),SUM([8]),SUM([9]),SUM([10]),SUM([11]),SUM([12])
FROM       ##temptable1
GROUP BY (case phone when null then ID else PHONE end), sitename
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fruhjCommented:
Good thought Dmitryz
0
dmitryz6Commented:
fruhj.

Thanks,hope it will work.
0
5thcavAuthor Commented:

dmitryz6 wins the grand prize!!!

INSERT INTO ##temptable2
SELECT (case phone when null then CAST(ID as nvarchar) else PHONE end), sitename, SUM([1]), SUM([2]),SUM([3]),SUM([4]),SUM([5]),SUM([6]),SUM([7]),SUM([8]),SUM([9]),SUM([10]),SUM([11]),SUM([12])
FROM       ##temptable1
GROUP BY (case phone when null then CAST(ID as nvarchar) else PHONE end), sitename
0
5thcavAuthor Commented:
thanks for the help!

ya gota love this EE :)
0
dmitryz6Commented:
You welcome and good luck
0
5thcavAuthor Commented:
:0(

it ran but the phone is stull NULL,,, what a bummer
0
dmitryz6Commented:
may be phone zero length string as well

case when phone is null  then ID
                   when datalength(trim(phone)) = 0 then ID
                   else PHONE end
0
5thcavAuthor Commented:
SELECT  ISNULL(LEFT(PHONE, 11),CAST(ID as nvarchar)) AS Phone


fixed it.... :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.