简体   繁体   中英

What is the equivalent of Conc() in SQL Server

I am translating MS Access queries to SQL Server.

I came across

SELECT 
    [R1EQ].CompanyId, [R1EQ].DateOnFile, 
    R1EQ.service_name, 
    Conc("DSPs", "ISPID", [ISPID], "R1EQ", "DateOnFile", [DateOnFile]) AS ColName

Can someone please tell me what is the equivalent of the conc function in SQL Server?

Is seems you are looking for concat() function :

SELECT [R1EQ].CompanyId, [R1EQ].DateOnFile, R1EQ.service_name, 
       CONCAT('DSPs', 'ISPID', [ISPID], 'R1EQ', 'DateOnFile', DateOnFile) AS ColName
. . . 

However, this would required at least SQL Server 2012+ for earlier you have to use + operator

SELECT [R1EQ].CompanyId, [R1EQ].DateOnFile, R1EQ.service_name, 
       ('DSPs'+'ISPID'+ CAST([ISPID] AS VARCHAR(255) +'R1EQ' + 
        'DateOnFile', CAST(DateOnFile AS VARCHAR(255)
       ) AS ColName
 . . . 

We can try using the concatenation operator in SQL Server:

SELECT
    [R1EQ].CompanyId,
    [R1EQ].DateOnFile,
    R1EQ.service_name,
    "DSPs" + "ISPID" + [ISPID] + "R1EQ" + "DateOnFile" + [DateOnFile] AS ColName
FROM yourTable;

CONC() is not a built-in function in MS Access. It can do anything, but references on the web suggest that it concatenates strings. Note that it might do more, such as inserting delimiter characters.

The equivalent oeprator SQL Server is + , but you need to be very careful. All the arguments need to be strings. So, you will probably need to convert them. The place to start is:

SELECT [R1EQ].CompanyId, [R1EQ].DateOnFile, 
       R1EQ.service_name, 
      ('DSPs' + 'ISPID' + CONVERT(VARCHAR(255), ISPID), +
       'R1EQ' + 'DateOnFile',
       CONVERT(VARCHAR(2555), DateOnFile)  -- you might want a format
      ) AS ColName

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