简体   繁体   中英

How do I code a dynamic NetSuite date range that begins the 1st day of the month (4 months ago) and ends the last day of last month?

I'm creating a formula(numeric) field in the criteria of a NetSuite Transaction Search. My goal is to view transactions with {transdate} between 4 months ago (first day) and last month (last day). A dynamic date range is important.

Example: If today's date is 1/15/19, then the criteria should filter out all transactions except those that fall between 9/1/18 and 12/31/18. If I run the search on 7/1/19, then the range should be 3/1/19 thru 6/31/19.

This is the Formula(Numeric) code that is failing:

  CASE WHEN {trandate} BETWEEN
  TO_DATE(TRUNC((ADD_MONTHS({today},-4)),’MONTH’)) AND
  TO_DATE(LAST_DAY(TRUNC({today},'MONTH')-1)) THEN 1 ELSE 0 END

Results in the following error:
“Your formula has an error in it. It could resolve to the wrong datatype, use an unknown function, or have a syntax error. Please go back, correct the formula, and re-submit.”

The closest I've come to solving the problem is this iteration:

 CASE WHEN {trandate} BETWEEN 
 TO_DATE(ADD_MONTHS({today},-4)) AND
 TO_DATE(LAST_DAY(TRUNC({today},'MONTH')-1)) THEN 1 ELSE 0 END

The problem is the first to_date code doesn't give me first day of the month. I end up with a range 9/15/18 thru 12/31/18. I thought adding a TRUNC would take me back to the first of the month. Where am I going wrong?

You were close. I think this will work for you. The first date goes back 5 months and adds a day, the second goes to the previous month's last day.

CASE WHEN {trandate} BETWEEN
LAST_DAY(ADD_MONTHS({today},-5))+1 AND
LAST_DAY(ADD_MONTHS({today},-1)) THEN 1 ELSE 0 END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM