123ArticleOnline Logo
Welcome to 123ArticleOnline.com!
ALL >> General >> View Article

Adding Computed Columns In Sql Server By Sql Yoga Guide

Profile Picture
By Author: sql yoga
Total Articles: 4
Comment this article
Facebook ShareTwitter ShareGoogle+ ShareTwitter Share

Today, I encountered a situation where I needed to display all related data in a comma-separated list using a SELECT statement. Up until now, I have been using a scalar function that utilizes COALESCE() to achieve this. However, I discovered a fantastic solution that allows us to generate a comma-separated list without needing a scalar function. Let’s dive into it with SQLYoga guide.

Example Scenario
Consider the following table:


CREATE TABLE #test(
field1 VARCHAR(5),
field2 VARCHAR(5)
);

Let’s insert some data into this table:

INSERT INTO #test
SELECT '001','AAA'
UNION ALL
SELECT '001','BBB'
UNION ALL
SELECT '002','CCC'
UNION ALL
SELECT '003','DDD'
UNION ALL
SELECT '004','EEE'
UNION ALL
SELECT '004','FFF'
UNION ALL
SELECT '004','GGG';

Current Data in the Table
After inserting the data, our table looks like this:

Output: www.sqlyoga.com


Expected Output
We want to generate a comma-separated list of field2 for each unique field1. The expected ...
... output is:

Output: www.sqlyoga.com


Proposed Solution
Here’s the SQL query to achieve the desired output:

SELECT field1,
SUBSTRING(
(
SELECT ', ' + field2
FROM #test t2
WHERE t1.field1 = t2.field1
ORDER BY t2.field2
FOR XML PATH('')
), 3, 1000) AS field2_list
FROM #test t1
GROUP BY field1;

Explanation
1. Subquery with FOR XML PATH: The subquery concatenates field2 values into a single string separated by commas for each field1. The FOR XML PATH('') clause converts the result into XML format, which we then transform into a plain string.

2.Substring Function: The SUBSTRING function removes the leading comma and space from the concatenated string. The 3 indicates starting from the third character, effectively skipping the first two characters (, ).

3.Group By: The GROUP BY clause ensures we get one row per field1.


Output
Running the above query will yield the following output:

Output: www.sqlyoga.com


With this approach, you can generate a comma-separated list without relying on scalar functions. This method is efficient and leverages SQL Server’s XML capabilities to concatenate strings.

Read More on SQLYoga about advanced SQL techniques and optimizing your queries.

At SQLYoga, we are committed to bringing you the latest and most efficient SQL Server solutions. This method of generating a comma-separated list is a prime example of how you can streamline your SQL queries and achieve better performance. Stay tuned for more tips and tricks to enhance your SQL skills and streamline your database management tasks.

Visit SQLYoga for more articles and tutorials on SQL Server. Join our community of SQL enthusiasts and take your database skills to the next level with SQLYoga.

Total Views: 174Word Count: 452See All articles From Author

Add Comment

General Articles

1. From 8k To 720p: When It’s Okay To Downscale
Author: Tekedge

2. Physical Security Consultancy And Cctv Systems Design Services In Dubai
Author: DSP Consultants

3. At Last, Underwear For Sensitive Skin That Doesn’t Irritate
Author: Lets Tilt

4. Still Settling For Less? Try Underwear For Plus Size Ladies That Wins
Author: Lets Tilt

5. What Makes Up For Anti Odor Underwear Women Love? Let's Find Out!
Author: Lets Tilt

6. Best Breathable Underwear For Women? This One’s Viral
Author: Lets Tilt

7. Super App Development Services: Merging E-commerce, Fintech, And Mobility In One Ecosystem
Author: michaeljohnson

8. Surgical Modifier 62: Comprehensive Guide For Assistant Surgeon Billing | Allzone
Author: Albert

9. Lucintel Forecasts The Global Education Tablet Market To Grow With A Cagr Of 4.3% From 2025 To 2031
Author: Lucintel LLC

10. Ai Agent Development: Redefining The Future Of Intelligent Systems In The United States
Author: eliza josh

11. Best Suburb To Live In Queensland & Best Suburb To Invest In Queensland: 2025 Property Insights
Author: Koala Invest

12. Choosing Between A Chatbot Development Company And Ai Chatbot Solutions Provider
Author: david

13. Kyc Bpo Banking Process With Zoetic Bpo Services
Author: Zoetic BPO Services

14. Why Crossbody Handbags And Belt Bags For Women Are So Popular?
Author: Aries Choy

15. Why Ucc Ireland Is The Smart Choice For International Students
Author: anjanasri

Login To Account
Login Email:
Password:
Forgot Password?
New User?
Sign Up Newsletter
Email Address: