Validate data in sql server

Asked

Viewed 893 times

0

I get a date, and I need to know if it’s valid, if it’s not, I need to shorten her day until it’s valid.

set @data = (select convert(date,CONCAT(@dia,'-',month(@vencimento1),'-',year(@vencimento1))))
set @vencimento = (select DATEADD(month, 1, @data))

I tried something like:

if (select isdate(DATEADD(month, 1, @data))) = 1 
    set @vencimento = (select DATEADD(month, 1, @data))
else
    set @vencimento = (select dateadd(day, -1, @data), DATEADD(month, 1, @data))

But it doesn’t work either. What would be the best way to check ?

EDIT

I arrived at this code with the junction of the answers:

while(isdate((CONCAT(@dia,'-',month(@vencimento1),'-',year(@vencimento1)))) = 0 ) 
        set @dia = @dia - 1;
        set @data = (select convert(date,CONCAT(@dia,'-',month(@vencimento1),'-',year(@vencimento1))))
        print @data
        set @vencimento = (select DATEADD(month, 1, @data))

But what happens, it does not generate the correct day, it was to generate for example on 31/12, it generates pro day 30/12, and on January 31/01, and in February it takes day 29.

  • What DATE would that be? Why has 2 due dates and date? can only be useful days?

2 answers

1

As it stands, if the date is not valid (considering your if validation), you are setting two values for the variable @vencimento ((select dateadd(day, -1, @data), DATEADD(month, 1, @data))). This is the error shown!

Leaves only the first expression that should solve what you need:

if (select isdate(DATEADD(month, 1, @data))) = 1 
    select @vencimento = DATEADD(month, 1, @data)
else
    select @vencimento = DATEADD(day, -1, @data)

Take a look in this test.


edited

Considering @data being the type date (see comment on this reply), a cast to be used by the function dateadd (another example):

if (select isdate(DATEADD(month, 1, CAST(@data AS datetime)))) = 1 
    select @vencimento = DATEADD(month, 1, CAST(@data AS datetime))
else
    select @vencimento = DATEADD(day, -1, CAST(@data AS datetime))
  • When I add, it returns me this error Tipo de dados de argumento date inválido para o argumento 1 da função isdate.

  • @data is the type datetime? If not, you will need to convert before using the function dateadd

  • @date is @data date,

  • @Mariana I changed the answer considering the type of @data

  • @Mariana, it worked out?

  • I was doing the tests, so I haven’t answered yet, I haven’t, because he’s skipping the month, he’s going 2 in 2, I’m adapting between the two answers, and the date he has to pick up the @dia, and not the date on @data

  • I’m trying something like while (select isdate(dateadd(DAY, @dia, (select DATEADD(month, 1, CAST(@vencimento1 AS datetime)))))) = 1 
 set @dia = @dia - 1;
 set @data = DATEADD(DAY, @dia, (select dateadd(month, 1, @vencimento1))) but does not execute.

Show 2 more comments

1


It could be done like this:

declare @dia varchar(2)
declare @mes varchar(2)
declare @ano  varchar(4)
declare @data date


set @dia = 40;
set @mes = 80;
set @ano = 2019

begin
   if ( @dia > 31 ) set @dia = 31
   if ( @mes > 12 ) set @mes = 12
   if ( @ano > 2019 ) set @ano = 2019


   while ( isdate( concat( @mes, '-', @dia, '-', @ano )) = 0 ) 
      set @dia = @dia - 1;

   set @data = cast( concat( @mes, '-', @dia, '-', @ano ) as date )
   print concat( @mes, '-', @dia, '-', @ano )

   print @data
end
  • I tried something like while (select isdate(dateadd(DAY, @dia, (select DATEADD(month, 1, CAST(@vencimento1 AS datetime)))))) = 1 
 set @dia = @dia - 1;
 set @data = DATEADD(DAY, @dia, (select dateadd(month, 1, @vencimento1))) but he does not perform.

  • I don’t understand why you can’t use the code I posted.

  • I can, but I’m adapting to mine, because I need to change the date, because if it’s in December, and it only adds in the month, I need to treat the year too, and the question of the day.

  • Change the question and post what are the possibilities.

  • Use this code. In the starting values, put the values you will be receiving and see if it will not work.

  • It worked, to validate the date, I did so while(isdate((CONCAT(@dia,'-',month(@vencimento1),'-',year(@vencimento1)))) = 0 ) 
 set @dia = @dia - 1;
 set @data = (select convert(date,CONCAT(@dia,'-',month(@vencimento1),'-',year(@vencimento1))))
 print @data
 set @vencimento = (select DATEADD(month, 1, @data)) just that I would like him to check a month forward, and add according to the year and month. Thank you.

  • It worked, thank you.

Show 2 more comments

Browser other questions tagged

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