![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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... |
#3
| |||
| |||
|
|
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 - |
#4
| |||
| |||
|
|
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. |
![]() |
| Thread Tools | |
| Display Modes | |
| |