-- DUAL : 가상테이블
SELECT 1 FROM DUAL;
-- 문자 함수
-- CHR(N) : ASCII 값을 문자로 변환
SELECT CHR(65) FROM DUAL;
SELECT CHR(112) FROM DUAL;
SELECT
'내 점수는 ' || CHR(97) || '입니다'
FROM DUAL;
-- LPAD(RPAD) : 나머지 빈칸을 특정 문자로 채운다
SELECT LPAD('BBB', 10) FROM DUAL;
SELECT LPAD('BBB', 10, '-') FROM DUAL;
SELECT RPAD('BBB', 10) FROM DUAL;
SELECT RPAD('BBB', 10, '-') FROM DUAL;
-- INSTR : indexOf
SELECT INSTR('A12345657A', 'A')
FROM DUAL;
-- 숫자 이후(포함)에 나오는 위치
SELECT INSTR('A12345657A', 'A', 3)
FROM DUAL;
SELECT INSTR('ABCABCABCABC', 'B', 6)
FROM DUAL;
-- REPLACE : 문자열 치환
SELECT REPLACE('AAAAAAAAAYYYSDHF', 'A')
FROM DUAL;
SELECT REPLACE('AAAAAAAAAYYYSDHF', 'AA', 'a')
FROM DUAL;
-- TRANSLATE : 문자 치환
SELECT TRANSLATE('AAAAAAAAAYYYSDHF', 'A', 'a')
FROM DUAL;
SELECT TRANSLATE('AAAAAAAAAYYYSDHF', 'AA', 'a')
FROM DUAL;
-- 숫자 함수
-- 올림
SELECT CEIL(13.1) FROM DUAL; -- 14
-- 내림
SELECT FLOOR(13.5) FROM DUAL; -- 13
-- 나눈 나머지 MOD
SELECT MOD(9, 4) FROM DUAL;
-- 승수
SELECT POWER(2, 3) FROM DUAL;
-- 반올림
SELECT ROUND(13.5) FROM DUAL; -- 14
SELECT ROUND(13.4) FROM DUAL; -- 13
-- 버림
SELECT TRUNC(13.12231) FROM DUAL;
SELECT TRUNC(13.12231, 3) FROM DUAL; -- 13.122
SELECT TRUNC(13.12231, -1) FROM DUAL;
-- CHR 함수의 반대
SELECT ASCII('A') FROM DUAL;
-- 변환 함수
-- TO_CHAR DATE -> STRING
SELECT TO_CHAR(SYSDATE) FROM DUAL;
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD-HH24-MI-SS') FROM DUAL;
SELECT TO_CHAR(100000000, '999,999,999') FROM DUAL;
-- TO_DATE STRING -> DATE
SELECT TO_DATE('20180618') FROM DUAL;
SELECT TO_DATE('20180618', 'YYYYMMDD') FROM DUAL;
SELECT TO_DATE('06182018', 'MMDDYYYY') FROM DUAL;
-- parseInt()
SELECT TO_NUMBER('100') + 1 FROM DUAL;
-- 해당하는 월의 마지막 일자
SELECT LAST_DAY(SYSDATE) FROM DUAL;
SELECT LAST_DAY(TO_DATE('2020-02-02', 'YYYY-MM-DD'))
FROM DUAL;
-- substring
SELECT SUBSTR('ABCDEFG', 3) FROM DUAL;
SELECT SUBSTR('ABCDEFG', 3, 2) FROM DUAL;
SELECT LENGTH('ABCDE') FROM DUAL;
--------------------------------------------------------------------------------------------------------------
SELECT * FROM EMPLOYEES; -- Query문
select * from employees;
SELECT * FROM EMP;
-- 한줄 주석문
/*
범위 주석문
*/
-- 자료형
-- 문자 자료형 CHAR char
-- 문자열 자료형 varchar2 VARCHAR2 == String
-- CHAR
CREATE TABLE TB_CHAR(
COL_CHAR1 CHAR(10 BYTE),
COL_CHAR2 CHAR(10 CHAR),
COL_CHAR3 CHAR(10) -- BYTE
);
/*
CREATE TABLE 테이블명(
컬럼명 자료형(크기),
컬럼명 자료형(크기),
컬럼명 자료형(크기)
)
*/
CREATE TABLE TB_CHAR1(
컬럼1 CHAR(10 BYTE),
컬럼2 CHAR(10 CHAR),
컬럼3 CHAR(10)
);
INSERT INTO TB_CHAR(COL_CHAR1, COL_CHAR2, COL_CHAR3)
VALUES('ABC', 'ABC', 'ABC'); -- 영문 1BYTE
INSERT INTO TB_CHAR(COL_CHAR1, COL_CHAR2, COL_CHAR3)
VALUES('가나다', '가나다', '가나다'); -- 한글 3BYTE
SELECT * FROM TB_CHAR;
SELECT COL_CHAR1, COL_CHAR2, COL_CHAR3,
LENGTHB(COL_CHAR1), LENGTHB(COL_CHAR2), LENGTHB(COL_CHAR3)
FROM TB_CHAR;
/*
CHAR : 10 BYTE 00000 00000
ABC ABC00 00000
가 - 3 + 9 = 12 가0000 00000
가나 - 3 + 3 + 8 = 14 가나000 00000
가나다 - 3 + 3 + 3 + 7 = 16 가나다00 00000
*/
-- VARCHAR2 == 문자열
CREATE TABLE TB_VARCHAR(
COL_VARCHAR1 VARCHAR2(10 BYTE),
COL_VARCHAR2 VARCHAR2(10 CHAR),
COL_VARCHAR3 VARCHAR2(10)
);
INSERT INTO TB_VARCHAR(COL_VARCHAR1, COL_VARCHAR2, COL_VARCHAR3)
VALUES('ABC', 'ABC', 'ABC');
INSERT INTO TB_VARCHAR(COL_VARCHAR1, COL_VARCHAR2, COL_VARCHAR3)
VALUES('가나다', '가나다', '가나다');
SELECT COL_VARCHAR1, COL_VARCHAR2, COL_VARCHAR3,
LENGTHB(COL_VARCHAR1), LENGTHB(COL_VARCHAR2), LENGTHB(COL_VARCHAR3)
FROM TB_VARCHAR;
-- LONG 최대 2GB까지 저장 가능
CREATE TABLE TB_LONG(
-- COL_LONG1 LONG,
COL_LONG2 LONG
);
INSERT INTO TB_LONG(COL_LONG2)
VALUES('ABC');
INSERT INTO TB_LONG(COL_LONG2)
VALUES('가나다');
SELECT COL_LONG2, LENGTHB(COL_LONG2) -- LENGTHB 사용못함
FROM TB_LONG;
-- 숫자 자료형 NUMBER(정수, 실수)
CREATE TABLE TB_NUMBER(
컬럼1 NUMBER,
컬럼2 NUMBER(5),
컬럼3 NUMBER(5, 2),
컬럼4 NUMBER(*, 2)
);
INSERT INTO TB_NUMBER(컬럼1, 컬럼2, 컬럼3, 컬럼4)
VALUES(1234.56789, 12345, 123.456, 1234.56789);
SELECT
*
FROM TB_NUMBER;
-- 날짜 자료형
CREATE TABLE TB_DATE(
COL_DATE1 DATE,
COL_DATE2 DATE
);
INSERT INTO TB_DATE(COL_DATE1, COL_DATE2)
VALUES(SYSDATE, TO_DATE('2018-06-20 10:25:30', 'YYYY-MM-DD HH24:MI:SS' ) );
SELECT * FROM TB_DATE;
INSERT INTO TB_DATE(COL_DATE1, COL_DATE2)
VALUES(SYSDATE - 1, SYSDATE - (5/24));
DROP TABLE TB_CHAR;
DROP TABLE TB_CHAR1;
DROP TABLE TB_VARCHAR;
DROP TABLE TB_LONG;
DROP TABLE TB_NUMBER;
DROP TABLE TB_DATE;
'Oracle Database > SQL' 카테고리의 다른 글
SQL 쿼리문 기본 (0) | 2018.06.26 |
---|---|
SQL 쿼리 그룹 (0) | 2018.06.26 |
SQL 쿼리 정렬 (0) | 2018.06.26 |
SQL명령어 (0) | 2018.06.26 |
Oracle Database (SELECT - WHERE)문 (0) | 2018.06.20 |