[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] Lv2. DATETIME์—์„œ DATE๋กœ ํ˜• ๋ณ€ํ™˜ [String, Date]

2022. 9. 26. 08:18ใ†์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค_2022

728x90

 

๋ฌธ์ œ ์„ค๋ช…

ANIMAL_INS ํ…Œ์ด๋ธ”์€ ๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๋™๋ฌผ์˜ ์ •๋ณด๋ฅผ ๋‹ด์€ ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค. ANIMAL_INS ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ, ANIMAL_ID, ANIMAL_TYPE, DATETIME, INTAKE_CONDITION, NAME, SEX_UPON_INTAKE๋Š” ๊ฐ๊ฐ ๋™๋ฌผ์˜ ์•„์ด๋””, ์ƒ๋ฌผ ์ข…, ๋ณดํ˜ธ ์‹œ์ž‘์ผ, ๋ณดํ˜ธ ์‹œ์ž‘ ์‹œ ์ƒํƒœ, ์ด๋ฆ„, ์„ฑ๋ณ„ ๋ฐ ์ค‘์„ฑํ™” ์—ฌ๋ถ€๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

 

NAME                                                                                   TYPE                                                      NULLABLE
ANIMAL_ID VARCHAR(N) FALSE
ANIMAL_TYPE VARCHAR(N) FALSE
DATETIME DATETIME FALSE
INTAKE_CONDITION VARCHAR(N) FALSE
NAME VARCHAR(N) TRUE
SEX_UPON_INTAKE VARCHAR(N) FALSE

ANIMAL_INS ํ…Œ์ด๋ธ”์— ๋“ฑ๋ก๋œ ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ์— ๋Œ€ํ•ด, ๊ฐ ๋™๋ฌผ์˜ ์•„์ด๋””์™€ ์ด๋ฆ„, ๋“ค์–ด์˜จ ๋‚ ์งœ1๋ฅผ ์กฐํšŒํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. ์ด๋•Œ ๊ฒฐ๊ณผ๋Š” ์•„์ด๋”” ์ˆœ์œผ๋กœ ์กฐํšŒํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์‹œ

์˜ˆ๋ฅผ ๋“ค์–ด, ANIMAL_INS ํ…Œ์ด๋ธ”์ด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค๋ฉด

ANIMAL_INS

ANIMAL_ID         ANIMAL_TYPE      DATETIME                         INTAKE_CONDITION NAME  SEX_UPON_INTAKE
A349996 Cat 2018-01-22 14:32:00 Normal Sugar Neutered Male
A350276 Cat 2017-08-13 13:50:00 Normal Jewel Spayed Female
A350375 Cat 2017-03-06 15:01:00 Normal Meo Neutered Male
A352555 Dog 2014-08-08 04:20:00 Normal Harley Spayed Female
A352713 Cat 2017-04-13 16:29:00 Normal Gia Spayed Female

SQL๋ฌธ์„ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค.

ANIMAL_ID                                              NAME                                       ๋‚ ์งœ
A349996 Sugar 2018-01-22
A350276 Jewel 2017-08-13
A350375 Meo 2017-03-06
A352555 Harley 2014-08-08
A352713 Gia 2017-04-13

 

๋‚˜์˜ ํ’€์ด [ORACLE]

SELECT ANIMAL_ID, NAME, TO_CHAR(DATETIME, 'yyyy-mm-dd') AS ๋‚ ์งœ FROM ANIMAL_INS ORDER BY ANIMAL_ID;

์˜ค๋ผํด์—์„œ DATETIME์„ 'YYYY-MM-DD' ํ˜•์‹์œผ๋กœ ์กฐํšŒํ•˜๋Š”๊ฒŒ ์ด๋ฒˆ ๋ฌธ์ œ๊ฐ€ ์š”๊ตฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. 

MySQL ํ•จ์ˆ˜๊ฐ€ ์ƒ๊ฐ์ด ์•ˆ๋‚˜์„œ ใ…‹ใ…‹ใ…‹์ด๋ฒˆ์—๋Š” ORACLE๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค. 

 

TO_CHAR ํ•จ์ˆ˜๋กœ DATETIME์„ 'YYYY-MM-DD' ํ˜•์‹์œผ๋กœ ๋งž์ถ”๊ณ , AS ์ปฌ๋Ÿผ๋ช…์„ '๋‚ ์งœ'๋กœ ํ•ด์„œ ์›ํ•˜๋Š” ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

๊ธ‰ MySQL์—์„œ๋Š” ์–ด๋–ค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋‚˜ ์ฐพ์•„๋ดค๋”๋‹ˆ DATE_FORMAT ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฒƒ ๊ฐ™๋‹ค. 

 

๋‚˜์˜ ํ’€์ด [MySQL]

SELECT ANIMAL_ID, NAME, DATE_FORMAT(DATETIME, '%Y-%m-%d') AS ๋‚ ์งœ FROM ANIMAL_INS ORDER BY ANIMAL_ID;

๊ทธ๋ฆฌ๊ณ  %Y ๊ฐ€ ๋…„๋„ 4์ž๋ฆฌ์ˆ˜์ด๊ณ , %m์ด ์›” %d๊ฐ€ ์ผ ์„ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์ด์—ˆ๋‹ค..! 

 

 

 

 

 

 

 

 

 

 

 

 

728x90