-1

I have 3 column ID , From date and To date.I need to check the date difference between from and to and add a new row based on difference.Example below.How can this be done in sql server

+-----+------------+------------+
| ID  |    From    |     To     |
+-----+------------+------------+
| 100 | 15/05/2016 | 17/05/2016 |
+-----+------------+------------+

Output that i need

+-----+------------+
| ID  |    Date    |
+-----+------------+
| 100 | 15/05/2016 |
| 100 | 16/05/2016 |
| 100 | 17/05/2016 |
+-----+------------+
Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118
  • So you want the average date between them ? And you mean you want to normalize your data? – sagi May 17 '16 at 13:53
  • 3
    Discussed 100 times. You should have a calendar table and then a join where you have between comparison in join clause. – Giorgi Nakeuri May 17 '16 at 13:53
  • working days or calendar? –  May 17 '16 at 13:53
  • @GiorgiNakeuri I don't think he needs a calender table here – sagi May 17 '16 at 13:54
  • Actually, agreed with Giorgi. There are a thousand good posts to explain how to do this. I'm going to vote to close your question. –  May 17 '16 at 13:54
  • Check this out about [how to post a T-SQL question](https://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/) – Peter Szekeli May 17 '16 at 13:54
  • 1
    Possible duplicate of [Calculating how many Working Days between 2 Dates - T-SQL?](http://stackoverflow.com/questions/2258767/calculating-how-many-working-days-between-2-dates-t-sql) –  May 17 '16 at 13:54

1 Answers1

0

There is a function here:

dbo.ExplodeDates(@startdate datetime, @enddate datetime) 

Just update it to include your ID

dbo.ExplodeDates(@ID id, @startdate datetime, @enddate datetime) 

and use it like

SELECT *
FROM dbo.ExplodeDates(@ID id, @startdate datetime, @enddate datetime) 
Community
  • 1
  • 1
Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118