November 7, 2012 at 3:18 am
I have a problem with dates
Problem: There is a master table which defines the date based on range as follows
[From][To][ResultDate]
'1st Jan''31st March''31.12.YYYY-1'
'1st April''30th June''31.03.YYYY'
'1st July''30th Sep''30.06.YYYY'
'1st Oct''31st Dec' '30.09.YYYY'
If my input date is today's date then it should return 30.09.2012 as today is between from and to dates of the 4th row.
If my input is '2012-05-07'(7th May 2012) then I should get 31.03.2012
November 7, 2012 at 3:44 am
You need to do a condition where Date >= FromDate and Date <= ToDate
DECLARE @Table TABLE (FromDate DATETIME, ToDate DATETIME, ResultDate DATETIME)
INSERT INTO @Table VALUES
('2012-01-01','2012-03-31','2011-12-31'),
('2012-04-01','2012-06-30','2012-03-31'),
('2012-07-01','2012-09-30','2012-06-30'),
('2012-10-01','2012-12-31','2012-09-30')
SELECT
ResultDate
FROM
@Table
WHERE
GETDATE() >= FromDate
AND
GETDATE() <= ToDate
November 7, 2012 at 4:15 am
Thanks,
But the thing is it works for only current year and the input date can be last year date.
for example input date '2010-07-06'(6th July 2010) then output should be 30.06.2010.
November 7, 2012 at 4:25 am
Then I suggest you build a table which houses all the different probabilities
November 19, 2012 at 12:04 am
hi, You can write a query like
select Case When Month(getdate()) <=3 Then '31-03-'+ Convert(Varchar(4),Year(GetDate()))
When Month(getdate()) > 3 AND Month(getdate()) <=6 THEN '31-06-' + Convert(Varchar(4),Year(GetDate()))
When Month(getdate()) > 9 AND Month(getdate()) <=9 THEN '31-09-' + Convert(Varchar(4),Year(GetDate()))
ELSE '31-12-' + Convert(Varchar(4),Year(GetDate())) END
this will return you the expected result
November 19, 2012 at 12:37 am
Simple:
declare @ThisDate datetime;
set @ThisDate = getdate();
select @ThisDate, dateadd(qq, datediff(qq, 0, @ThisDate), -1);
set @ThisDate = '20120507';
select @ThisDate, dateadd(qq, datediff(qq, 0, @ThisDate), -1);
November 19, 2012 at 12:38 am
If you want to see some more date calculations, look here:
http://www.sqlservercentral.com/blogs/lynnpettis/2009/03/25/some-common-date-routines/
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply