[SQL]-(029) 특정 날짜가 있는 달의 마지막 날짜 출력하기
[Reference] 초보자를 위한 SQL 200제
[SQL 문법]
특정 날짜가 있는 달의 마지막 날짜를 출력할 수 있다.
특정 날짜 이후 다음 달까지의 남은 날을 출력할 수 있다.
SELECT '2019/05/22' as 날짜, LAST_DAY('2019/05/22') as "마지막 날짜", LAST_DAY(SYSDATE) - SYSDATE as "남은 날짜"
FROM dual
[예시]
# 오라클 연동 및 접속
import pandas as pd
import cx_Oracle
dsn=cx_Oracle.makedsn('localhost',1521,'orcl')
db=cx_Oracle.connect('scott','tiger')
cursor=db.cursor()
# SQL 문법
cursor.execute("""
SELECT '2019/05/22' as 날짜, LAST_DAY('2019/05/22') as "마지막 날짜", LAST_DAY(SYSDATE) - SYSDATE as "남은 날짜"
FROM dual
""")
row=cursor.fetchall()
colname=cursor.description
col=[]
for i in colname:
col.append(i[0])
# pandas를 사용한 데이터 프레임 형식으로 변환
emp=pd.DataFrame(row,columns=col)
print(emp)
[결과]
날짜 마지막 날짜 남은 날짜
0 2019/05/22 2019-05-31 12
Leave a comment