SQL Server插入修剪和连接的值

时间:2018-03-22 02:18:50

标签: sql sql-server

我正在尝试将来自不同变量的值插入一列,你们可以查看我的代码吗?

  

这是我的SQL Server脚本,我对sql server不是很熟悉,顺便说一下我的mysql版本下面有我的工作脚本。你可以查看一下我想在这里发生的事情

(SELECT
    concat(LTRIM(RTRIM(HomeAdd1)) AS HomeAdd1),
    ' ',
    (LTRIM(RTRIM(HomeAdd2)) AS HomeAdd2),
    ' ',
    (LTRIM(RTRIM(HomeAdd3)) AS HomeAdd3),
    ' ',
    (LTRIM(RTRIM(HomeAdd4)) AS HomeAdd4),
    ' ',
    (LTRIM(RTRIM(HomeAdd5)) AS HomeAdd5) as Residence,
  

这是我正在使用的MySQL代码

concat(trim(AltHome1),' ',trim(AltHome2),' ',trim(AltHome3),' ',trim(AltHome4),' ',trim(AltHome5)) as AltAdd,

1 个答案:

答案 0 :(得分:2)

如果删除表别名并修复某些逻辑,这应该有效:

SELECT (LTRIM(RTRIM(HomeAdd1)) + ' ' +
        LTRIM(RTRIM(HomeAdd2)) + ' ' +
        LTRIM(RTRIM(HomeAdd3)) + ' ' +
        LTRIM(RTRIM(HomeAdd4)) + ' ' +
        LTRIM(RTRIM(HomeAdd5))
       ) as Residence,

作为参考,MySQL代码应该使用concat_ws()

concat_ws(' ', trim(AltHome1), trim(AltHome2), trim(AltHome3), trim(AltHome4), trim(AltHome5)
         ) as AltAdd,