SQLScript Locate String Function for SAP HANA SQL Developer
SAP SQLScript developers use LOCATE SQL string function to find the position of a substring in an other given string value. SQL programmers can use the string function Locate to search for an explicit text within an other longer text variable or in a string type table column. An other use of Locate for SAP HANA developers is to split text objects to find the exact place of delimiter characters.
Here is sample SQL code where Locate string function is used with is basic form but with special string variables in SQLScript by SAP HANA developer
As HANA developers will realize easily, whenever one of the arguments is NULL then the return value of the string function Locate() is NULL too.
If the string to search is empty string '' then the Locate function returns the first character by its return value as 1
Here is the outputs of the sample SQLScript code where string function Locate is used on SQL Console
In fact, the SAP HANA SQLScript Locate string function takes two optional arguments which provide more flexibility to SQL developers.
Locate function haystack argument is the main string object where the needle argument is being searched for.
start_position argument enables the developer to search the needle within haystack beginning from a certain numbers of characters from left to the end of the haystack
occurrences argument enables the search for the nth occurence of the needle within haystack
Let's make a few more SQLScript examples to understand the Locate function arguments.
I executed below SQLScript code anonymous block on SQL Console using SAP HANA Web-based Development Workbench
Let's check the outputs of each SQLScript Select statements executed on SQL Console
First seperator position is found with SQL Locate() function and the string up to that position is fetched with SQLScript Left() string function as the first string piece
Below SQL Select output is very similar with above one.
With one difference, SQLScript SubString function is used for fetching the second string.
On the last SQL Select statement and its output is here.
SQLScript Locate() string function is used with start_position and occurrences input arguments.
SQLScript developers can refer to SAP Help Portal for more information on SQL string function Locate in SQLScript
For the SQLScript Left string function which I have used in these SQL scripts frequently, SAP HANA database developers can refer to given tutorial.