2

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 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah this is the solution I implemented but I was wondering if we can somehow directly make a generic call
You could use stored procedures and compose dynamic strings to be evaluated, but the per-record overhead will be quite high, so it's probably only efficient for small datasets.
@MarcinZukowski Yip, I was going to suggest that, but it's along the same lines as do all your ELT via INSERT commands. for tiny SQL "it just works (tm)" for real data sizes, your compile times will be longer than your execution times (speaking from a lift and shift experience)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.