Search code examples
sqlsql-serversql-server-2008-r2concatenationnvarchar

Concatenating two Float columns and insert them to another Float column in SQL Server 2008 R2


I am dealing with a situation as mentioned in question:

I got two Float columns which I want to concatenate like so:

(Column1 + '_' + Column2)

and insert them into column3.

I think the correct query would be something like this:

Update Table as A
set A.Column3 = select ((Column1 + '_' + Column2),ID ) as B
where A.ID = B.ID 

Thank you for your help in advance


Solution

  • If you want to make sure to avoid getting NULLs, you'd need to use something like this:

    UPDATE dbo.YourTable
    SET Column3 = CONVERT(NVARCHAR(50), ISNULL(Column1, N'')) + N'_' + 
                  CONVERT(NVARCHAR(50), ISNULL(Column2, N''))
    

    assuming all three columns are in the same table. Otherwise you'd need to use a JOIN in your UPDATE

    Update: since those are FLOAT column, they need to be converted to NVARCHAR using the CONVERT function