SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Date Issue


Date Issue

Author
Message
ningaraju.n
ningaraju.n
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1048 Visits: 425
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
anthony.green
anthony.green
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63328 Visits: 8598
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





How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


ningaraju.n
ningaraju.n
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1048 Visits: 425
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.
anthony.green
anthony.green
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63328 Visits: 8598
Then I suggest you build a table which houses all the different probabilities



How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


Ajay.Kedar
Ajay.Kedar
SSC Veteran
SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)

Group: General Forum Members
Points: 255 Visits: 59
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
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)

Group: General Forum Members
Points: 225858 Visits: 40423
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);




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)

Group: General Forum Members
Points: 225858 Visits: 40423
If you want to see some more date calculations, look here:

http://www.sqlservercentral.com/blogs/lynnpettis/2009/03/25/some-common-date-routines/

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search