I'm going to do my best to explain to you what you need, but you really, really really need to listen to advice and act upon it. Answer the questions that are actually asked, not what you want to write.
CHill60 asked:
Show the table structure and give some sample data and expected results.
You presumably have a table that you want to extract this information from. I have guessed what it might look like as you haven't actually told us. I was hoping for something like this...
create table test
(
id int identity(1,1),
[lease start] date,
[lease end] date
)
Some sample data:
INSERT INTO test VALUES
('2016-FEB-01', '2016-DEC-31'),
('2016-JAN-01', '2016-SEP-05'),
('2015-JAN-01', '2018-SEP-05'),
('2016-APR-01', '2016-SEP-01'),
('2015-JAN-01', '2016-SEP-05')
Expected results:
id result
1 365
2 248
3 978
4 153
5 248
Akhil Jain said:
for 1.2 as 05/09/2016 it will be 1st date of 2016 for this eg:(or the any year)
To get the first day of a year you can use this query snippet:
dateadd(yy, datediff(yy, 0, [lease end]), 0)
Akhil Jain said:
daydiff is no of days b/w from_date and @to_date
The function you are looking for is actually
DATEDIFF[
^] - to get the results in days you can use
day
,
dd
or
d
as the first parameter. For example:
DATEDIFF(day, @from, [lease end])
DATEDIFF(dd, @from, [lease end])
DATEDIFF(d, @from, [lease end])
CHill60 said:
Then tabulate those conditions properly ... Spell things properly and consistently ...
If you draw up a TABLE of the dates you are checking - e.g. in a spreadsheet - then you can spot any problems, overlaps or gaps. It also makes the requirements much, much easier to read. I was expecting something like:
1) Rating period @from = 01/01/2016 @to 31/12/2016
Reference [lease start] [lease end] Calculation (results in days)
1.1 01/02/2016 31/12/2016 [lease start] - [lease end]
1.2 01/01/2015 05/09/2016 1stDayOfYear([lease end]) - (05/09/2016)
1.3 01/01/2015 05/09/2018 @from- @to
1.4 01/03/2016 01/09/2016 [lease start] -[lease end]
2) Rating period @from = 01/03/2016 @to = 31/12/2016
Reference [lease start] [lease end] Calculation (results in days)
2.1 01/02/2016 31/12/2016 @from - @to
2.2 01/01/2015 05/09/2016 @from -[lease end]
2.3 01/01/2015 05/09/2018 @from -[lease end]
2.4 01/04/2016 01/09/2016 [lease start] - [lease end]
Note how I have been consistent - all of the column names are spelled the same way instead of leas.start, (lease end), lease.end etc.
The user inputs are clearly indicated with the @ symbol.
By using this table I can clearly see some overlap issues (2.2 and 2.3 for example)
To implement the change in Rating Periods you will probably need to use
IF...ELSE[
^]. For example:
IF @from = '2016-JAN-01' AND @to = '2016-DEC-31'
ELSE
To implement the other checks you will need to use
CASE[
^]. A partial example:
SELECT id,
CASE WHEN [lease start] = CAST('2016-FEB-01' AS DATE) AND [lease end] = CAST('2016-DEC-31' AS DATE) THEN DATEDIFF(dd, @from, @to)
WHEN [lease start] = CAST('2016-JAN-01' AS DATE) AND [lease end] = CAST('2016-SEP-05' AS DATE) THEN DATEDIFF(dd, @from, [lease end])
WHEN [lease start] = CAST('2015-JAN-01' AS DATE) AND [lease end] = CAST('2018-SEP-05' AS DATE) THEN DATEDIFF(dd, @from, [lease end])
WHEN [lease start] = CAST('2016-APR-01' AS DATE) AND [lease end] = CAST('2016-SEP-01' AS DATE) THEN DATEDIFF(dd, [lease start], [lease end])
WHEN [lease start] = CAST('2015-JAN-01' AS DATE) AND [lease end] = CAST('2016-SEP-05' AS DATE) THEN DATEDIFF(dd, dateadd(yy, datediff(yy, 0, [lease end]), 0), CAST('2016-SEP-05' AS DATE))
END as result
FROM test
The rest is up to you. If you have further questions then do come back but remember to clearly state your problem and include the code that you have tried