{"id":1012,"date":"2021-10-03T13:09:03","date_gmt":"2021-10-03T07:39:03","guid":{"rendered":"https:\/\/techieshouts.com\/home\/?p=1012"},"modified":"2022-08-09T19:03:13","modified_gmt":"2022-08-09T13:33:13","slug":"substring-in-teradata","status":"publish","type":"post","link":"https:\/\/techieshouts.com\/home\/substring-in-teradata\/","title":{"rendered":"Substring function in Teradata"},"content":{"rendered":"\n<p>Be it a programming language or the querying language, the substring function is one of the most commonly used ones. The need of extracting the particular portion in a string is much needed while working with the data especially when the contents are of the same length. This will help a programmer to extract a piece of meaningful information from a large string. Let us see the type of substring functions in Teradata.<\/p>\n\n\n\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Teradata\" target=\"_blank\" rel=\"noopener\">Teradata<\/a> has two functions to get this done.<\/p>\n\n\n\n<ol><li>SUBSTRING &#8211; ANSI SQL-2011 compliant function<\/li><li>SUBSTR &#8211; Teradata extended ANSI SQL-2011 compliant function<\/li><\/ol>\n\n\n\n<h2>Supported data types<\/h2>\n\n\n\n<p>Both these functions support Character, Byte, and Numeric data types. There will be an implicit conversion into characters for the Numeric data types. Let us see each of these Teradata substring functions with examples. <\/p>\n\n\n\n<h2>Syntax<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SUBSTRING(String expression, starting position, number of characters)\nSUBSTR(String expression, starting position, number of characters)<\/pre>\n\n\n\n<h2>Example 1 &#8211; Substring using the SUBSTRING function<\/h2>\n\n\n\n<p>Let us see an example to understand this function better. For example, there is a column in a table that holds the license number of people in a specific format. This could be because the source system has stored the information in that manner. During data migration, if we want to extract and keep them in separate columns, the substring function would be very useful.<\/p>\n\n\n\n<p>Consider that the license number is stored in a column of a table in the database as shown below<\/p>\n\n\n\n<p>INDIA-TN-2020-ABC12345(COUNTRY-STATE-LICENSED)<\/p>\n\n\n\n<p>In this, if you want to extract the state information, you can use the substring function<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SELECT SUBSTRING('INDIA-TN-ABC12345', 7,2);<\/pre>\n\n\n\n<h3>Output<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"932\" height=\"402\" src=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-2.png\" alt=\"\" class=\"wp-image-1013\" srcset=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-2.png 932w, https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-2-300x129.png 300w, https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-2-768x331.png 768w, https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-2-660x285.png 660w\" sizes=\"(max-width: 932px) 100vw, 932px\" \/><\/figure>\n\n\n\n<h2>Example 2 &#8211; Substring using the SUBSTR function<\/h2>\n\n\n\n<p>Suppose, you are storing the name of the log file in a column of a table for auditing purposes like below. In that, if you want to extract the DATE value alone, you can use the SUBSTR function to extract that.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SELECT SUBSTRING('logfile_20201002_database.log',9,8) as LogDate;<\/pre>\n\n\n\n<h3>Output<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"1024\" height=\"368\" src=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-4-1024x368.png\" alt=\"\" class=\"wp-image-1015\" srcset=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-4-1024x368.png 1024w, https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-4-300x108.png 300w, https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-4-768x276.png 768w, https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-4-660x237.png 660w, https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-4.png 1108w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Also read,<\/p>\n\n\n\n<ol><li><a href=\"https:\/\/techieshouts.com\/home\/concatenation-of-columns-values-in-teradata\/\">Concatenation function in Teradata<\/a><\/li><li><a href=\"https:\/\/techieshouts.com\/home\/teradata-get-logged-in-user-details\/\">Fetch logged in user details<\/a><\/li><li><a href=\"https:\/\/techieshouts.com\/home\/teradata-get-current-date-and-time\/\">Get the Current date and time in Teradata<\/a><\/li><\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Be it a programming language or the querying language, the substring function is one of the most commonly used ones. The need of extracting the particular portion in a string is much needed while working with the data especially when the contents are of the same length. This will help a programmer to extract a\u2026 <span class=\"read-more\"><a href=\"https:\/\/techieshouts.com\/home\/substring-in-teradata\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[179,177,208,209,178],"_links":{"self":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/1012"}],"collection":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/comments?post=1012"}],"version-history":[{"count":5,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/1012\/revisions"}],"predecessor-version":[{"id":1215,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/1012\/revisions\/1215"}],"wp:attachment":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/media?parent=1012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/categories?post=1012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/tags?post=1012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}