Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle, Make date time's first day of its month

Tags:

sql

oracle

I have a datevariable, I would like to have convert it to first day of its monh,

  • Eg: 10/10/2010 -> 01/10/2010
  • Eg: 31/07/2010 -> 01/07/2010
like image 602
Bilgin Kılıç Avatar asked Oct 21 '11 14:10

Bilgin Kılıç


People also ask

How do you get the 1st day of the month in Oracle?

Since SYSDATE provides the current date and time, using it as the date parameter for the TRUNC() function and a MM (Month) format model returns the first day of the month because TRUNC() rounds down. First day of the current month. The last day of the current month.

How do I get the first day of the month in SQL?

Here's how this works: First we format the date in YYYYMMDD... format truncating to keep just the 6 leftmost characters in order to keep just the YYYYMM portion, and then append '01' as the month - and voila! you have the first day of the current month.

How do you get the first and last day of the previous month in Oracle?

trunc(sysdate,'MONTH') = first day. last_day(sysdate) = last day.

How do you get the first day of the current year in Oracle?

Here's one way to find the first and last day of the current year: SELECT TRUNC (SYSDATE, 'YEAR') AS first_day, ADD_MONTHS ( TRUNC (SYSDATE, 'YEAR') , 12 ) - 1 AS last_dayFROM dual; You can substitute any DATE expression for SYSDATE to get the first or last day of the year that contains that DATE.


2 Answers

According to http://psoug.org/reference/date_func.html, this should work a dandy...

SELECT TRUNC(yourDateField, 'MONTH') FROM yourTable 
like image 147
MatBailie Avatar answered Sep 20 '22 00:09

MatBailie


SQL> select to_date('31/07/2010', 'DD/MM/YYYY') from dual;  TO_DATE(' --------- 31-JUL-10  SQL> select trunc(to_date('31/07/2010', 'DD/MM/YYYY'), 'MM') from dual;  TRUNC(TO_ --------- 01-JUL-10  SQL> 
like image 23
BQ. Avatar answered Sep 22 '22 00:09

BQ.