{"id":6960,"date":"2024-03-12T07:35:37","date_gmt":"2024-03-12T06:35:37","guid":{"rendered":"https:\/\/expert-only.com\/?p=6960"},"modified":"2024-03-12T11:40:27","modified_gmt":"2024-03-12T10:40:27","slug":"split-a-string-after-a-character","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/t-sql\/split-a-string-after-a-character\/","title":{"rendered":"How to Split a String after a Character with SQL Server ?"},"content":{"rendered":"\n<h4 class=\"wp-block-heading has-text-align-left\"><em>How to split a simple string stored in a variable after a specific character using SQL Server functions?<\/em><\/h4>\n\n\n\n<p>For example, use the substring function to split a string like file.txt to separate the file name from its extension after the dot character. Indeed, it is obvious that splitting a string just before the point character must be done dynamically, i.e., whatever the position of the character is.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-1-split-a-string-after-a-character-using-3-standard-t-sql-functions\">1. Split a string after a character using 3 standard T-SQL functions<\/h2>\n\n\n\n<p>First, to split a string with SQL Server just after a certain character, use the three following built-in text functions:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>SUBSTRING<\/strong> to trim a text by indicating the start and length to be trimmed.<\/li>\n\n\n\n<li><strong>CHARINDEX<\/strong> returns the position of a text to search. The function uses the text to search for, the text to search in and the starting position of the search.<\/li>\n\n\n\n<li><strong>LEN<\/strong> is a function that returns the length of a given text.<\/li>\n<\/ol>\n\n\n\n<p>By the way, this example is not recursive. However, to make it recursive, integrate the logic in a loop. Then adjust the variable that contains the character in the main string. This time, here is an example of <a href=\"https:\/\/expert-only.com\/en\/t-sql\/split-delimited-text-with-sql-server-xml-string-split-functions\/\">how to recursively split a string that contains delimiters<\/a> (like dots, semi-columns or commas) with a simple T-SQL script.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. T-SQL query to cut the text using 3 variables<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>StartString <\/strong>: to store the complete string to process.<\/li>\n\n\n\n<li><strong>Result <\/strong>: to store the result of the string functions.<\/li>\n\n\n\n<li><strong>Character <\/strong>: the delimiter to search inside the text.<\/li>\n<\/ol>\n\n\n\n<p>To adapt it to your coding case and make your SQL query work, simply change the @Character variable. And of course, also change the assigned value in the @StartString T-SQL variable. Example of T-SQL code to split a text after a specific character using SQL Server <a href=\"https:\/\/learn.microsoft.com\/en-us\/sql\/t-sql\/functions\/substring-transact-sql?view=sql-server-ver15\" target=\"_blank\" rel=\"noreferrer noopener\">substring<\/a> function.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">-- Declaration of variables used as start string, result string\n-- and the character used to split the text\ndeclare @StartString nvarchar(100)\ndeclare @Result nvarchar(100)\ndeclare @Character nvarchar(10)\n\n-- Set the string, for example 'file.ext' \nset @StartString = 'START_STRING.END_STRING'\n\n-- Set the character or string to search for to split the string in two\nset @Character = '.'\nset @Result =\n  substring(@StartString,CHARINDEX(@Character, @StartString)+1,len(@StartString))\n\nselect @StartString as 'Start String'\nselect @Result as 'Result String'\n<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"560\" src=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/11\/t-sql-query-to-split-string-after-character.jpg\" alt=\"How to Split a String after a Character with SQL Server ?\" class=\"wp-image-16556\" srcset=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/11\/t-sql-query-to-split-string-after-character.jpg 800w, https:\/\/expert-only.com\/wp-content\/uploads\/2022\/11\/t-sql-query-to-split-string-after-character-300x210.jpg 300w, https:\/\/expert-only.com\/wp-content\/uploads\/2022\/11\/t-sql-query-to-split-string-after-character-768x538.jpg 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><figcaption class=\"wp-element-caption\"><em>How to Split a String after a Character with SQL Server ?<\/em><\/figcaption><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-splitting-sql-strings-without-the-split-string-function\">Splitting SQL strings without the SPLIT_STRING function<\/h3>\n\n\n\n<p>In the screenshot of the query, the last part of the text is isolated by the substring function and displayed as a result. Here is another SQL tutorial on text management, learn <a href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-text-with-more-than-8000-characters\/\">to manage strings with more than 8000 characters with T-SQL variables<\/a> and specifically inside stored procedures.<\/p>\n\n\n\n<figure class=\"wp-block-embed aligncenter is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"zvlZR93qs0\"><a href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-string-longer-than-8000-chars\/\">SQL Server string longer than 8000 characters<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;SQL Server string longer than 8000 characters&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-string-longer-than-8000-chars\/embed\/#?secret=lGeQuVTkdz#?secret=zvlZR93qs0\" data-secret=\"zvlZR93qs0\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>How to split a simple string stored in a variable after a specific character using SQL Server functions? For example, use the substring function to split a string like file.txt to separate the file name from its extension after <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/t-sql\/split-a-string-after-a-character\/\" title=\"How to Split a String after a Character with SQL Server ?\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":6206,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[454],"tags":[],"class_list":{"0":"post-6960","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-t-sql"},"_links":{"self":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/6960","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/comments?post=6960"}],"version-history":[{"count":3,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/6960\/revisions"}],"predecessor-version":[{"id":30628,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/6960\/revisions\/30628"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/6206"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=6960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=6960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=6960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}