[Reference] 초보자를 위한 SQL 200제


[SQL 문법]

문자 출력 시 특정 철자를 N개 만큼 채워서 출력할 수 있다.

SELECT LPAD(sal, 10, '*') as salary1, RPAD(sal, 10, '*') as salary2
  FROM emp

[예시]

# 오라클 연동 및 접속
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 LPAD(sal, 10, '*') as salary1, RPAD(sal, 10, '*') as salary2
  FROM emp
""")

row=cursor.fetchall()
colname=cursor.description
col=[]

for i in colname:
    col.append(i[0])

# pandas를 사용한 데이터 프레임 형식으로 변환
emp=pd.DataFrame(row,columns=col)
print(emp)

[결과]

       SALARY1     SALARY2
0   ******5000  5000******
1   ******2850  2850******
2   ******2450  2450******
3   ******2975  2975******
4   ******1250  1250******
5   ******1600  1600******
6   ******1500  1500******
7   *******950  950*******
8   ******1250  1250******
9   ******3000  3000******
10  *******800  800*******
11  ******3000  3000******
12  ******1100  1100******
13  ******1300  1300******

Leave a comment