简体   繁体   中英

convert timestamp with timezone to day of week

table name: test

column name | datatype

order_date | timestamp w/ timezone (Eg: '2019-01-20 23:59:10+08')

looking to convert the timestamp to day of the week example: 2019-01-20 = 'Sunday'

You can use EXTRACT to extract the day of week as an integer and then convert it to a string:

SELECT
    CASE EXTRACT(DOW FROM order_date AT TIME ZONE 'UTC')
        WHEN 0 THEN 'Sunday'
        WHEN 1 THEN 'Monday'
        ...
    END
FROM test

You can use the following function which will return the day of week of a given date

create function day_of_week(@d as date) returns varchar(20)
as
begin
declare @day_of_week as varchar(20)
select @day_of_week =case datepart(w,getdate())
when 1 then 'Sunday'
when 2 then 'Monday'
when 3 then 'Tuesady'
when 4 then 'Wednasday'
when 5 then  'Thursday'
when 6 then  'Friday'
when 7 then 'Saturday'
end 
return @day_of_week
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM