Run the following query:
UPDATE CPSCli
SET FimValidPre = DATEADD(s, -1, DATEADD(mm, DATEDIFF(m, 0, FimValidPre) + 1, 0))
Explaining the operation of the query:
DATEDIFF(m, 0, FimValidPre)
DATEDIFF: We’re taking through the parameter m (datepart representing months), the number of months from 1900 (last year of research accepted in SQL) to our date Fimvalidpre. So if for example our date is 03/08/2018 will be returned 1423.
DATEADD(mm, DATEDIFF(m, 0, FimValidPre) + 1, 0)
DATEADD: This function has the function of summing dates, however we are saying to add using the months as reference (because of mm as parameter). In the second parameter we are passing the function result DATEDIFF that is 1423. We’re adding up +1 to seek the next month after our current field month Fimvalidpre (which in our example is the date 03/08/2018) then our current month is 08 - August and the next would be 09 - September. The last parameter we are using to catch the first day of next month, which would be 01/09/2018.
DATEADD(s, -1, DATEADD(mm, DATEDIFF(m, 0, FimValidPre) + 1, 0))
DATEADD: With the result we possess 01/09/2018 00:00:00.000, we are passing as parameter the seconds s, and say to subtract a second -1 of this our result. From this understanding, we will obtain the expected result which is the last day of the month at 23:59:59 hours 2018-08-31 23:59:59.000.
If you need to filter by any specific criteria, be sure to add the clause WHERE in the query.
Set: "It’s Off!". It is necessary to add the error message obtained so that we can help you.
– William John Adam Trindade
sorry... corrected now! I will reset the question...
– Reculos Gerbi Neto
you could give an example of your table data for us to simulate here ;)
– Leonardo Bonetti
I’m sorry, I don’t understand... what’s the difference between DATE_FORMAT and ELSE? why in case it is running ELSE, since the registration date was 4 and was updated to 30!
– Reculos Gerbi Neto
Answer your question with your solution, it’s much better than the answer being in the question, right?
– Melissa