简体   繁体   中英

SQL concat value as column

I'm trying to convert one string to a valid column. As you can see I need to get something like 'mo._olddb_uid_'+nu._olddb_name_db that should use the column mo._olddb_uid_001

How can I achieve this:

SELECT *
  FROM [PI_CONSOLIDATION].[dbo].[new_unite] nu
  LEFT JOIN [PI_CONSOLIDATION].[dbo].[Motif_Orientation] mo ON CONCAT('mo._olddb_uid_',nu._olddb_name_db) = CONCAT(nu._olddb_name_db,'_',nu.id_motif_orientation)
   WHERE nu.nom_res = 'TEST' and nu.prenom_res = 'Foobar'

Thanks


EDITED: I have 18 application each with his DB. Those applications are almost similar with different data but sometimes the data can be found on also on the other sources. So [new_unite] has the id of patient, id_group and the source database

id_resident id_groupe_res   _olddb_name_db
728         31              src1
629         21              src6
731         25              src9
934         12              src18
...

The other table has some parameters that have identical params but with different IDs depending on the DB of the source.

So the [Motif_Orientation] looks like : 表

So mainly this query is just only to test if the data is stored correctly on the final application where there is just one DB and all the data merged

id_motif_orientation    label
1407                    Famille
1410                    Structures d'hébergement
1422                    Etablissement d'Education Spéciale

What you could try to do is make a function with the output being the value you want to left join. Your output should be a single value doesn't matter the type. Then, call it as:

LEFT JOIN 
    (func_name(param1,param2,...) AS CONCAT(...) FROM DUAL)
ON 1=1

The result should be your value left joined to the current table under whatever column name you need. The only problem is that you will need to do this one by one for each row, so it is only really useful for smaller tables. Wish you gave more info so I could give a better answer but this worked for me when I was having a similar issue with renaming a column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM