Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need SQL Function ?


Need SQL Function ?

Author
Message
bala2013
bala2013
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
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?
HanShi
HanShi
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3051 Visits: 3633
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’! **
bala2013
bala2013
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 167
Thanks very much !
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16680 Visits: 17038
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)
HanShi
HanShi
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3051 Visits: 3633
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’! **
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16680 Visits: 17038
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)
bala2013
bala2013
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 167
very intersting link,very useful for me.thanks a lot!
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45464 Visits: 39948
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
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