Getting months from daily items

Last post 05-10-2008 5:25 PM by frist44. 9 replies.

Sort Posts:

  • Getting months from daily items

    05-09-2008, 9:03 AM
    • Loading...
    • frist44
    • Joined on 03-01-2007, 8:38 AM
    • Posts 431

    I have a database with multiple items each day. I'm looking to extract monthly information from this data. So I'm thinking that I would like to have a drop down list with each available month in the list. How would I extract which months (for multiple years) have data from a datatype = smalldatetime?

    I'm using c#

     Thanks!

  • Re: Getting months from daily items

    05-09-2008, 9:14 AM

     

                        select distinct datename(month,CreatedDate) from tablename

                                                or

                        select distinct datename(month,cast(CreatedDate as datetime)) from tablename

  • Re: Getting months from daily items

    05-09-2008, 9:25 AM
    • Loading...
    • frist44
    • Joined on 03-01-2007, 8:38 AM
    • Posts 431

    Thanks. That returns the month. I tried:

    SELECT DATENAME(m, GETDATE()) AS 'Month Name'; and that returned "May"

    I would like it to return "May 2008". And then when i select that option in the drop down, it just selects those records during that month and year.

  • Re: Getting months from daily items

    05-09-2008, 9:28 AM
    Answer

    select distinct substring(convert(varchar,datecolumn,106),4,10) as months from table

  • Re: Getting months from daily items

    05-09-2008, 9:28 AM
    • Loading...
    • frist44
    • Joined on 03-01-2007, 8:38 AM
    • Posts 431

    I guess this would work

    SELECT DATENAME(m, GETDATE()) + ' ' + DATENAME(yy, GETDATE()) AS 'Month' ?

     But how would I get it the right values?

  • Re: Getting months from daily items

    05-09-2008, 9:39 AM
    Answer

    what is the meaning of           "But how would I get it the right values?".

    that means u are asking how to retrieve the values for the selected item as it don't have any id.

    if it is case,

    select * from the tablename where substring(convert(varchar,datecolumn,106),4,10) = (dropdownlist selected value)

  • Re: Getting months from daily items

    05-09-2008, 9:47 AM
    • Loading...
    • frist44
    • Joined on 03-01-2007, 8:38 AM
    • Posts 431

    Exactly what i was looking for. Thanks!

  • Re: Getting months from daily items

    05-09-2008, 1:39 PM
    • Loading...
    • frist44
    • Joined on 03-01-2007, 8:38 AM
    • Posts 431

    Here's my solution

    SELECT DISTINCT SUBSTRING(CONVERT (VARCHAR, [dateworked], 106), 4, 10) AS Months
    FROM [tasks]

    ORDER BY ??????

    However, I would like to order those Descendingly by [dateworked], but ORDER BY isn't working, and then I have to include it in the select, and it shows multiple dates with a Month.

    any idea?

  • Re: Getting months from daily items

    05-10-2008, 4:54 AM
    Answer

     try this.

    SELECT DISTINCT substring(convert(varchar,dateworked,112),1,6) as Tempdate,
    SUBSTRING(CONVERT (VARCHAR, [dateworked], 106), 4, 10) AS Months
    FROM [tasks] order by TempDate asc

    Don't show tempdate in frontend. it is just used as a helper column.

  • Re: Getting months from daily items

    05-10-2008, 5:25 PM
    • Loading...
    • frist44
    • Joined on 03-01-2007, 8:38 AM
    • Posts 431

    Perfect.

Page 1 of 1 (10 items)