Concatenate SQL Server

Asked

Viewed 495 times

0

I have the following appointment:

SELECT UPPER(admatribuido)        AS [admAtribuido], 
       Count(admatribuido) AS [quantidade_admAtribuido] 
FROM   ##tmp 
WHERE  Month(dataabertura) = Month(Getdate()) 
       AND Year(dataabertura) = Year(Getdate())
AND admAtribuido <> 'null'
GROUP  BY admatribuido

That brings me the following return:

inserir a descrição da imagem aqui

I would like to concatenate a dash " - " and the quantity_admAtributed in the admtributed, for example:

admAtribuido              |   quantidade_admAtribuido               
ANA PAULA SOARES - 100    |   100 
  • If you are using Sql Server 2016 there is tsql Contat, will look something like: SELECT CONCAT(UPPER(admatribuido), '-' ,Count(admatribuido))...

2 answers

3


SELECT concat( UPPER(admatribuido), ' - ',cast([quantidade_admAtribuido] as varchar(100)))        AS [admAtribuido], 
   Count(admatribuido) AS [quantidade_admAtribuido]

Use the Concat function as in the above example

3

Uses the default concatenation operator of MSSQL, the "+":

1 - With the 2 fields:

SELECT (UPPER(admatribuido) + ' - ' + Count(admatribuido)) AS [admAtribuido], 
       Count(admatribuido) AS [quantidade_admAtribuido] 
FROM   ##tmp 
WHERE  Month(dataabertura) = Month(Getdate()) 
       AND Year(dataabertura) = Year(Getdate())
AND admAtribuido <> 'null'
GROUP  BY admatribuido

2 - With 1 field:

SELECT (UPPER(admatribuido) + ' - ' + Count(admatribuido)) AS [admAtribuido]
FROM   ##tmp 
WHERE  Month(dataabertura) = Month(Getdate()) 
       AND Year(dataabertura) = Year(Getdate())
AND admAtribuido <> 'null'
GROUP  BY admatribuido

This solution applies to these applications below:

SQL Server (as of 2008) Microsoft Azure SQL Database Azure SQL Data Warehouse Parallel Data Warehouse

Browser other questions tagged

You are not signed in. Login or sign up in order to post.