Use function/parameters stored in columns

Viewed 51

I'm not sure this question has already been asked, if so I didn't find it.

For a log project we used table to parametrize our data loading run and get track of their basic configuration.

On the specific side of the pull date we also wanna have the configuration in the table and so we putted three information in it: the base date / the range we use to calculate pull from and pull to timestamps.

My issue here is I wanna call the DATEADD() function using those columns as parameters but I don't know how to unstring those values as 'CURRENT_TIMESTAMP' will not work, just like DATEADD('year',...) will also raise an error cause the parameter should not be a string.

To summ up, from values ['CURRENT_TIMESTAMP', 'year', '-6'] in base I wanna create the call DATEADD(year, -6, CURRENT_TIMESTAMP)

Thanks

Screen of my table

1 Answers

Make a case statement, and just match the two columns to there values. Remember case stop on the first in order match.

CASE 
    WHEN col1 = ‘CURRENT_TOMESTAMP’’ AND col2 = ‘year’ 
        THEN DATEADD(year, col3, CURRENT_TIMESTAMP)
    WHEN col1 = ‘CURRENT_TOMESTAMP’’ AND col2 = ‘month’ 
        THEN DATEADD(month, col3, CURRENT_TIMESTAMP)
...

Etc etc

Related