There are scenarios where we get data in the form of strings/character sequences with some delimiter. And we want to break/split the string on delimiters and use the sub-strings in In-clause of subquery.
SELECT regexp_substr('tractor,scooter,bike','[^,]+',1,day_of_calendar) fields
FROM sys_calendar.calendar
WHERE day_of_calendar <= (CHAR('tractor,scooter,bike') - CHAR(oreplace('tractor,scooter,bike',',','')))+1;
Output:
fields
~~~~~~~~
tractor
scooter
bike
SELECT * FROM
WHERE yourtable.requiredColumn in
(
SELECT regexp_substr('tractor,scooter,bike','[^,]+',1,day_of_calendar) fields
FROM sys_calendar.calendar
WHERE
day_of_calendar <= (CHAR('tractor,scooter,bike') - CHAR(oreplace('tractor,scooter,bike',',','')))+1
);
SELECT (CHAR('tractor,scooter,bike') - CHAR(oreplace('tractor,scooter,bike',',','')))+1;
Output: 3
Here CHAR is used to get the length of string and visit Teradata String Functions to learn more about string functions
SELECT day_of_calendar
FROM sys_calendar.calendar
WHERE day_of_calendar <= (CHAR('tractor,scooter,bike') - CHAR(oreplace('tractor,scooter,bike',',','')))+1;
Output:
day_of_calendar
~~~~~~~~~~~~~~~
1
2
3
regexp_substr('tractor,scooter,bike','[^,]+',1,n);
**Here n is number and will return nth value in the string after splitting on delimiter.