Hi all,
Got a question about some SQL I'm writing (for SQL Server 2005): how do I select one field from one table, and another field from another table, and have them returned as one 'virtual' field?
I know how to do that when you're talking about two fields from the same table. It would look something like this:
SELECT FirstName + ' ' + LastName AS 'FullName'
FROM person;
That would produce one 'virtual' field called "FullName" that contained the first name, then a space character, and then the last name.
My problem is that I have two fields to combine into one, but the fields are in two different tables. I've got an asset table (called 'Asset') that contains basic details of every PC, laptop, server, monitor, etc., that we have in the business, including the unique identifying number, the Asset ID. Then there's a separate table that stores details specific to the PCs and laptops, including what we call a PC ID, which is just a number that looks something like "PC123", and is used as an easy way to name all of the PCs.
What I need is to select the description from the 'Asset' table, and the PC ID from the 'AssetComputer' table, and have them returned as a single field.
The problem is that
not all assets have a PC ID. Monitors, for example, will have a record in the 'Asset' table (because they're an asset, after all) but because they're not a PC, they won't have a record in 'AssetComputer', and so won't have a PC ID to return. What I need is for
all assets in the 'Asset' table to be included in the results, regardless of whether they have a PC ID, but if they
do have a PC ID, then that is to be returned as well, preferably in this format:
PCID <space> Description
Here's a nice ERD of the tables in question (it's slightly wrong in that it's a one-to-one relationship, not one to many as the diagram shows):

Here's the SQL that I tried:
SELECT
tblAsset.AssetID,
(tblAssetComputer.PCID + ' ' + tblAsset.AssetDescription) AS 'Description'
FROM tblAsset
LEFT OUTER JOIN tblAssetComputer
ON tblAsset.AssetID = tblAssetComputer.AssetID;
And here's what it returned:

As you can see, it returned all the assets, regardless of whether they had an associated PC ID, but if they
didn't have a PC ID, then the 'Description' field is returned as NULL, rather than just the contents of the 'AssetDescription' field like I would have expected.
If there's no PC ID, I'd like just the 'AssetDescription' field to be returned, but if there is a PC ID, I'd like the 'PCID' and the 'AssetDescription' fields to be returned as one 'virtual' field, called 'Description'.
Thanks in advance for any help!
