Search code examples
sqlpostgresqldateselecttruncate

postgresql query to return the day of the week of the first day of the month two years from today


I used postgresql to solve the quesion, query to return the day of the week of the first day of the month two years from today. I was able to solve it with the query below, but I am not sure my query is correct, I just wanna make sure

select cast(date_trunc('month', current_date + interval '2 years') as date)


Solution

  • You are correctly computing the first day of the month two years later with:

    date_trunc('month', current_date + interval '2 years')
    

    If you want the corresponding day of the week, you can use extract();

    extract(dow from date_trunc('month', current_date + interval '2 years'))
    

    This gives you an integer value between 0 (Sunday) and 6 (Saturday)