1

I have a field date which is a date format.

It outputs like this for example.

09-NOV-14

Now what i want the output to be is any date rolled back to the 1st of its month.

01-NOV-14

What i did to achieve this is

CONCAT('01',SUBSTR(table.date,3))

Is this the most efficient/best practice way of doing this?

Matt
  • 14,906
  • 27
  • 99
  • 149

2 Answers2

3

moved from comment to answer:

trunc(date,'month')
Thomas Krojer
  • 1,018
  • 7
  • 9
2

The output format you get when a date is cast to string is configurable:

SQL> SELECT SYSDATE FROM DUAL;

SYSDATE
---------
26-NOV-14

SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';

Session altered.

SQL> SELECT SYSDATE FROM DUAL;

SYSDATE
-------------------
2014-11-26 16:15:44

... thus your approach is not particularly robust. To use date functions (rather than string manipulation) please check Make date time's first day of its month.

Community
  • 1
  • 1
Álvaro González
  • 142,137
  • 41
  • 261
  • 360