Groups | Blog | Home
all groups > sql server programming > august 2007 >

sql server programming : working on concatinated columns??


Dinu
8/6/2007 10:44:47 PM
Hi

I have a table 'contacts' that contains following columns:

fistname
middlename
lastname
contactno

i wrote following query which is not accepted
SELECT (firstname + ' ' + middlename + ' ' + lastname) AS Name,
contactno AS Mobile FROM Contacts WHERE Name LIKE 'A%' ORDER BY Name

It gave Invalid column 'Name' exception

can any one suggest me is there any another way to do this.

Thanks
Maurice Telkamp
8/7/2007 12:00:00 AM
Also take a look in BOL at CONCAT_NULL_YIELDS_NULL and ISNULL() or
COALESCE().

Maurice Telkamp.


[quoted text, click to view]

Tom Cooper
8/7/2007 2:31:46 AM
SELECT Name, Mobile
From (SELECT (firstname + ' ' + middlename + ' ' + lastname) AS Name,
contactno AS Mobile FROM Contacts) AS x
WHERE Name LIKE 'A%'
ORDER BY Name

will work, however, the following query would return the same rows and may
be more efficient because it may be more likely to use an index on
firstname.

SELECT Name, Mobile
From (SELECT (firstname + ' ' + middlename + ' ' + lastname) AS Name,
contactno AS Mobile FROM Contacts
WHERE firstname LIKE 'A%') AS x
ORDER BY Name

Tom

[quoted text, click to view]

Tom Cooper
8/7/2007 3:09:28 AM
Oops!.

SELECT Name, Mobile
From (SELECT (firstname + ' ' + middlename + ' ' + lastname) AS Name,
contactno AS Mobile FROM Contacts
WHERE firstname LIKE 'A%') AS x
ORDER BY Name

is the same as long as neither middlename nor lastname can contain NULL. If
either of those columns can contain NULL, then you should not use that
version.

Tom

[quoted text, click to view]

Jack Vamvas
8/7/2007 7:01:03 AM
1)Assuming the first name is not null You coulkd do -- WHERE firstName Like
'A%'

--

Jack Vamvas
___________________________________
Need an IT job? http://www.ITjobfeed.com




[quoted text, click to view]

TheSQLGuru
8/7/2007 12:04:13 PM
replace Name with actual concat in where clause:

SELECT (firstname + ' ' + middlename + ' ' + lastname) AS Name,
contactno AS Mobile FROM Contacts
WHERE (firstname + ' ' + middlename + ' ' + lastname) LIKE 'A%'
ORDER BY Name


--
TheSQLGuru
President
Indicium Resources, Inc.

[quoted text, click to view]

AddThis Social Bookmark Button