dbTalk Databases Forums  

constructing strings from table

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss constructing strings from table in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Oonz
 
Posts: n/a

Default constructing strings from table - 05-22-2007 , 10:36 AM






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

the record is as follows:
Id HomePhone WorkPhone Mobile Email
20 2323223 323232232 test (AT) test (DOT) com

i have to select values as follows.

Id DeviceType DeviceInfo
20 HomePhone, Mobile, Email 2323223, 323232232, test (AT) test (DOT) 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


Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: constructing strings from table - 05-22-2007 , 04:48 PM






Oonz (arundhaj (AT) gmail (DOT) com) writes:
Quote:
Hi friends,
please help me in selecting values from the table

the record is as follows:
Id HomePhone WorkPhone Mobile Email
20 2323223 323232232 test (AT) test (DOT) com

i have to select values as follows.

Id DeviceType DeviceInfo
20 HomePhone, Mobile, Email 2323223, 323232232, test (AT) test (DOT) 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,test (AT) test (DOT) 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, esquel (AT) sommarskog (DOT) 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


Reply With Quote
  #3  
Old   
Oonz
 
Posts: n/a

Default Re: constructing strings from table - 05-22-2007 , 10:07 PM



On May 23, 2:48 am, Erland Sommarskog <esq... (AT) sommarskog (DOT) se> wrote:
Quote:
Oonz (arund... (AT) gmail (DOT) 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... (AT) test (DOT) com

i have to select values as follows.

Id DeviceType DeviceInfo
20 HomePhone, Mobile, Email 2323223, 323232232, t... (AT) test (DOT) 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... (AT) test (DOT) 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... (AT) sommarskog (DOT) 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... (AT) test (DOT) com

please help me in this regards.



Reply With Quote
  #4  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: constructing strings from table - 05-23-2007 , 02:35 AM



Oonz (arundhaj (AT) gmail (DOT) com) writes:
Quote:
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... (AT) test (DOT) 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, esquel (AT) sommarskog (DOT) 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


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.