{"id":5379,"date":"2016-09-18T02:08:09","date_gmt":"2016-09-17T20:38:09","guid":{"rendered":"http:\/\/sqlhints.com\/?p=5379"},"modified":"2016-09-18T02:09:01","modified_gmt":"2016-09-17T20:39:01","slug":"trim-function-in-sql-server","status":"publish","type":"post","link":"https:\/\/sqlhints.com\/2016\/09\/18\/trim-function-in-sql-server\/","title":{"rendered":"TRIM function in Sql Server"},"content":{"rendered":"<p style=\"text-align: justify;\">Many a times we come across a scenario where we get an input string with leading and\/or trailing spaces. In such scenarios we would like to store it into the database by removing the leading and trailing spaces. But Microsoft doesn&#8217;t have a built-in TRIM function which can remove both leading and Trailing spaces.<\/p>\n<p style=\"text-align: justify;\">Instead it has LTRIM function which can be used to remove leading blanks and RTRIM function which can be used to remove trailing spaces. Let us understand these functions with examples and then see how we can nest these two functions together as shown in the below examples to remove leading and trailing spaces.<\/p>\n<h3>LTRIM Function<\/h3>\n<p style=\"text-align: justify;\">LTRIM function removes the leading spaces from the input string<\/p>\n<pre class=\"brush: sql; gutter: false\">\r\nDECLARE @StringToTrim VARCHAR(100) = &#039;    String to trim    &#039;\r\nSELECT @StringToTrim &#039;String To Trim&#039;, \r\n\t  DATALENGTH(@StringToTrim) &#039;Length of the string&#039;,\r\n\t  LTRIM(@StringToTrim) &#039;String trimmed by LTRIM&#039;, \r\n\t  DATALENGTH(LTRIM(@StringToTrim)) &#039;Length of LTRIM string&#039;\r\n<\/pre>\n<p><strong>RESULT:<\/strong><br \/>\n<a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/09\/LTRIM-in-Sql-Server.jpg\" rel=\"attachment wp-att-5381\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/09\/LTRIM-in-Sql-Server.jpg\" alt=\"ltrim-in-sql-server\" width=\"493\" height=\"168\" class=\"alignnone size-full wp-image-5381\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/09\/LTRIM-in-Sql-Server.jpg 493w, https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/09\/LTRIM-in-Sql-Server-300x102.jpg 300w\" sizes=\"auto, (max-width: 493px) 100vw, 493px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\">From the above result we can see that the LTRIM function is removing only the leading spaces. That is it has removed only the leading 4 spaces and not the trailing 4 spaces.<\/p>\n<h3>[ALSO READ] <a href=\"https:\/\/sqlhints.com\/2015\/07\/18\/difference-between-len-and-datalength-functions-in-sql-server\/\" target=\"_blank\">Difference between Len() and Datalength() functions in Sql Server<\/a> <\/h3>\n<h3>RTRIM Function<\/h3>\n<p style=\"text-align: justify;\">RTRIM function removes the trailing spaces from the input string<\/p>\n<pre class=\"brush: sql; gutter: false\">\r\nDECLARE @StringToTrim VARCHAR(100) = &#039;    String to trim    &#039;\r\nSELECT @StringToTrim &#039;String To Trim&#039;, \r\n\t  DATALENGTH(@StringToTrim) &#039;Length of the string&#039;,\r\n\t  RTRIM(@StringToTrim) &#039;String trimmed by RTRIM&#039;, \r\n\t  DATALENGTH(RTRIM(@StringToTrim)) &#039;Length of RTRIM string&#039;<\/pre>\n<p><strong>RESULT:<\/strong><br \/>\n<a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/09\/RTRIM-in-Sql-Server.jpg\" rel=\"attachment wp-att-5382\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/09\/RTRIM-in-Sql-Server.jpg\" alt=\"rtrim-in-sql-server\" width=\"492\" height=\"167\" class=\"alignnone size-full wp-image-5382\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/09\/RTRIM-in-Sql-Server.jpg 492w, https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/09\/RTRIM-in-Sql-Server-300x102.jpg 300w\" sizes=\"auto, (max-width: 492px) 100vw, 492px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\">From the above result we can see that the RTRIM function is removing only the trailing spaces. That is it has removed only the trailing 4 spaces and not the leading 4 spaces.<\/p>\n<h3>[ALSO READ] <a href=\"https:\/\/sqlhints.com\/2011\/08\/28\/usage-of-function-on-index-column-leads-to-indextable-scan\/\" target=\"_blank\">Usage of Function on Index Column in WHERE Caluse Leads to Index\/Table Scan<\/a><\/h3>\n<h3>TRIM Function<\/h3>\n<p style=\"text-align: justify;\">To remove the leading and trailing spaces we can nest the above LTRIM and RTRIM functions as shown in the below example:<\/p>\n<pre class=\"brush: sql; gutter: false\">\r\nDECLARE @StringToTrim VARCHAR(100) = &#039;    String to trim    &#039;\r\nSELECT @StringToTrim &#039;String To Trim&#039;, \r\n DATALENGTH(@StringToTrim) &#039;Length of the string&#039;,\r\n RTRIM(LTRIM(@StringToTrim)) &#039;String trimmed by LTRIM and RTRIM&#039;, \r\n DATALENGTH(RTRIM(LTRIM(@StringToTrim)))\r\n  AS &#039;Length of trimmed string&#039;\r\n<\/pre>\n<p><strong>RESULT:<\/strong><br \/>\n<a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/09\/TRIM-in-Sql-Server.jpg\" rel=\"attachment wp-att-5383\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/09\/TRIM-in-Sql-Server.jpg\" alt=\"trim-in-sql-server\" width=\"562\" height=\"167\" class=\"alignnone size-full wp-image-5383\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/09\/TRIM-in-Sql-Server.jpg 562w, https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/09\/TRIM-in-Sql-Server-300x89.jpg 300w\" sizes=\"auto, (max-width: 562px) 100vw, 562px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\">From the above result we can see that the nested use of RTRIM and LTRIM functions has removed both the leading and trailing paces.. That is it has removed both the leading 4 spaces and the trailing 4 spaces from the input string.<\/p>\n<p style=\"text-align: justify;\">We can create a user defined function like the below one. And use it wherever we want to remove both leading and trailing spaces.<\/p>\n<pre class=\"brush: sql; gutter: false\">\r\nCREATE\tFUNCTION dbo.TRIM(@StringToTrim VARCHAR(MAX))\r\nRETURNS\tVARCHAR(MAX)\r\nBEGIN \r\n\tRETURN\tRTRIM(LTRIM(@StringToTrim))\r\nEND\r\n<\/pre>\n<p style=\"text-align: justify;\">Below example shows how we can use the above created user defined TRIM function:<\/p>\n<pre class=\"brush: sql; gutter: false\">\r\nDECLARE @StringToTrim VARCHAR(100) = &#039;    String to trim    &#039;\r\nSELECT dbo.TRIM( @StringToTrim ) &#039;Trimmed string&#039;,\r\n DATALENGTH(dbo.TRIM( @StringToTrim )) \r\n  AS &#039;Length of trimmed string&#039;\r\n<\/pre>\n<p><strong>RESULT:<\/strong><br \/>\n<a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/09\/TRIM-function-in-Sql-Server.jpg\" rel=\"attachment wp-att-5386\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/09\/TRIM-function-in-Sql-Server.jpg\" alt=\"trim-function-in-sql-server\" width=\"519\" height=\"129\" class=\"alignnone size-full wp-image-5386\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/09\/TRIM-function-in-Sql-Server.jpg 519w, https:\/\/sqlhints.com\/wp-content\/uploads\/2016\/09\/TRIM-function-in-Sql-Server-300x75.jpg 300w\" sizes=\"auto, (max-width: 519px) 100vw, 519px\" \/><\/a><\/p>\n<h3>[ALSO READ] <a href=\"https:\/\/sqlhints.com\/2016\/08\/28\/100-frequently-used-queries-in-sql-server-part-1\/\" target=\"_blank\">100 Frequently used queries in Sql Server \u2013 Part 1<\/a><\/h3>\n","protected":false},"excerpt":{"rendered":"<p>Many a times we come across a scenario where we get an input string with leading and\/or trailing spaces. In such scenarios we would like to store it into the database by removing the leading and trailing spaces. But Microsoft doesn&#8217;t have a built-in TRIM function which can remove both leading and Trailing spaces. Instead &hellip; <a href=\"https:\/\/sqlhints.com\/2016\/09\/18\/trim-function-in-sql-server\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">TRIM function in Sql Server<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[3,118],"tags":[1284,1287,1289,1290,1291,1292,1285,1288,321,986,1283,1286,1293],"class_list":["post-5379","post","type-post","status-publish","format-standard","hentry","category-sql-server","category-string-functions-functions","tag-ltrim","tag-ltrim-function-in-sql-server","tag-remove-blank-spaces","tag-remove-empty-spaces","tag-remove-leading-spaces","tag-remove-trailing-spaces","tag-rtrim","tag-rtrim-function-in-sql-server","tag-sql","tag-sql-server","tag-trim","tag-trim-funtion-in-sql-server","tag-trim-in-sql-server"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3xNAz-1oL","_links":{"self":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/5379","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/comments?post=5379"}],"version-history":[{"count":9,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/5379\/revisions"}],"predecessor-version":[{"id":5453,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/5379\/revisions\/5453"}],"wp:attachment":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/media?parent=5379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/categories?post=5379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/tags?post=5379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}