You must Sign In to post a response.
  • Category: SQL Server

    How get all the from table data for a particular month

    i have sale table with a date field

    i have a drodownbox which contain month if i select any month then

    i want to write a query which display all the record of particular month.

    thank u
  • #745387
    You can use following:

    If you just want to use month then you can use

    Select * from tablename where DATENAME(MM, TransDateTime) = 'May'

    If you want to use both month and year then you can use

    Select * from tablename where SUBSTRING(CONVERT(VARCHAR(11), TransDateTime, 113), 4, 8) = 'May 2014'

    Here TransDateTime is the column storing date and time.

  • #745389
    Hi Guddu

    Please try with below query

    select * from table_name where Datename(m,column_name)='may'


    Hope it will be help you.


    Regards,
    Sudeep Bhawsar

  • #745396
    following simple query will resolve your issue

    select * from Table_Name where
    YEAR(date_column)=YEAR(getdate()) AND MONTH(date_column)=MONTH(getdate())

    you need to enter name of date column instead of 'date_column'
    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #745440
    Pass the value as a parameter to the Query fro example



    Select * from tablename
    where DATENAME(MM, DateColumn) = 'January'


    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #745465
    Let's assume you have a table named things that looks something like this:

    id happend_at
    -- ----------------
    1 2009-01-01 12:08
    2 2009-02-01 12:00
    3 2009-01-12 09:40
    4 2009-01-29 17:55
    And let's say you want to execute to find all the records that have a happened_at during the month 2009/01 (January 2009). The SQL query would be:

    SELECT id FROM things
    WHERE MONTH(happened_at) = 1 AND YEAR(happened_at) = 2009
    Which would return:

    id
    ---
    1
    3
    4

  • #745569
    Hi,

    If you want to filter data based on your selection then filter it based on your need.

    In your case you need to filter based on month column then refer below sample code


    select * from tablename
    where datename(col)='yourmonthname'

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #751705
    hi,

    You should compare dateColumn's month name and dropdown selected month name like that

    select * from sale where datename(MONTH,dateColumn)='"+convert.ToString(ddlMonth.SelectedValue)+"';

    Suppose current month is september than this query will gives all records from september.
    In this way you can get month records which is selected in dropdown,
    I hope you will get your solution from this, if still you have an issue than i'll get back to you.

    Regards,
    Nirav Prabtani (Senior Web Developer)
    Email : niravjprabtani@gmail.com
    blog : niravprabtani.blogspot.in


  • Sign In to post your comments