Skip to main content
Solved

Select records from the previous 5 years based on the current date


craig
Participant
  • Participant

Hi, I have a database table with a date column containing dates formatted as YYYYMMDD. I need to select all records from the table with a date ranging from the current date to 5 years previous of the current date. Any assistance appreciated.

Best answer by david_r

What type of database is this? Most databases can do this in a simple where-clause, e.g. Oracle:

WHERE my_timestamp BETWEEN add_months(trunc(sysdate), -12*5AND sysdate

Or SQL Server:

WHERE my_timestamp BETWEEN DateAdd(yy, -5, GetDate()) AND GetDate()

Or PostgreSQL:

WHERE my_timestamp BETWEEN NOW() - INTERVAL '5 years' AND NOW()

You can then use this where-clause in either a regular reader, a FeatureReader or a SQLExecutor. This will be a lot faster than reading the entire table contents into FME and filter there.

View original
Did this help you find an answer to your question?

5 replies

ebygomm
Influencer
Forum|alt.badge.img+32
  • Influencer
  • June 16, 2020

Are you reading all the records in and then want to restrict to only keep the last five years, or do you just want to read the last five years in from the database table? If the latter, you probably need to specify what format your data is in.


david_r
Celebrity
  • Best Answer
  • June 16, 2020

What type of database is this? Most databases can do this in a simple where-clause, e.g. Oracle:

WHERE my_timestamp BETWEEN add_months(trunc(sysdate), -12*5AND sysdate

Or SQL Server:

WHERE my_timestamp BETWEEN DateAdd(yy, -5, GetDate()) AND GetDate()

Or PostgreSQL:

WHERE my_timestamp BETWEEN NOW() - INTERVAL '5 years' AND NOW()

You can then use this where-clause in either a regular reader, a FeatureReader or a SQLExecutor. This will be a lot faster than reading the entire table contents into FME and filter there.


craig
Participant
  • Author
  • Participant
  • June 16, 2020

Hi david_r & ebygomm, thanks for the replies. it's an SQL DB and I want to output all records in one stream (which I'm OK with) and output the date filtered records to another.


david_r
Celebrity
  • June 16, 2020

If you want a pure FME solution, you can use the DateTimeCalculator to create a timestamp five years back:

You can then use e.g. a Tester to check if your timestamp is between five_years_ago and @DateTimeNow()


ebygomm
Influencer
Forum|alt.badge.img+32
  • Influencer
  • June 16, 2020

You can also do this directly in a tester, the right statement returns a date five years ago from now and you can test for greater than this

The statement on the right gets today's date (@DateTimeNow), formats it to drop the time (@DateTimeFormat) then subtracts 5 years from that value (@DateTimeAdd)


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings