1

I want to get the last date of the previous month.

Something like this:

FunctionName(10.02.2011) Result should be 31.01.2011

In oracle we can do it using the last_day() function as suggested in this question question. Is there a similar function to do the same in Netezza Database.

Community
  • 1
  • 1
heretolearn
  • 6,387
  • 4
  • 30
  • 53

5 Answers5

4

last_day() exists in Netezza (not sure which version we're on)

select extract(day from last_day(to_date('20121001','YYYYMMDD') - interval '1 month' ));
 DATE_PART 
-----------
        30
Mike
  • 47,263
  • 29
  • 113
  • 177
1

For Netezza, it would use add_month() and last_day() functions to get the result. Take '2013-01-01' for example.

select last_day(add_month('2013-01-01',-1));

Result: 2013-12-31

Alan
  • 37
  • 7
0

I don't have Netezza available, but the following might work:

select <date> - datepart('day', <date>) - 1

This subtracts the current day of the month from the date, and then one more day to go back to the last day of the previous month.

You can do this also with:

select date(current_Date) - datepart('day', current_Date) - 1

for the current date.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

Similar to Gordon's answer, I've tested this on NZ 6 and it works to get the last day of the previous month by subtracting the current day # from the current date. You can replace current_timestamp with your date field.

select current_timestamp, current_timestamp - extract(day from current_timestamp)

Results:

2012-10-05 13:35:05 2012-09-30 00:00:00
N West
  • 6,768
  • 25
  • 40
0

select extract(day from (select (last_day(to_char(current_timestamp,'dd/mm/yyyy')))))

Mariappan Subramanian
  • 9,527
  • 8
  • 32
  • 33