June 28, 2013 at 2:33 am
Dear all,
Hope things are going well at your end.
I need sql function for getting the following Scenarios:
Input parameter : ShiftDate
Need to create function for
Senario 1
1st day of the month – Till Date(Output:01-06-2013,02-06-2013,...28-06-2013)
Senario 2
1st day of the week – Till Day(week start date and end date)(23-06-2013,29-06-2013)
Senario 3
1st day of the year – Till Date(from 01-01-2013,02-01-2013,...01-02-2013,....28-06-2013)
please help on this?
June 28, 2013 at 3:20 am
Create a reference or tally table that holds all the dates of the period you need (i.e. 365 rows per year). Select the desired date range from this reference table with a simple SELECT and WHERE clause. You can JOIN the results with other tables when needed. You can also create a stored procedure (or TVF) from the code below.
DECLARE @ShiftDate date
SET @ShiftDate = '20130202'
SELECT datecolumn
FROM referencetable
WHERE datecolumn <= @ShiftDate
-- unmark the row below to get all dates from the first day of the year
--AND datecolumn >= CAST(YEAR(@ShiftDate) as CHAR(4)) + '0101'
-- unmark the row below to get all dates from the first day of the month
--AND datecolumn >= CAST(YEAR(@ShiftDate) as CHAR(4)) + RIGHT('00' + CAST(MONTH(@ShiftDate) as varchar(2)), 2) + '01'
-- unmark the row below to get all dates from the first day of the week
--AND datecolumn >= dateadd(day, -1 * (DATEPART(weekday, @ShiftDate))+1, @ShiftDate)
ORDER BY datecolumn
June 28, 2013 at 3:48 am
Thanks very much !
June 28, 2013 at 7:33 am
In addition to using a tally/numbers table you might also want to take a look at this article from Lynn. It has a collection of excellent methods for finding certain dates.
http://www.sqlservercentral.com/blogs/lynnpettis/2009/03/25/some-common-date-routines/[/url]
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
June 28, 2013 at 7:43 am
Hi Sean,
That's an interesting link. I've never thought of using the "0" in the DATEDIFF function, but it's an nice approach and consistent for the different dateparts (and therefor more readable).
June 28, 2013 at 8:09 am
HanShi (6/28/2013)
Hi Sean,That's an interesting link. I've never thought of using the "0" in the DATEDIFF function, but it's an nice approach and consistent for the different dateparts (and therefor more readable).
I can't take credit for it, that is all Lynn. But I frequently point people to that article because it works really well. 😉
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
June 30, 2013 at 10:50 pm
very intersting link,very useful for me.thanks a lot!
July 1, 2013 at 6:31 pm
balamurugan.devadas (6/30/2013)
very intersting link,very useful for me.thanks a lot!
The next question would be... do you understand how all of those examples actually work? Your are, in fact, the one that's going to have to support whatever you write at least until it gets to production. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply