连接记录(名称)以创建电子邮件地址

时间:2016-09-02 10:51:16

标签: sql-server

我正在使用SQL server studio管理。以下是我遇到的问题。

表名:Citizen

Firstname       Lastname        Email

John             Smith      jsoi@yahoo.com

Andrew           Evans     evanda@gmail.com

我想更改两个记录的电子邮件地址以采用格式

john.smith@ipno.gov.uk  Andrew.evans@ipno.gov.uk

INTENDED:

Firstname       Lastname        Email

John             Smith      John.Smith@ipno.gov.uk

Andrew           Evans     Andrew.Evans@ipno.gov.uk

所以它将是firstname和lastname的串联,在它们之间加一个点并添加@ ipno.gov.uk

我将非常感谢您对此的帮助或指导。

3 个答案:

答案 0 :(得分:1)

UPDATE Citizens SET Email = LOWER(FirstName) + '.' + LOWER(LastName) + '@ipno.gov.uk' WHERE FirstName IN ('John', 'Andrew') AND LastName IN ('Smith', 'Evans')

答案 1 :(得分:1)

我假设您要更新整个表格。试试这个简单的方法,

下() - >将大写字符转换为小写字母。

UPDATE Citizens
SET Email = Lower(FirstName) + '.' + Lower(LastName) + '@ipno.gov.uk'

答案 2 :(得分:0)

对于SQL Server 2012+,您可以使用CONCAT

CONCAT(LOWER(FirstName), '.', LOWER(LastName), '@ipno.gov.uk')

使用给定数据执行示例:

DECLARE @Citizens TABLE (FirstName VARCHAR (100), LastName VARCHAR (100), Email VARCHAR (100));

INSERT INTO @Citizens (FirstName, LastName, Email) VALUES 
('John', 'Smith', 'jsoi@yahoo.com'),
('Andrew', 'Evans', 'evanda@gmail.com');

-- Only to update the two records need to add the WHERE clause
-- If you want to update for all the entries in the table remove the WHERE clause

UPDATE @Citizens SET Email = CONCAT(LOWER(FirstName), '.', LOWER(LastName), '@ipno.gov.uk')
FROM @Citizens
WHERE FirstName = 'John' AND LastName = 'Smith'

UPDATE @Citizens SET Email = CONCAT(LOWER(FirstName), '.', LOWER(LastName), '@ipno.gov.uk')
FROM @Citizens
WHERE FirstName = 'Andrew' AND LastName = 'Evans'