I have the below data in a table.
| row_num | desc |
|---|---|
| 1 | First Name/Last Name - Middle Name |
| 2 | FirstName/LastName - MiddleName |
| 3 | FirstName/LastName |
I am looking for the desired output as below
| row_num | desc_1 | desc_2 | desc_3 |
|---|---|---|---|
| 1 | First Name | Last Name | Middle Name |
| 2 | FirstName | LastName | MiddleName |
| 3 | FirstName | LastName | NULL |
In sybase I am not able to use functions like SUBSTR(desc,INSTR(desc,' ',1,1) + 1) . Need some inputs on how to achieve the desired result set in Sybase
Assumptions:
Sybase ASEso the functions of interest will besubstring()andcharindex()@variablesas part of a looping/cursor-based construct)desccolumns are of the formatfirstname/lastname - middlename(so we can search for the static delimiters/and-)The general approach is to use
charindex()to find the positions of our delimiters and then feed these positions tosubstring().Since
ASEdoes not support CTEs we'll use a derived table to generate the delimiter start positions:NOTES:
descis a reserved word inASEhence the need to bracket the name ([desc])substring()replace100with a number that's at least as long as thedesccolumndt) by replacing eachdt.posXwith the associatedcharindex()call (obviously thesubstring()calls would become a bit unwieldly)This generates:
FWIW, SQL Function/Reference manuals for the various Sybase RDBMS products: