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

Need SQL Function ? Expand / Collapse
Author
Message
Posted Friday, June 28, 2013 2:33 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, October 9, 2013 1:15 AM
Points: 15, Visits: 167
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?
Post #1468465
Posted Friday, June 28, 2013 3:20 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 8:20 AM
Points: 2,238, Visits: 2,691
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



** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
Post #1468470
Posted Friday, June 28, 2013 3:48 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, October 9, 2013 1:15 AM
Points: 15, Visits: 167
Thanks very much !
Post #1468481
Posted Friday, June 28, 2013 7:33 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:09 PM
Points: 13,122, Visits: 11,958
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/


_______________________________________________________________

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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1468543
Posted Friday, June 28, 2013 7:43 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 8:20 AM
Points: 2,238, Visits: 2,691
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).


** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
Post #1468548
Posted Friday, June 28, 2013 8:09 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:09 PM
Points: 13,122, Visits: 11,958
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1468564
Posted Sunday, June 30, 2013 10:50 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, October 9, 2013 1:15 AM
Points: 15, Visits: 167
very intersting link,very useful for me.thanks a lot!
Post #1468924
Posted Monday, July 1, 2013 6:31 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:42 PM
Points: 36,781, Visits: 31,237
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
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1469257
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse