How do I convert a comma separated string to a temp table SQL server?

Asked by Last Modified  

1 Answer

Learn SQL Server

Follow 1
Answer

Please enter your answer

To convert a comma-separated string into a temporary table in SQL Server, you can use a combination of string splitting and table variable or temporary table. The `STRING_SPLIT` function is available in SQL Server 2016 and later versions and is specifically designed for this purpose.Here's an example...
read more
To convert a comma-separated string into a temporary table in SQL Server, you can use a combination of string splitting and table variable or temporary table. The `STRING_SPLIT` function is available in SQL Server 2016 and later versions and is specifically designed for this purpose.Here's an example of how you can achieve this:```sql-- Declare a variable to hold the comma-separated stringDECLARE @YourCommaSeparatedString VARCHAR(MAX) = 'value1,value2,value3';-- Create a temporary table to store the valuesCREATE TABLE #TempTable (Column1 VARCHAR(255));-- Insert the values from the comma-separated string into the temporary tableINSERT INTO #TempTable (Column1)SELECT TRIM(VALUE) AS Column1FROM STRING_SPLIT(@YourCommaSeparatedString, ',');-- Display the contents of the temporary tableSELECT * FROM #TempTable;-- Drop the temporary table when you are doneDROP TABLE #TempTable;```Replace `'value1,value2,value3'` with your actual comma-separated string, and adjust the data type and size of the temporary table column based on your needs.Keep in mind that the `STRING_SPLIT` function splits the string based on the specified delimiter (in this case, the comma `,`). If you are using an older version of SQL Server that does not support `STRING_SPLIT`, you might need to use a custom string splitting function or another method to achieve the same result. In such cases, various user-defined functions are available online for string splitting, or you can create your own if necessary. read less
Comments

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Recommended Articles

Microsoft Office is a very popular tool amongst students and C-Suite. Today, approximately 1.2 billion people across 140 countries use the office programme. It is used at home, schools and offices on a daily basis for organizing, handling and presenting data and information. Microsoft Office Suite offers programs that can...

Read full article >

Whether it was the Internet Era of 90s or the Big Data Era of today, Information Technology (IT) has given birth to several lucrative career options for many. Though there will not be a “significant" increase in demand for IT professionals in 2014 as compared to 2013, a “steady” demand for IT professionals is rest assured...

Read full article >

Information technology consultancy or Information technology consulting is a specialized field in which one can set their focus on providing advisory services to business firms on finding ways to use innovations in information technology to further their business and meet the objectives of the business. Not only does...

Read full article >

Software Development has been one of the most popular career trends since years. The reason behind this is the fact that software are being used almost everywhere today.  In all of our lives, from the morning’s alarm clock to the coffee maker, car, mobile phone, computer, ATM and in almost everything we use in our daily...

Read full article >

Looking for SQL Server Training?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you