Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Sql query to select Date between the range

Sql query to select Date between the range

Scheduled Pinned Locked Moved Database
databasehelpcsharpasp-nettutorial
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pavanip
    wrote on last edited by
    #1

    Hi, I am having database table as TicketNo,Name,Issue Date. I inserted the data for "IssueDate" field using calender control in Asp.Net. So the table data looks like this TicketNo Name IssueDate 1 xxx Feb 5 2009 12:00AM 2 yyy Feb 6 2009 12:00AM 3 zzz Feb 10 2009 12:00AM 4 aaa Feb 15 2009 12:00AM Now i want to write the query to select "IssueDate" between 02/04/2009 and 02/11/2009. Please Can anybody help me how to write query to retrieve the data between those dates? Thanks Pavani

    M R 2 Replies Last reply
    0
    • P pavanip

      Hi, I am having database table as TicketNo,Name,Issue Date. I inserted the data for "IssueDate" field using calender control in Asp.Net. So the table data looks like this TicketNo Name IssueDate 1 xxx Feb 5 2009 12:00AM 2 yyy Feb 6 2009 12:00AM 3 zzz Feb 10 2009 12:00AM 4 aaa Feb 15 2009 12:00AM Now i want to write the query to select "IssueDate" between 02/04/2009 and 02/11/2009. Please Can anybody help me how to write query to retrieve the data between those dates? Thanks Pavani

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      pavanip wrote:

      select "IssueDate" between 02/04/2009 and 02/11/2009.

      almost had it in your question Select * from Tablename where IssueDate Between '02/04/2009' and '02/11/2009'

      Never underestimate the power of human stupidity RAH

      P 1 Reply Last reply
      0
      • P pavanip

        Hi, I am having database table as TicketNo,Name,Issue Date. I inserted the data for "IssueDate" field using calender control in Asp.Net. So the table data looks like this TicketNo Name IssueDate 1 xxx Feb 5 2009 12:00AM 2 yyy Feb 6 2009 12:00AM 3 zzz Feb 10 2009 12:00AM 4 aaa Feb 15 2009 12:00AM Now i want to write the query to select "IssueDate" between 02/04/2009 and 02/11/2009. Please Can anybody help me how to write query to retrieve the data between those dates? Thanks Pavani

        R Offline
        R Offline
        Rupesh Kumar Swami
        wrote on last edited by
        #3

        select * from table where issueDate BETWEEN CONVERT(varchar, '2009-02-04', 111) AND CONVERT(varchar, '2009-02-11', 111)

        Rupesh Kumar Swami Software Developer, Integrated Solution, Bikaner (India) My Company Award: Best VB.NET article of June 2008: Create Column Charts Using OWC11

        P M 2 Replies Last reply
        0
        • M Mycroft Holmes

          pavanip wrote:

          select "IssueDate" between 02/04/2009 and 02/11/2009.

          almost had it in your question Select * from Tablename where IssueDate Between '02/04/2009' and '02/11/2009'

          Never underestimate the power of human stupidity RAH

          P Offline
          P Offline
          pavanip
          wrote on last edited by
          #4

          I tried with that query but when i execute that query i am getting zero results but there are records in my table with that dates. I stored date value in varchar is it correct or i have to use only datetime datatype.

          J M 2 Replies Last reply
          0
          • R Rupesh Kumar Swami

            select * from table where issueDate BETWEEN CONVERT(varchar, '2009-02-04', 111) AND CONVERT(varchar, '2009-02-11', 111)

            Rupesh Kumar Swami Software Developer, Integrated Solution, Bikaner (India) My Company Award: Best VB.NET article of June 2008: Create Column Charts Using OWC11

            P Offline
            P Offline
            pavanip
            wrote on last edited by
            #5

            I tried with this query select * from table where issueDate BETWEEN CONVERT(varchar, '2009-02-04', 111) AND CONVERT(varchar, '2009-02-11', 111) but when i execute that query i am getting zero results but there are records in my table with that dates. I stored date value in varchar is it correct or i have to use only datetime datatype.

            1 Reply Last reply
            0
            • P pavanip

              I tried with that query but when i execute that query i am getting zero results but there are records in my table with that dates. I stored date value in varchar is it correct or i have to use only datetime datatype.

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              You need to store the date as a date. Why people store dates in a character field is totally beyond me.

              1 Reply Last reply
              0
              • P pavanip

                I tried with that query but when i execute that query i am getting zero results but there are records in my table with that dates. I stored date value in varchar is it correct or i have to use only datetime datatype.

                M Offline
                M Offline
                Mycroft Holmes
                wrote on last edited by
                #7

                As Jamie said, mistake 1 STORE YOUR DATES AS DATE TIME. So now that your have screwed your data you have 2 choices. Convert the dates from varchar to datetime - this is the recommended solution Make all the date values into datetime in your query. Select * from table name where convert(datetime,Stupiddate) between thisdate and thathdate

                Never underestimate the power of human stupidity RAH

                P 1 Reply Last reply
                0
                • R Rupesh Kumar Swami

                  select * from table where issueDate BETWEEN CONVERT(varchar, '2009-02-04', 111) AND CONVERT(varchar, '2009-02-11', 111)

                  Rupesh Kumar Swami Software Developer, Integrated Solution, Bikaner (India) My Company Award: Best VB.NET article of June 2008: Create Column Charts Using OWC11

                  M Offline
                  M Offline
                  Mycroft Holmes
                  wrote on last edited by
                  #8

                  Rupesh Wrong way - you are converting to varchar and comparing strings, you need to convert to datetime to use between!

                  Never underestimate the power of human stupidity RAH

                  1 Reply Last reply
                  0
                  • M Mycroft Holmes

                    As Jamie said, mistake 1 STORE YOUR DATES AS DATE TIME. So now that your have screwed your data you have 2 choices. Convert the dates from varchar to datetime - this is the recommended solution Make all the date values into datetime in your query. Select * from table name where convert(datetime,Stupiddate) between thisdate and thathdate

                    Never underestimate the power of human stupidity RAH

                    P Offline
                    P Offline
                    pavanip
                    wrote on last edited by
                    #9

                    Thanks for your response that query is working for my condition. Select * from tbl_ticketinfo where convert(datetime,issuedate) between '" 02/05/2009' and '02/09/2009'

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups