将列设置为另一个表的连接值

时间:2014-05-20 14:51:07

标签: sql sql-server sql-server-2008 join concatenation

我在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

我需要将电子邮件设置为[memberid]@test.com,并记住电子邮件是varcharmemberidint

感谢。

4 个答案:

答案 0 :(得分:2)

这是你在找什么?

update a
    set email = cast(m.memberid as varchar(255)) + '@test.com'
    from address a join
         member m
         on a.nameid = m.nameid;

答案 1 :(得分:0)

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 

简单地使用其MemberID对@test.com进行硬编码非常简单

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 

答案 2 :(得分:0)

如果我理解你的问题,这就是我认为你正在寻找的:

SELECT CAST(memberid AS VARCHAR(10)) + '@test.com'

答案 3 :(得分:0)

我猜你可以根据评论使用这个......

SELECT CONVERT(VARCHAR(50), memberid) + '@Test.com' FROM member