[Database] SQL 날짜함수

정현명·2022년 3월 16일
0

Database

목록 보기
9/17
post-thumbnail

[Database] SQL 날짜함수


  • now, sysdate, current_timestamp : 현재 날짜와 시간
-- now() or sysdate() or current_timestamp() : 현재 날짜와 시간 리턴
-- 2022-03-16 01:06:43	2022-03-16 01:06:43	2022-03-16 01:06:43
select now(), sysdate(), current_timestamp()
from dual;

  • curdate, current_date : 현재 날짜 , curtime, current_time : 현재 시간
-- curdate() or current_date(): 현재 날짜 리턴
-- curtime() or current_time() : 현재 시간 리턴
-- 2022-03-16	2022-03-16	01:06:35	01:06:35
select curdate(), current_date(), curtime(), current_time()
from dual;

  • date_add : 날짜 연산
-- date_add(날짜, INTERVAL 기준 값) : 날짜에서 기준 값만큼 더한다.
-- date_sub(날짜, INTERVAL 기준 값) : 날짜에서 기준 값만큼 뺀다.
-- 2022-03-16 01:06:27	2022-03-16 01:06:32	2022-03-16 06:06:27	2022-03-21 01:06:27
select now() 현재시간, date_add(now(), interval 5 second) 5초후,
	   date_add(now(), interval 5 hour) 5시간후, date_add(now(), interval 5 day) 5일후
from dual;

  • 년, 월, 주, 요일 등
-- year(날짜) : 날짜의 연도 리턴
-- month(날짜) : 날짜의 월 리턴
-- monthname(날짜) : 날짜의 월을 영어로 리턴
-- dayname(날짜) : 날짜의 요일을 영어로 리턴
-- dayofmonth(날짜) : 날짜의 월별 일자 리턴
-- dayofweek(날짜) : 날짜의 주별 일자 리턴 [일요일(1), 월요일(2), ..., 토요일(7)]
-- weekday(날짜) : 날짜의 주별 일자 리턴 [월요일(0), 화요일(1),...,일요일(6)]
-- dayofyear(날짜) : 일년을 기준으로 한 날짜까지의 일수(365일 중 x일)
-- week(날짜) : 일년 중 몇 번째 주
-- 2022	3	March	Wednesday	16	4	2	75	11
select year(now()), month(now()), monthname(now()), 
       dayname(now()), dayofmonth(now()), dayofweek(now()), 
	   weekday(now()), dayofyear(now()), week(now())
from dual;

  • 날짜 포맷
-- date_format(날짜, '형식') : 날짜를 형식에 맞게 리턴
	-- 날짜 형식
    -- %Y 년, 2022
    -- %y 년, 22
    -- %b 월, Jan,..,Dec
    -- %M 월, January..December
    -- %m 월, 01,02, ..., 11, 12
    -- %d 일, 01,...,30,31
    -- %e 일, 1, 2,..., 30,31
    -- %a 요일, sun...sat
    -- %W 요일, Sunday, ... Saturday
    -- %w 요일, 0 : 일요일, ..., 6: 토요일
    -- %p AM or PM
    -- %H 시간, 00, 01, 02, ...22,23
    -- %h 시간, 01,02, .., 11, 12
    -- %I 시간, 01,02, .., 11, 12
    -- %k 시간, 0,1,2,...,22,23
    -- %l 시간, 1,2,...,11,12
    -- %i 분, 00..59
    -- %S 초, 00..59
    -- %s 초, 00..59
    -- %T 시간, 24-houer(hh:mm:ss)
    -- %j 1년중 X일, 001,...365)
-- 2022-03-16 01:05:54	2022 March 16 AM 1 05 54	22-03-16 01:05:54	22.03.16 Wednesday	01시05분54초	01:05:54	075
select now(), date_format(now(), '%Y %M %e %p %l %i %S'), date_format(now(), '%y-%m-%d %H:%i:%s'),
	   date_format(now(), '%y.%m.%d %W'), date_format(now(), '%H시%i분%s초'), date_format(now(),'%T'),date_format(now(),'%j') 
from dual;
profile
꾸준함, 책임감

0개의 댓글