August 18, 2015 at 12:07 pm
What is the best formula to do this in SQL?
This is from Access
SELECT Year([EndDate]) AS SeasonYear, PersonMembership.EndDate
FROM PersonMembership
GROUP BY Year([EndDate]), PersonMembership.EndDate
HAVING (((PersonMembership.EndDate)>=DateSerial(Year(Date()),6,30)));
August 18, 2015 at 12:11 pm
i found this user defined function that does the same thing:
http://www.experts-exchange.com/articles/670/DateSerial-Function-for-Microsoft-SQL-Server.html
SELECT dbo.DateSerial(Year(getdate()),6,30)
Lowell
August 18, 2015 at 12:42 pm
Here's one with the same functionality but as an inline table valued function.
CREATE FUNCTION dbo.iDateSerial
(
@year int ,
@month int ,
@day int
)
RETURNS TABLE
AS
RETURN
SELECT DATEADD(dd, @day - 1, DATEADD(mm, @month - 1, CAST( @year as CHAR(4)))) AS DateSerial WHERE @year BETWEEN 1900 AND 9999
UNION ALL
SELECT DATEADD(dd, @day - 1, DATEADD(mm, @month - 1, 0)) WHERE @year < 1900
UNION ALL
SELECT DATEADD(dd, @day - 1, DATEADD(mm, DATEDIFF( mm, 0, @month), - 1)) WHERE @year > 9999;
In 2012 there's also an available function called DATEFROMPARTS() which works in a similar way.
August 18, 2015 at 12:55 pm
Thank you! it works!
Yay!
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply