Need SQL Function ?

  • 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?

  • 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’! **
  • Thanks very much !

  • 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/

  • 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’! **
  • 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/

  • very intersting link,very useful for me.thanks a lot!

  • 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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply