[오라클] 날짜함수, 변환함수
본문 바로가기
IT. 컴퓨터

[오라클] 날짜함수, 변환함수

by 솔기잇 2020. 6. 18.

 

날짜함수

SELECT  EXTRACT(YEAR FROM SYSDATE),    --년도만

        EXTRACT(MONTH FROM SYSDATE),   --달만

        EXTRACT(DAY FROM SYSDATE)  -- 일만

FROM DUAL;

 

<<결과>>

  --> 2010           11           25

 

            

변환함수

1. 날짜 또는 숫자를 문자로 변환시켜주는 함수 : TO_CHAR

 

SELECT SYSDATE,

       TO_CHAR(SYSDATE, 'YYYY'),

       TO_CHAR(SYSDATE, 'YEAR'),

       TO_CHAR(SYSDATE, 'MM'),

       TO_CHAR(SYSDATE, 'MONTH'), --

       TO_CHAR(SYSDATE, 'MON'), --

       TO_CHAR(SYSDATE, 'D'), -- 그주의 몇번째인가(목요일이면:5)

       TO_CHAR(SYSDATE, 'DD'), --일수

       TO_CHAR(SYSDATE, 'DDD'), --그해부터 지금까지의 날수

       TO_CHAR(SYSDATE, 'DAY'), --

       TO_CHAR(SYSDATE, 'DY'),

       TO_CHAR(SYSDATE, 'HH24'),

       TO_CHAR(SYSDATE, 'HH AM'),

       TO_CHAR(SYSDATE, 'HH PM'),

       TO_CHAR(SYSDATE, 'MI'), --

       TO_CHAR(SYSDATE, 'SS'), --

       TO_CHAR(SYSDATE, 'SSSSS'), -- 하루의 초

       TO_CHAR(SYSDATE, 'Q') --분기

FROM DUAL;

 

<<결과>>

--> 2010-11-25 오전 11:43:23             2010    TWENTY TEN     11         11월      11월      5           25             329       목요일    목          11         11 오전 11 오전 43         23         42203  4

 

----------------------------------------------------------------------------------------- 

SELECT NAME,

      TO_CHAR(HIREDATE, 'YYYY-MM-DD'),

      TO_CHAR(HIREDATE, 'YYYY')||'' ||

      TO_CHAR(HIREDATE, 'MM')||'' ||

      TO_CHAR(HIREDATE, 'DD')||'' AS "입사일자"

FROM TBL_NEWSAWON;  

 

<<결과>>   

NAME    TO_CHAR(HIREDATE,'YYYY-MM-DD')    입사일자

한석규         1990-01-02                               19900102

석규            1992-01-03                               19920103

세석한         1994-01-04                               19940104

네석규         1996-01-05                               19960105

오연수         1996-01-06                               19960106

일용이         2000-01-07                               20000107

이미자         2001-01-08                               20010108

삼미자         2002-01-09                               20020109

사미자         2003-01-10                              20030110

고소영         1999-01-01                              19990101

노주현         1999-01-02                              19990102

도지원         2001-01-03                             20010103

류시원         2002-01-04                             20020104

김석           2003-01-05                              20030105

박찬호        2004-01-06                                 20040106

----------------------------------------------------------------------------------------------- 

 

SELECT NAME, SAL,

            TO_CHAR(SAL),

            TO_CHAR(SAL, '$99,999'),  -- 자리를 정리해 주는 역할

            LTRIM( TO_CHAR(SAL, 'L99,999') ),  

            LTRIM( TO_CHAR(SAL, 'L09,999') ),

            LTRIM( TO_CHAR(SAL, 'L00,000') ),

            LTRIM( TO_CHAR(SAL, 'L99,999.00') )

FROM TBL_NEWSAWON;

 

<<결과>>

한석규    5000    5000      $5,000             ₩5,000          ₩05,000             ₩05,000 ₩5,000.00

 

------------------------------------------------------------------------------------------------------------------

 

SELECT 2936.986,

            2936.984,

            936,

 

            TO_CHAR(2936.986, '0999.00'),

            TO_CHAR(2936.984, '0999.00'),

            TO_CHAR(936, '0999.00'),

 

            TO_CHAR(2936.986, '9999.99'),

            TO_CHAR(2936.984, '9999.99'),

            TO_CHAR(936, '9999.99')

 

<<결과>>

--> 2936.986   2936.984          936       2936.99           2936.99           0936.00             2936.99           2936.98              936.00

 

------------------------------------------------------------------------------------------------------------------- 

 

 

2. 문자를 날짜로 변환시켜주는 함수 : TO_DATE

 

3. 문자를 숫자로 변환시켜주는 함수 : TO_NUMBER

 

SELECT '000327', TO_NUMBER('000327')

FROM DUAL;

--> 000327       327

 


close