Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Date Issue Expand / Collapse
Author
Message
Posted Wednesday, November 7, 2012 3:18 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, February 21, 2013 3:14 AM
Points: 134, 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
Post #1381858
Posted Wednesday, November 7, 2012 3:44 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:54 AM
Points: 5,149, Visits: 4,961
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





Want an answer fast? Try here
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
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1381888
Posted Wednesday, November 7, 2012 4:15 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, February 21, 2013 3:14 AM
Points: 134, 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.
Post #1381900
Posted Wednesday, November 7, 2012 4:25 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:54 AM
Points: 5,149, Visits: 4,961
Then I suggest you build a table which houses all the different probabilities



Want an answer fast? Try here
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
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1381907
Posted Monday, November 19, 2012 12:04 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, March 27, 2013 3:17 AM
Points: 205, Visits: 58
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
Post #1386174
Posted Monday, November 19, 2012 12:37 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 5:09 PM
Points: 23,089, Visits: 31,635
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);





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)
Post #1386185
Posted Monday, November 19, 2012 12:38 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 5:09 PM
Points: 23,089, Visits: 31,635
If you want to see some more date calculations, look here:

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



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)
Post #1386186
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse