Showing posts with label homephone. Show all posts
Showing posts with label homephone. Show all posts

Sunday, February 19, 2012

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 test@.test.com
i have to select values as follows.
Id DeviceType DeviceInfo
20 HomePhone, Mobile, Email 2323223, 323232232, test@.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
Use the same thinking as was suggested in my earlier reply:
SELECT
No
,'HomePhone, ' + CASE WHEN WorkPhone IS NOT NULL THEN 'WorkPhone' + ', ' ELSE '' END + 'Mobile' AS
DeviceType
,HomePhone + ', ' + COALESCE(WorkPhone + ', ', '') + Mobile
FROM
(
SELECT 20 AS No, '23232' AS HomePhone, NULL AS WorkPhone, '98327' AS Mobile
) AS x
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
"Oonz" <arundhaj@.gmail.com> wrote in message
news:1179848438.242226.4580@.n15g2000prd.googlegrou ps.com...
> 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@.test.com
> i have to select values as follows.
> Id DeviceType DeviceInfo
> 20 HomePhone, Mobile, Email 2323223, 323232232, test@.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
>

constructing strings from table

Hi friends,
i have to select values from the table consider with the following
record:
No HomePhone WorkPhone Mobile
20 23232 98327
the selection have to be done as:
No DeviceType DeviceInfo
20 HomePhone, Mobile 23232, 98327
since there is no value for workphone it have to be removed in both
devicetype and deviceinfo column.
Thanks
Arunkumar.D
No DDL = not tested...
Something like (assuming NULL when you say "no value"):
SELECT
No
,'HomePhone, ' + CASE WHEN WorkPhone IS NULL THEN 'WorkPhone' + ', ' ELSE '' END + 'Mobile' AS
DeviceType
,HomePhone + ', ' + COALESCE(WorkPhone + ', ', '') + Mobile
FROM ...
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
"Oonz" <arundhaj@.gmail.com> wrote in message
news:1179849918.549063.61900@.n15g2000prd.googlegro ups.com...
> Hi friends,
> i have to select values from the table consider with the following
> record:
> No HomePhone WorkPhone Mobile
> 20 23232 98327
> the selection have to be done as:
> No DeviceType DeviceInfo
> 20 HomePhone, Mobile 23232, 98327
> since there is no value for workphone it have to be removed in both
> devicetype and deviceinfo column.
> Thanks
> Arunkumar.D
>
|||> i have to select values from the table consider with the following
> record:
> No HomePhone WorkPhone Mobile
> 20 23232 98327
> the selection have to be done as:
> No DeviceType DeviceInfo
> 20 HomePhone, Mobile 23232, 98327
What about something like:
SELECT No,
CASE WHEN HomePhone IS NULL THEN '' ELSE 'HomePhone' END +
CASE WHEN WorkPhone IS NULL THEN '' ELSE 'WorkPhone' END +
CASE WHEN Mobile IS NULL THEN '' ELSE 'Mobile' END
AS DeviceType,
CASE WHEN HomePhone IS NULL THEN '' ELSE HomePhone END +
CASE WHEN WorkPhone IS NULL THEN '' ELSE WorkPhone END +
CASE WHEN Mobile IS NULL THEN '' ELSE Mobile END
AS DeviceInfo
FROM TableName
(You will need to add a bit of logic to work out where to put the commas)
Rgds,
Gavin

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 test@.test.com
i have to select values as follows.
Id DeviceType DeviceInfo
20 HomePhone, Mobile, Email 2323223, 323232232, test@.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.DUse the same thinking as was suggested in my earlier reply:
SELECT
No
,'HomePhone, ' + CASE WHEN WorkPhone IS NOT NULL THEN 'WorkPhone' + ', ' ELSE '' END + 'Mobile' AS
DeviceType
,HomePhone + ', ' + COALESCE(WorkPhone + ', ', '') + Mobile
FROM
(
SELECT 20 AS No, '23232' AS HomePhone, NULL AS WorkPhone, '98327' AS Mobile
) AS x
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
"Oonz" <arundhaj@.gmail.com> wrote in message
news:1179848438.242226.4580@.n15g2000prd.googlegroups.com...
> 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@.test.com
> i have to select values as follows.
> Id DeviceType DeviceInfo
> 20 HomePhone, Mobile, Email 2323223, 323232232, test@.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
>

constructing strings from table

Hi friends,
i have to select values from the table consider with the following
record:
No HomePhone WorkPhone Mobile
20 23232 98327
the selection have to be done as:
No DeviceType DeviceInfo
20 HomePhone, Mobile 23232, 98327
since there is no value for workphone it have to be removed in both
devicetype and deviceinfo column.
Thanks
Arunkumar.DNo DDL = not tested...
Something like (assuming NULL when you say "no value"):
SELECT
No
,'HomePhone, ' + CASE WHEN WorkPhone IS NULL THEN 'WorkPhone' + ', ' ELSE '' END + 'Mobile' AS
DeviceType
,HomePhone + ', ' + COALESCE(WorkPhone + ', ', '') + Mobile
FROM ...
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
"Oonz" <arundhaj@.gmail.com> wrote in message
news:1179849918.549063.61900@.n15g2000prd.googlegroups.com...
> Hi friends,
> i have to select values from the table consider with the following
> record:
> No HomePhone WorkPhone Mobile
> 20 23232 98327
> the selection have to be done as:
> No DeviceType DeviceInfo
> 20 HomePhone, Mobile 23232, 98327
> since there is no value for workphone it have to be removed in both
> devicetype and deviceinfo column.
> Thanks
> Arunkumar.D
>|||> i have to select values from the table consider with the following
> record:
> No HomePhone WorkPhone Mobile
> 20 23232 98327
> the selection have to be done as:
> No DeviceType DeviceInfo
> 20 HomePhone, Mobile 23232, 98327
What about something like:
SELECT No,
CASE WHEN HomePhone IS NULL THEN '' ELSE 'HomePhone' END +
CASE WHEN WorkPhone IS NULL THEN '' ELSE 'WorkPhone' END +
CASE WHEN Mobile IS NULL THEN '' ELSE 'Mobile' END
AS DeviceType,
CASE WHEN HomePhone IS NULL THEN '' ELSE HomePhone END +
CASE WHEN WorkPhone IS NULL THEN '' ELSE WorkPhone END +
CASE WHEN Mobile IS NULL THEN '' ELSE Mobile END
AS DeviceInfo
FROM TableName
(You will need to add a bit of logic to work out where to put the commas)
Rgds,
Gavin

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 test@.test.com
i have to select values as follows.
Id DeviceType DeviceInfo
20 HomePhone, Mobile, Email 2323223, 323232232, test@.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.DUse the same thinking as was suggested in my earlier reply:
SELECT
No
,'HomePhone, ' + CASE WHEN WorkPhone IS NOT NULL THEN 'WorkPhone' + ', ' ELS
E '' END + 'Mobile' AS
DeviceType
,HomePhone + ', ' + COALESCE(WorkPhone + ', ', '') + Mobile
FROM
(
SELECT 20 AS No, '23232' AS HomePhone, NULL AS WorkPhone, '98327' AS Mobile
) AS x
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
"Oonz" <arundhaj@.gmail.com> wrote in message
news:1179848438.242226.4580@.n15g2000prd.googlegroups.com...
> 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@.test.com
> i have to select values as follows.
> Id DeviceType DeviceInfo
> 20 HomePhone, Mobile, Email 2323223, 323232232, test@.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
>

constructing strings from table

Hi friends,
i have to select values from the table consider with the following
record:
No HomePhone WorkPhone Mobile
20 23232 98327
the selection have to be done as:
No DeviceType DeviceInfo
20 HomePhone, Mobile 23232, 98327
since there is no value for workphone it have to be removed in both
devicetype and deviceinfo column.
Thanks
Arunkumar.DNo DDL = not tested...
Something like (assuming NULL when you say "no value"):
SELECT
No
,'HomePhone, ' + CASE WHEN WorkPhone IS NULL THEN 'WorkPhone' + ', ' ELSE ''
END + 'Mobile' AS
DeviceType
,HomePhone + ', ' + COALESCE(WorkPhone + ', ', '') + Mobile
FROM ...
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
"Oonz" <arundhaj@.gmail.com> wrote in message
news:1179849918.549063.61900@.n15g2000prd.googlegroups.com...
> Hi friends,
> i have to select values from the table consider with the following
> record:
> No HomePhone WorkPhone Mobile
> 20 23232 98327
> the selection have to be done as:
> No DeviceType DeviceInfo
> 20 HomePhone, Mobile 23232, 98327
> since there is no value for workphone it have to be removed in both
> devicetype and deviceinfo column.
> Thanks
> Arunkumar.D
>|||> i have to select values from the table consider with the following
> record:
> No HomePhone WorkPhone Mobile
> 20 23232 98327
> the selection have to be done as:
> No DeviceType DeviceInfo
> 20 HomePhone, Mobile 23232, 98327
What about something like:
SELECT No,
CASE WHEN HomePhone IS NULL THEN '' ELSE 'HomePhone' END +
CASE WHEN WorkPhone IS NULL THEN '' ELSE 'WorkPhone' END +
CASE WHEN Mobile IS NULL THEN '' ELSE 'Mobile' END
AS DeviceType,
CASE WHEN HomePhone IS NULL THEN '' ELSE HomePhone END +
CASE WHEN WorkPhone IS NULL THEN '' ELSE WorkPhone END +
CASE WHEN Mobile IS NULL THEN '' ELSE Mobile END
AS DeviceInfo
FROM TableName
(You will need to add a bit of logic to work out where to put the commas)
Rgds,
Gavin

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 test@.test.com
i have to select values as follows.

Id DeviceType DeviceInfo
20 HomePhone, Mobile, Email 2323223, 323232232, test@.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.DOonz (arundhaj@.gmail.com) writes:

Quote:

Originally Posted by

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@.test.com
>
i have to select values as follows.
>
Id DeviceType DeviceInfo
20 HomePhone, Mobile, Email 2323223, 323232232, test@.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,test@.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, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||On May 23, 2:48 am, Erland Sommarskog <esq...@.sommarskog.sewrote:

Quote:

Originally Posted by

Oonz (arund...@.gmail.com) writes:

Quote:

Originally Posted by

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


>

Quote:

Originally Posted by

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


>

Quote:

Originally Posted by

i have to select values as follows.


>

Quote:

Originally Posted by

Id DeviceType DeviceInfo
20 HomePhone, Mobile, Email 2323223, 323232232, t...@.test.com


>

Quote:

Originally Posted by

the one solution is:
select
'HomePhone, Mobile, Email' AS DeviceType
HomePhone + ',' + WorkPhone + ',' + MobilePhone + ',' + Email AS
DeviceInfo
from table where Id = 20


>

Quote:

Originally Posted by

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.|||Oonz (arundhaj@.gmail.com) writes:

Quote:

Originally Posted by

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, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx