Questions tagged [string-agg]

Concatenates the values of string expressions and places separator values between them. The separator is not added at the end of string.

Applies to

SQL Server 2017 (14.x) and later YesAzure SQL Database YesAzure SQL Managed Instance yesAzure Synapse Analytics.

Syntax

STRING_AGG ( expression, separator ) [ <order_clause> ]

<order_clause> ::=   
    WITHIN GROUP ( ORDER BY <order_by_expression_list> [ ASC | DESC ] )   

Remarks

STRING_AGG is an aggregate function that takes all expressions from rows and concatenates them into a single string. Expression values are implicitly converted to string types and then concatenated. The implicit conversion to strings follows the existing rules for data type conversions. For more information about data type conversions, see CAST and CONVERT (Transact-SQL).

If the input expression is type VARCHAR, the separator cannot be type NVARCHAR.

Null values are ignored and the corresponding separator is not added. To return a place holder for null values, use the ISNULL function as demonstrated in example B.

STRING_AGG is available in any compatibility level.

Official Documentation

73 questions
0
votes
1 answer

How to support STRING_AGG in django queryset for sql server 2017

I tried to return the queryset with one field which is the list string in django, for example, like this: Name: Chicken Wing Ids: 2529,2695,10829,10995,12129,12295 I only know how to get the queryset by GROUP_CONCAT which support…
Ellie
  • 1
  • 1
0
votes
1 answer

SQL Server [PATSTAT] query | Multiple charindex values &

Hello Stack Overflow Community. I am retrieving data with SQL from PATSTAT (patent data base from the European Patent Office). I have two issues (see below). For your info the PATSAT sql commands are quite limited. I. Charindex with multiple…
-1
votes
1 answer

SQL: Use STRING_AGG with condition

I have the following PostgreSQL query: (This is a Kata at codewars, you can find it at the following link: https://www.codewars.com/kata/64956edc8673b3491ce5ad2c/train/sql ) SELECT * FROM ( SELECT s.id AS student_id, MIN(s.name) AS name, …
Amir saleem
  • 1,404
  • 1
  • 8
  • 11
-1
votes
2 answers

SQL Aggregation function to get concrete value

I need help with an aggregation functionality. what I want to know is if it is possible to extract a concrete value from a grouped query on one of the columns I return, like…
-1
votes
1 answer

How to use STRING_AGG that selects from JOINs within an outer SELECT that also has JOINs

I have a compound SELECT statement that retrieves its values (columns) from several Joined tables. I want to add a column to the result set that is a STRING_AGG. The values that will be used in the STRING_AGG are retrieved from joined tables,…
PathJim
  • 3
  • 3
-1
votes
1 answer

Error Code: 1305. FUNCTION 577. employee bonus.string_agg does not exist

I use this code. but, it is giving me the error string_agg does not exist. What to do? select customer_id, budget, count(*) as no_of_product , string_agg(p.product_id) as list_of_product FROM customer_budget c left join (SELECT * , sum(cost)…
Sk337
  • 1
  • 2
-1
votes
1 answer

Aggregate multiple invoice numbers and invoice amount rows into one row

I have the following: budget_id invoice_number April June August 004 11 NULL 690 NULL 004 12 1820 NULL NULL 004 13 NULL NULL 890 What I want to do is do the following: budget_id invoice_number April June August 004 11, 12,…
runner16
  • 87
  • 13
-1
votes
1 answer

Deleting characters in a char array

#include #include #include # define N 100 using namespace std; int main() { char A[N]; unsigned char APP[256] = {0}; cout << "Insert string" << endl; cin.getline(A,100); for(int i=0; i…
Pepp
  • 9
-1
votes
1 answer

SQL - Postgres string agg is giving duplicates

Im trying to collect the foreign key mapping from system tables. And I used this below query. query 1: select kcu.table_schema, kcu.table_name as foreign_table, string_agg(kcu.column_name, ', ') as fk_columns, rel_tco.table_name as…
TheDataGuy
  • 2,712
  • 6
  • 37
  • 89
-2
votes
2 answers

Group By with Multiple Stuff Statements - SQL

I am trying to aggregate multiple rows into a single field (for multiple columns), grouped by a common ID (See below) This is the what I have ID CPT SVCDate Vendor 1 A3A 1/14/2023 A 2 DC6 1/23/2023 B 1 5WS 4/2/2023 A 3 …
-2
votes
2 answers

SQL query GROUP BY groups

I have something like this: id name totalAmount 1 name1 10 2 name1 20 3 name1 25 4 name2 5 5 name2 12 And need to looks like…
Ivan C
  • 3
  • 2
-2
votes
2 answers

Port STRING_AGG to FOR XML

I'm currently working on Data Discovery and Classification and I have a query that allows me to see a preview of the data while doing the classification. This is an example of how it works against AdventureWorks: DECLARE @TableName VARCHAR(100) =…
Francesco Mantovani
  • 10,216
  • 13
  • 73
  • 113
-8
votes
2 answers

sql aggregate and split

I have this table Table_1 : pk Column_1 1 addcd 2 swrrh 3 dggdd 4 wdffa I want to merge and split the result into 4 letters that will return something like this addc dswr rhdg gddw dffa Note: left over letters will be discarded I tried to use…
Ed Bangga
  • 12,879
  • 4
  • 16
  • 30
1 2 3 4
5