在T-SQL中基于fullName更新电子邮件列?

时间:2016-01-08 12:43:01

标签: sql-server tsql

假设这个表:

用户

public int getAvgARGB(int[] clr1, int[] clr2){
    int[] returnArray = new int[clr1.length];
    for(int i=0; i<clr1.length;i++){
       int a1[i] = (clr1[i] & 0xFF000000) >>> 24;
       int r1[i] = (clr1[i] & 0x00FF0000) >> 16;
       int g1[i] = (clr1[i] & 0x0000FF00) >> 8;
       int b1[i] = (clr1[i] & 0x000000FF) ;

       int a2[i] = (clr2[i] & 0xFF000000) >>> 24;
       int r2[i] = (clr2[i] & 0x00FF0000) >> 16;
       int g2[i] = (clr2[i] & 0x0000FF00) >> 8;
       int b2[i] = (clr2[i] & 0x000000FF) ;

       int aAvg = (a1[i] + a2[i]) / 2;
       int rAvg = (r1[i] + r2[i]) / 2;
       int gAvg = (g1[i] + g2[i]) / 2;
       int bAvg = (b1[i] + b2[i]) / 2;

       int returnArray[i] = (aAvg << 24) + (rAvg << 16) + (gAvg << 8) + bAvg; 

     }
       return returnArray;
}

如何更新---------------------------------------------------------------------- |FullName |Email | |---------------------------------------|----------------------------| |FirstName some MiddleNames LastName |NULL | |Washington L Guedes |NULL | |... |... | ---------------------------------------------------------------------- 列:

Email

提前致谢。

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

查询选择

DECLARE @FullName VARCHAR(MAX) = 'FirstName some MiddleNames LastName '

SELECT SUBSTRING(@FullName, 1, CHARINDEX(' ', @FullName) - 1) + '.' +
       REVERSE(SUBSTRING(REVERSE(@FullName), 1, 
               CHARINDEX(' ', REVERSE(@FullName)) - 1) ) + '@specific.site' AS [Email]

<强>输出

Email
FirstName.LastName@specific.site

查询更新

UPDATE Users
SET Email = SUBSTRING(FullName, 1, CHARINDEX(' ', FullName) - 1) + '.' +
            REVERSE(SUBSTRING(REVERSE(FullName), 1, 
                    CHARINDEX(' ', REVERSE(FullName)) - 1) ) + '@specific.site'

更新#1

根据您的评论删除空格,您必须使用LTRIMRTRIM,并在以下内容中使用小写字母LOWER获取结果:

update #t
set Email = LOWER(SUBSTRING(LTRIM(RTRIM(FullName)), 1, CHARINDEX(' ', LTRIM(RTRIM(FullName))) - 1) + '.' +
            REVERSE(SUBSTRING(REVERSE(LTRIM(RTRIM(FullName))), 1, 
                    CHARINDEX(' ', REVERSE(LTRIM(RTRIM(FullName)))) - 1) ) + '@specific.site')