Pick up day of the week in a select

Asked

Viewed 235 times

2

I need to know how to get inside a select (with LEFT JOIN, INNER JOIN) take the day of the week from a field of a table, compare and see which day of the week, if it is 1 add 2, if it is 7 add 1?

Example:

case when datepart (weekday, campo.da.tabela) = 1 then campo.tabela+2 when (datepart (weekday, campo.da.tabela) = 7 then campo.tabela+ 1

but that way it’s not working, I don’t know how to ride.

In SQL SERVER.

  • Hello @Rafael. Your question is unclear. Can you elaborate further? The field campo.tabela is whole? The week starts on Sunday or Monday?

  • Welcome to Stackoverflow in English. I edited your question to remove the greetings as we usually keep the text as clean as possible to focus on your scheduling question. If you are interested in visiting a part of the site that is not aimed to ask questions can know the [chat]. If you have questions about the operation, rules and procedures of the site visit the [meta] :)

1 answer

1

Not knowing well the objective and structure of the table, considering that the column campo.tabela is a whole and the beginning of the week begins at the Domingo, may consult the Commission as follows::

SELECT  campo.tabela = campo.tabela + (CASE DATEPART(WEEKDAY, campo.da.tabela)
        WHEN 1 THEN 2
        WHEN 7 THEN 1
        ELSE 0
        END)
FROM    Tabela

If you want to change the beginning of the week to Monday, just include this command at the beginning of your query:

SET DATEFIRST 1

This will, of course, have an impact on their results, in particular on the number allocated on the day of the week of the date in question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.