Split function in SQL Server to break Comma separated strings

14 Flares Filament.io 14 Flares ×

Sometimes we need to insert – save multiple checked checkbox values in SQL Server.For that we can use Split function.

Split function of SQL Server is not in-built function,We have to add it manually in our Table-valued function as you can see in below image.

Split-function-in-SQL-Server

Split function in SQL Server

Below you can find query for splitting the string in SQL.

Now,I will explain how to use this split function.Once you have created above split function in your database,It will add to Table-valued function.

Now whenever you want to separate string by comma,just pass your string to the function,it will return Data column.

Here’s I will use my table that I created in Case Statement in SQL Server.You can find create,insert script for Product table from that article.On select of product table,you will find below image.

Note : To use this tutorial,allow NULL values to Price & Availability columns.

SQL-Server-Product-Table

Now,here I will pass ProductNames variable as comma separated string,in your case it may be checked items or anything else.But to use split function of this SQL server,you need to pass comma separated string to function.

I will add MotherBoard & other 2 products in table using below script.You can also make stored procedure according to your requirement.

Now after running above script,you can find below image Product table as 3 rows inserted in Product table.

After-save-multiple-checked-checkbox-values-in-SQL-Server

This is how you can save multiple checked checkbox values tho SQL Server table.

Related reading : Find duplicate records in SQL Server

Hope,you enjoy this article.You can also check other SQL Tutorial.

You can also share some another ideas about Split function in SQL Server by commenting below to me & other readers.!

5 thoughts on “Split function in SQL Server to break Comma separated strings

  1. nice post really help me……..

    • Thanks,Good to see it help you.Don’t forget to share it.!

      • Hi pratik,

        I like your article and its very easy to use.
        However, I am facing below issue. I am doing in following way..

        ) I am sending two strings as input parameter to stored procedure

        1) strControls :- ‘txtname’|’txtaddress’|’txtcity’
        2) strControlValues :- ‘ABC’|’XYZ’|’PQR’

        Now in SQL Server, I want to split this string to form the insert query.How can I do that ?

        2) Second thing is I have configuration table in which control value should be stored in which column is configured like below

        ControlName ColumnName
        ——————————————
        txtName Name
        txtAddress Address
        txtCity City

        I have stored the above configuration is #temp table in sql..

        and now by splitting string I need to check whether it does exists in configured table OR not ?? So how can i do it ??

  2. Give me stored procedure using sql server,

    Table1:

    Id FullName
    ——————-
    1 Cleo,Smith,james

    Table2:

    I want to separate the comma delimited string into 3 columns

    Id FullName Name Surname Last
    — ———— ——- ———– ——
    1 Cleo,Smith Cleo Smith james

    First table values separate comma vles after that values insert into Table2 ?

  3. I am your fan! this helped me a lot

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">