how to subtract column date with system date?

Asked

Viewed 435 times

0

I have a date column data_novo and I want to know how many days the product is open, I have to make a subtraction account between the "system date" and the date that is in the column data_novo. And this result will be displayed in another column (e.g. open days)?

I tried the following:

diferença= DATETIME-DAT_ABER

I have no idea how to do it. Can you please help me?

  • diferenca = getdate() - dat_abert

  • I tried that way and it didn’t work.

  • Are you going to perform an update? I don’t understand this: esse resultado vai ser exibido em outra coluna

  • Is that it? http://sqlfiddle.com/#! 18/9eecb/21540

1 answer

1


You can use the sql server DATEDIFF function:

SELECT DATEDIFF(day,startDate,endDate) AS 'Duration'  
FROM dbo.Duration; 

In this case he calculates the difference of days between two dates.

  • I tried that, but it didn’t work out Maycon: SELECT top 100* select DATEDIFF(day,startDate,openingdata_) AS 'Duration' FROM [tb_a] WITH (NOLOCK)

  • I don’t think it worked because my column carries the date information as follows: 2017-08-12 14:32:23,000

  • Your opening date column?

  • That’s right, Maycon.

  • 1

    Try SELECT top 100* select DATEDIFF(day,Getdate(),start_date) AS 'Duration' FROM [tb_a] WITH (NOLOCK)

  • Good morning. It worked out Maycon. Thanks a lot. abs

Show 1 more comment

Browser other questions tagged

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