Skip to content
Advertisement

Set column to a concatenated value from another table

I have 2 tables in SQL Server 2008:

Address:

nameid | e-mail
---------------
1      | xyz@abc.com
2      | fgh@asdf.com
3      | 123@doremi.com

Member:

nameid | memberid
---------------
1      | 456
2      | 457
3      | 458

I need to set e-mail to [memberid]@test.com keeping in mind that e-mail is varchar and memberid is int.

Advertisement

Answer

SELECT A.nameid 
      ,CAST(M.memberid AS NVARCHAR(20)) + 
           RIGHT([e_mail], LEN([e_mail]) - CHARINDEX('@', [e_mail])+1) AS New_Column
FROM [address] A INNER JOIN [member] M
ON A.nameid = M.nameid 

To simply hardcode @test.com with their MemberID is fairly simple

SELECT A.nameid 
      ,CAST(M.memberid AS VARCHAR(20)) + '@test.com' AS New_Column

FROM [address] A INNER JOIN [member] M
ON A.nameid = M.nameid 
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement