1
Below I have the following code where I search in a table all the data within the specified period in the declared variables. However, a specific client has a different month closure, in this case, his month starts on the 26th, and ends on the 25th.
I’m trying to make the variables bring this period but without success!
In this case, the variable @periodoInicial
would have to start at 2017-07-26
and the variable @periodoFinal
in 2017-08-25
, and at the end of this period, the variables have already changed to 2017-08-26
until 2017-09-25
, it is possible to do this?
Code:
declare @periodoInicial as date,
@periodoFinal as date
set @periodoInicial = cast(dateadd(mm,-1,dateadd(dd,-day(getdate())+1,getdate())) as date)
set @periodoFinal = cast(dateadd(dd,-day(getdate()),getdate()) as date)
SELECT * FROM productionEnvironmentInvoiceData
where proposalDate between @periodoInicial and @periodoFinal
NOTE: I AM WITH SQL SERVER 2008
did not understand the " ending this period, the variables already changed to 2017-08-26 until 2017-09-25"
– Rovann Linhalis
In fact a dia_de_closure column should be created in the client table, get this "day" and based on it do the calculation.
– Motta
@Rovannlinhalis for example, the month in this case is not from day 01 until the 30 or 31, but starts on day 26 and ends in the 25, understands?
– João Vitor
@Motta you say put a column 'periodoInicial' that repeats 26 in all lines and another with 26 'periodoFinal' and follow the calculation of the variables giving select in this table?
– João Vitor
No, I mean column of event in the enterprise table sql would look something like SELECT * FROM productionEnvironmentInvoiceData , COMPANIES Where productionEnvironmentInvoiceData.CODEMPESA = COMPANIES.CODEMPESA AND proposalDate between CONVERT(DATETIME, ANO + MES-1 + diadefechamento+1) and CONVERT(DATETIME, ANO + MES + diadefechamento)
– Motta
@jvbarsou, only one question, in the table productionEnvironmentInvoiceData the field is
Date
orDatetime
? If it isDatetime
, interesting to use from day 26 to day 26, why being the final date 2017-08-26, there will be the question of hours and minutes.– Ricardo Souza
How’s your client chart?
– Sorack
@Ricardosouza the field is
date
– João Vitor
What do you mean? @Sorack
– João Vitor
@jvbarsou you get the day by customer registration, it is not?
– Sorack
@Sorack if I understood well what you meant, no, in the true I’m in the variable even the period because I will make a process in specific for this type of client. So much so that this table is just data from them.
– João Vitor
@jvbarsou you know the version of your SQL Server?
– Sorack
@Sorack in case I am using sql server 2008
– João Vitor