Search code examples
sqlsql-serverconcatenationmultiple-columns

How to create a new column in table that is the concatenation of others


Used the following but to no avail (How do I create a column that is a concatenation of two other columns in mariadb?)

The task appears to be simple but it is eluding me. I have six columns that I need to be concatenated into one column

censusstate char(2)
censuscounty  char(3)
censustrac   char(6)
censusblocgroup char(1)
censusbloccode char(2)
censusbloccode2 char(1) 

end result   should be censusbloc char(15)

tried the following code but get errors

   alter table dbo.equi2022t
   add censusbloc char(15) AS 
 (Concat(CensusState,CensusCounty,Censustrac,Censusblocgroup,Censusbloccode,Censusbloccode2));


 Msg 156, Level 15, State 1, Line 364
 Incorrect syntax near the keyword 'AS'.

Solution

  • SQL server support computed columns, but need some special syntax

      alter table dbo.equi2022t
       add censusbloc   
       AS (CensusState + CensusCounty + Censustrac + Censusblocgroup + Censusbloccode + Censusbloccode2)
     ;