471,071 Members | 3,803 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,071 software developers and data experts.

constructing strings from table

Hi friends,
please help me in selecting values from the table

the record is as follows:
Id HomePhone WorkPhone Mobile Email
20 2323223 323232232 te**@test.com

i have to select values as follows.

Id DeviceType DeviceInfo
20 HomePhone, Mobile, Email 2323223, 323232232, te**@test.com
the one solution is:
select
'HomePhone, Mobile, Email' AS DeviceType
HomePhone + ',' + WorkPhone + ',' + MobilePhone + ',' + Email AS
DeviceInfo
from table where Id = 20

but here the work phone number is not available so that information
has to be truncated...

Thanks in Advance
Arunkumar.D

May 22 '07 #1
3 1281
Oonz (ar******@gmail.com) writes:
Hi friends,
please help me in selecting values from the table

the record is as follows:
Id HomePhone WorkPhone Mobile Email
20 2323223 323232232 te**@test.com

i have to select values as follows.

Id DeviceType DeviceInfo
20 HomePhone, Mobile, Email 2323223, 323232232, te**@test.com
the one solution is:
select
'HomePhone, Mobile, Email' AS DeviceType
HomePhone + ',' + WorkPhone + ',' + MobilePhone + ',' + Email AS
DeviceInfo
from table where Id = 20

but here the work phone number is not available so that information
has to be truncated...
Provided that WorkPhone is blank, the query above make sense to me.
The output would be "2323223,,323232232,te**@test.com". But if the
double comma wasn't there, how would you know which number that is
missing?

If WorkPhone is NULL, the entire expression will be NULL. In this
case you must use coalesce(WorkPhone, '') and similar for the other
columns.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
May 22 '07 #2
On May 23, 2:48 am, Erland Sommarskog <esq...@sommarskog.sewrote:
Oonz (arund...@gmail.com) writes:
Hi friends,
please help me in selecting values from the table
the record is as follows:
Id HomePhone WorkPhone Mobile Email
20 2323223 323232232 t...@test.com
i have to select values as follows.
Id DeviceType DeviceInfo
20 HomePhone, Mobile, Email 2323223, 323232232, t...@test.com
the one solution is:
select
'HomePhone, Mobile, Email' AS DeviceType
HomePhone + ',' + WorkPhone + ',' + MobilePhone + ',' + Email AS
DeviceInfo
from table where Id = 20
but here the work phone number is not available so that information
has to be truncated...

Provided that WorkPhone is blank, the query above make sense to me.
The output would be "2323223,,323232232,t...@test.com". But if the
double comma wasn't there, how would you know which number that is
missing?

If WorkPhone is NULL, the entire expression will be NULL. In this
case you must use coalesce(WorkPhone, '') and similar for the other
columns.

--
Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se

Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books...
Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx- Hide quoted text -

- Show quoted text -

The solution i have given is a static one.
The above solution holds good when all the column have value in it.
but when one column, say "HomePhone" is not available the resulting
table should build as
Id DeviceType DeviceInfo
20 Mobile, Email 323232232, t...@test.com

please help me in this regards.

May 23 '07 #3
Oonz (ar******@gmail.com) writes:
The solution i have given is a static one.
The above solution holds good when all the column have value in it.
but when one column, say "HomePhone" is not available the resulting
table should build as
Id DeviceType DeviceInfo
20 Mobile, Email 323232232, t...@test.com

please help me in this regards.
Use the CASE exprssion:

SELECT DeviceType = CASE WHEN HomePhone IS NOT NULL
THEN HomePhone + ','
ELSE ''
END +
CASE WHEN WorkPhone IS NOT NULL
THEN WorkdPhone + ','
ELSE ''
END ...

It gets a little devilish if the email is missing as you may end with a
trailing comma, but maybe you can live wity that.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
May 23 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by karthick | last post: by
7 posts views Thread by Roman Mashak | last post: by
5 posts views Thread by Andrew | last post: by
1 post views Thread by stewart | last post: by
25 posts views Thread by Rainmaker | last post: by
16 posts views Thread by Dave | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.