Dynamic SQL statement in data source through scripting

Aaron Munoz
Tera Guru

I want to create a SQL statement that uses two different datetime fields to pull delta records. I know there is a field called "Use last run datetime", but it can only use one field, and only imports records where the datetime field is between the last import and the lastest datetime in the chosen column. It would be something like this pseudo code:

IMPORT id
FROM my_table
WHERE (
(created_dt BETWEEN {ts gs.now()} AND {ts gs.now()})
OR
(approved_dt BETWEEN {ts gs.now()} AND {ts gs.now()})
)

the challenge is how to add a function like gs.now() inside the query that runs daily.

1 ACCEPTED SOLUTION

I think the following documentation should be helpful:

https://docs.teradata.com/reader/kmuOwjp1zEYg98JsB8fu_A/W2ZJsPFeR7EFlyBUnQWrEw

https://teradata.github.io/presto/docs/0.167-t/functions/datetime.html

 

I haven't worked on Teradata SQL, but from looking at the documentation my guess would be:

SELECT *
FROM my_table
WHERE created > current_date - interval '2' day OR approved = current_date

 

 

View solution in original post

6 REPLIES 6

SanjivMeher
Kilo Patron
Kilo Patron

Below should help. You dont need servicenow functions there. You should need sql date functions

 

https://dba.stackexchange.com/questions/208555/how-to-extract-data-between-yesterday-and-today


Please mark this response as correct or helpful if it assisted you with your question.

Mike Allen
Mega Sage

gs.now is:

 

DECLARE @LastChangeDate as date
SET @LastChangeDate = GetDate()

and you can put @LastChangeDate in your between where clause.

Aaron Munoz
Tera Guru

Thanks for the suggestions so far. I like the idea of using native functions, though GetDate() doesn't seem to work in Teradata. How would I perform a query in Teradata like the following:

SELECT *
FROM my_table
WHERE created > 'seven_days_ago' OR approved = 'today'

I think the following documentation should be helpful:

https://docs.teradata.com/reader/kmuOwjp1zEYg98JsB8fu_A/W2ZJsPFeR7EFlyBUnQWrEw

https://teradata.github.io/presto/docs/0.167-t/functions/datetime.html

 

I haven't worked on Teradata SQL, but from looking at the documentation my guess would be:

SELECT *
FROM my_table
WHERE created > current_date - interval '2' day OR approved = current_date