Get the number of days in a month with to_char
SQL>
SQL> create table ord(
2 order_no integer primary key
3 ,cust_no integer
4 ,order_date date not null
5 ,total_order_price number(7,2)
6 ,deliver_date date
7 ,deliver_time varchar2(7)
8 ,payment_method varchar2(2)
9 ,emp_no number(3,0)
10 ,deliver_name varchar2(35)
11 ,gift_message varchar2(100)
12 );
Table created.
SQL>
SQL>
SQL> insert into ord(order_no,cust_no,order_date,total_order_price,deliver_date,deliver_time,payment_method,emp_no,deliver_name,gift_message)
2 values(1,1,'14-Feb-2002', 23.00, '14-Feb-2002', '12 noon', 'CA',1, null, 'Gift for wife');
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time ,payment_method ,emp_no,deliver_name ,gift_message )
2 values(2,1,'14-Feb-2003', 510.98, '14-feb-2003', '5 pm', 'NY',7, 'Rose Ted', 'Happy Valentines Day to Mother');
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
2 values(3, 2,'14-Feb-2004', 315.99, '14-feb-2004', '3 pm', 'VS',2, 'Ani Forest', 'Happy Valentines Day to Father');
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
2 values(4, 2,'14-Feb-1999', 191.95, '14-feb-1999', '2 pm', 'NJ',2, 'O. John', 'Happy Valentines Day');
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
2 values(5, 6,'4-mar-2002', 101.95, '5-mar-2002', '2:30 pm', 'MO' , 2, 'Cora', 'Happy Birthday from John');
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
2 values(6, 9,'7-apr-2003', 221.95, '7-apr-2003', '3 pm', 'MA', 2, 'Sake Keith', 'Happy Birthday from Joe' );
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
2 values(7, 9,'20-jun-2004', 315.95, '21-jun-2004', '12 noon', 'BC', 2, 'Jessica Li', 'Happy Birthday from Jessica');
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
2 values (8, 12, '31-dec-1999', 135.95, '1-jan-2000', '12 noon', 'DI', 3, 'Larry', 'Happy New Year from Lawrence');
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
2 values (9, 12, '26-dec-2003', 715.95, '2-jan-2004', '12 noon', 'SK',7, 'Did', 'Happy Birthday from Nancy' );
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
2 values(10, 4, sysdate-1, 119.95, sysdate+2, '6:30 pm', 'VG',2, 'P. Jing', 'Happy Valentines Day to Jason');
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
2 values(11, 2, sysdate, 310.00, sysdate+2, '3:30 pm', 'DC',2, 'C. Late', 'Happy Birthday Day to Jack');
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message)
2 values(12, 7, sysdate-3, 121.95, sysdate-2, '1:30 pm', 'AC',2, 'W. Last', 'Happy Birthday Day to You');
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message)
2 values(13, 7, sysdate, 211.95, sysdate-4, '4:30 pm', 'CA',2, 'J. Bond', 'Thanks for hard working');
1 row created.
SQL>
SQL> select order_no, 'Order placed on the ' ||
2 to_char(order_date, 'fmddth') || ' day of '||
3 to_char(order_date, 'fmMonth') || ', ' ||
4 to_char(order_date, 'yyyy') as "Order date"
5 from ord;
ORDER_NO Order date
---------- -----------------------------------------------
1 Order placed on the 14th day of February, 2002
2 Order placed on the 14th day of February, 2003
3 Order placed on the 14th day of February, 2004
4 Order placed on the 14th day of February, 1999
5 Order placed on the 4th day of March, 2002
6 Order placed on the 7th day of April, 2003
7 Order placed on the 20th day of June, 2004
8 Order placed on the 31st day of December, 1999
9 Order placed on the 26th day of December, 2003
10 Order placed on the 15th day of June, 2008
11 Order placed on the 16th day of June, 2008
12 Order placed on the 13th day of June, 2008
13 Order placed on the 16th day of June, 2008
13 rows selected.
SQL>
SQL> drop table ord;
Table dropped.
SQL> --
Related examples in the same category
1. | format parameter options available | | |
2. | Change the original date format in the TO_CHAR function | | |
3. | to_char( sysdate, 'HH24:MI:SS' ) | | |
4. | select to_char( sysdate, 'DD/MM/YY HH24:MI:SS' ) "Right Now" | | |
5. | select to_char(sysdate,'DD-MON-YY HH24:MI:SS' ) "Right Now" | | |
6. | Use to_char to format a date type column | | |
7. | Combine to_char and trunc with date value | | |
8. | to_char(order_date, 'hh24:mi:ss') | | |
9. | Use string returned to_char from date type column in where clause | | |
10. | Converting a DATE to Another Language | | |
11. | SELECT TO_CHAR(SYSDATE, 'MM-DD-YYYY HH24:MI:SS') NOW | | |
12. | TO_CHAR(SYSDATE, 'MONTH DDTH') | | |
13. | TO_CHAR(SYSDATE, 'MONTH DDSP') | | |
14. | TO_CHAR(SYSDATE, 'MONTH DDSPTH') | | |
15. | INITCAP(RTRIM(TO_CHAR(SYSDATE, 'MONTH'))) ||' ' ||TO_CHAR(SYSDATE, 'DDTH') | | |
16. | initcap(rtrim(to_char(sysdate, 'MONTH'))) ||' ' || initcap(to_char(sysdate, 'DDSPTH')) | | |
17. | INITCAP(RTRIM(TO_CHAR(SYSDATE, 'MONTH'))) | | |
18. | to_char(LAST_STOCK_DATE, 'MON DD, YYYY HH24:MI') | | |