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. selecting first 7 days from database

selecting first 7 days from database

Scheduled Pinned Locked Moved Database
questiondatabaseannouncement
8 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.
  • B Offline
    B Offline
    benjamin yap
    wrote on last edited by
    #1

    hi, i got a database table that have 4 column id,subject,contents,date the date column is varchar datatype and its the data is DateTime.Now.ToString() How do i select 7 days record from the table.. Select * from announcement WHERE ???? thanks

    L M D 3 Replies Last reply
    0
    • B benjamin yap

      hi, i got a database table that have 4 column id,subject,contents,date the date column is varchar datatype and its the data is DateTime.Now.ToString() How do i select 7 days record from the table.. Select * from announcement WHERE ???? thanks

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      if your date and datetime information were stored as such (and not as a string), then you could simply use SQL stuff such as "ORDER BY" and "TOP N" since databases that support date types also know how to sort them chronologically. If you insist on storing datetime information as strings, you will: - be subject to problems due to different regional settings (which day/month/year is 02/03/04 in each country on Earth?) - need conversion functions everywhere in your SQL statements. You might want to read this little article[^]. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
      [The QA section does it automatically now, I hope we soon get it on regular forums as well]


      1 Reply Last reply
      0
      • B benjamin yap

        hi, i got a database table that have 4 column id,subject,contents,date the date column is varchar datatype and its the data is DateTime.Now.ToString() How do i select 7 days record from the table.. Select * from announcement WHERE ???? thanks

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

        As Luc has said you need to change your data type to datetime, leaving it as varchar is a disaster. Think about this, to get the top 7 records you could do the following SELECT TOP 2 Dt FROM (SELECT DISTINCT CONVERT(DATETIME,PeriodDate) Dt FROM Period ) X ORDER BY Dt desc That will have to convert EVERY datestring in your table, creating an index is a waste of time and all the regional problem Luc mentioned will be visited upon you. FIX YOUR DATA NOW!

        Never underestimate the power of human stupidity RAH

        D 1 Reply Last reply
        0
        • B benjamin yap

          hi, i got a database table that have 4 column id,subject,contents,date the date column is varchar datatype and its the data is DateTime.Now.ToString() How do i select 7 days record from the table.. Select * from announcement WHERE ???? thanks

          D Offline
          D Offline
          David Skelly
          wrote on last edited by
          #4

          What do you mean by "select 7 days record from the table"? Do you mean that you want all the records from the table for the last 7 days? Or do you mean that you want the top 7 records from the table irrespective of date? Or do you mean that you want all the records for the most recent 7 days which are actually in the database (which chronologically may range over more than 7 days if for example you have no entries at the weekend)? Each one of these will require a different query. None of which are particularly straightforward given the way in which the dates are stored in the table.

          1 Reply Last reply
          0
          • M Mycroft Holmes

            As Luc has said you need to change your data type to datetime, leaving it as varchar is a disaster. Think about this, to get the top 7 records you could do the following SELECT TOP 2 Dt FROM (SELECT DISTINCT CONVERT(DATETIME,PeriodDate) Dt FROM Period ) X ORDER BY Dt desc That will have to convert EVERY datestring in your table, creating an index is a waste of time and all the regional problem Luc mentioned will be visited upon you. FIX YOUR DATA NOW!

            Never underestimate the power of human stupidity RAH

            D Offline
            D Offline
            David Skelly
            wrote on last edited by
            #5

            Mycroft Holmes wrote:

            to get the top 7 records you could do the following SELECT TOP 2 Dt FROM (SELECT DISTINCT CONVERT(DATETIME,PeriodDate) Dt FROM Period ) X ORDER BY Dt desc

            Call me picky, but won't this give the top 2 records? Or am I missing something here?

            M 1 Reply Last reply
            0
            • D David Skelly

              Mycroft Holmes wrote:

              to get the top 7 records you could do the following SELECT TOP 2 Dt FROM (SELECT DISTINCT CONVERT(DATETIME,PeriodDate) Dt FROM Period ) X ORDER BY Dt desc

              Call me picky, but won't this give the top 2 records? Or am I missing something here?

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

              I only had 3 records in the test table I wrote this against :-O . If the OP can't work out the difference then I probably can't help him. And yep I call you picky :rolleyes:

              Never underestimate the power of human stupidity RAH

              D 1 Reply Last reply
              0
              • M Mycroft Holmes

                I only had 3 records in the test table I wrote this against :-O . If the OP can't work out the difference then I probably can't help him. And yep I call you picky :rolleyes:

                Never underestimate the power of human stupidity RAH

                D Offline
                D Offline
                David Skelly
                wrote on last edited by
                #7

                Mycroft Holmes wrote:

                And yep I call you picky

                Every forum needs at least one anal-retentive pedant to keep things in line. I do my best.

                M 1 Reply Last reply
                0
                • D David Skelly

                  Mycroft Holmes wrote:

                  And yep I call you picky

                  Every forum needs at least one anal-retentive pedant to keep things in line. I do my best.

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

                  David I doubt you are the only one - AR is a required trait for some aspects of development so you are welcome.

                  Never underestimate the power of human stupidity RAH

                  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