SQL/Redshift

Redshift : next_day() (다음 특정 요일의 날짜)

CosmosProject 2023. 9. 20. 23:34
728x90
반응형

 

 

 

Redshift의 next_day() 함수는 어떤 timestamp로부터 가장 가까운 미래의 어떤 요일의 날짜를 return해줍니다.

 

 

Syntax

next_day(timestamp, weekday)

 

- timestamp

기준이 되는 날짜시점입니다.

 

- weekday

가장 가까운 미래의 어떤 요일을 의미합니다.

 

 

 

예시로 보는게 더 이해가 빠를겁니다.

 

select  current_date  --> 2023-09-20
        , next_day(current_date, 'mon') as next_mon  --> 2023-09-25
        , next_day(current_date, 'tue') as next_tue  --> 2023-09-26
        , next_day(current_date, 'wed') as next_wed  --> 2023-09-27
        , next_day(current_date, 'thu') as next_thu  --> 2023-09-21
        , next_day(current_date, 'fri') as next_fri  --> 2023-09-22
        , next_day(current_date, 'sat') as next_sat  --> 2023-09-23
        , next_day(current_date, 'sun') as next_sun  --> 2023-09-24

 

current_date를 통해 현재 날짜를 얻습니다.

오늘 날짜는 2023-09-20입니다.

 

 

- next_day(current_date, 'mon')

current_date인 2023-09-20을 기준으로 가장 가까운 월요일(mon)의 날짜가 무엇인지를 return하라는 의미입니다.

2023년 9월 달력을 보면 2023-09-20 이후 가장 가까운 월요일은 2023-09-25입니다.

 

- next_day(current_date, 'tue')

current_date인 2023-09-20을 기준으로 가장 가까운 화요일(tue)의 날짜가 무엇인지를 return하라는 의미입니다.

2023년 9월 달력을 보면 2023-09-20 이후 가장 가까운 화요일은 2023-09-26입니다.

 

모두가 이런식입니다.

 

- next_day(current_date, 'fri')

current_date인 2023-09-20을 기준으로 가장 가까운 금요일(fri)의 날짜가 무엇인지를 return하라는 의미입니다.

2023년 9월 달력을 보면 2023-09-20 이후 가장 가까운 금요일 2023-09-22입니다.

 

 

 

next_day() 함수의 weekday 인자에는 위처럼 요일에 대한 이름이 들어갑니다.

위 예시에서는 요일을 나타내는 심볼로서 mon, tue, wed, thu, fri, sat, sun를 썼는데 사실 이것 말고도 다양한 심볼을 사용할 수 있습니다.

 

아래 표는 각 요일별로 사용할 수 있는 weekday symbol의 종류입니다.

 

Weekday Symbols
월요일 mon Mon m M monday Monday
화요일 tue Tue tu Tu tuesday Tuesday
수요일 wed Wed w W wednesday Wednesday
목요일 thu Thu th Th thursday Thursday
금요일 fri Fri f F friday Friday
토요일 sat Sat sa Sa saturday Saturday
일요일 sun Sun su Su sunday Sunday

 

 

 

 

 

 

728x90
반응형