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. General Programming
  3. C#
  4. Problem with SQL-Server Date Query

Problem with SQL-Server Date Query

Scheduled Pinned Locked Moved C#
8 Posts 5 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.
  • D Offline
    D Offline
    der flori
    wrote on last edited by
    #1

    hi, i have a little problem with the following Query:

    string str_Verlaufdate = "'" + p.Verlauf_date.Month + "/" + p.Verlauf_date.Day + "/" + p.Verlauf_date.Year + "'";

    DataRow[] dr2 = this.dataNet.Tables["Verlauf"].Select("BehID = '"+v[0].ToString()+"' and Date = "+str_Verlaufdate);

    I don´t get any result, but there exist a lot of records that match to this date in the database!

    D 1 Reply Last reply
    0
    • D der flori

      hi, i have a little problem with the following Query:

      string str_Verlaufdate = "'" + p.Verlauf_date.Month + "/" + p.Verlauf_date.Day + "/" + p.Verlauf_date.Year + "'";

      DataRow[] dr2 = this.dataNet.Tables["Verlauf"].Select("BehID = '"+v[0].ToString()+"' and Date = "+str_Verlaufdate);

      I don´t get any result, but there exist a lot of records that match to this date in the database!

      D Offline
      D Offline
      Dino Mulahusic
      wrote on last edited by
      #2

      How are your dates stored? If they are stored with time (3/3/2009 11:12:13) then you wont get any results. try entering your query in sql server and see what you get you can also try something like this

      string str_Verlaufdate = "'" + p.Verlauf_date.Month + "/" + p.Verlauf_date.Day + "/" + p.Verlauf_date.Year + "%'";

      DataRow[] dr2 = this.dataNet.Tables["Verlauf"].Select("BehID = '"+v[0].ToString()+"' and Date LIKE "+str_Verlaufdate);

      D 1 Reply Last reply
      0
      • D Dino Mulahusic

        How are your dates stored? If they are stored with time (3/3/2009 11:12:13) then you wont get any results. try entering your query in sql server and see what you get you can also try something like this

        string str_Verlaufdate = "'" + p.Verlauf_date.Month + "/" + p.Verlauf_date.Day + "/" + p.Verlauf_date.Year + "%'";

        DataRow[] dr2 = this.dataNet.Tables["Verlauf"].Select("BehID = '"+v[0].ToString()+"' and Date LIKE "+str_Verlaufdate);

        D Offline
        D Offline
        der flori
        wrote on last edited by
        #3

        yes, they are stored with time! But when I change the string in this way I get an syntax error:

        string str_Verlaufdate = "'" + p.Verlauf_date.Month + "/" + p.Verlauf_date.Day + "/" + p.Verlauf_date.Year + " 00:00:00'";

        I don´t know, how the format of the string has to look like.

        S D 2 Replies Last reply
        0
        • D der flori

          yes, they are stored with time! But when I change the string in this way I get an syntax error:

          string str_Verlaufdate = "'" + p.Verlauf_date.Month + "/" + p.Verlauf_date.Day + "/" + p.Verlauf_date.Year + " 00:00:00'";

          I don´t know, how the format of the string has to look like.

          S Offline
          S Offline
          Searril
          wrote on last edited by
          #4

          When I have to search by dates with times I do a 'from date' and a 'to date' because of the time issues. The from date should be the date with a time of midnight and end date would have a time of 1 second before midnight. For example, if I wanted everything from April 1 through April 7 I would search for: 2009/04/01 00:00:00 through 2009/04/07 23:59:59

          1 Reply Last reply
          0
          • D der flori

            yes, they are stored with time! But when I change the string in this way I get an syntax error:

            string str_Verlaufdate = "'" + p.Verlauf_date.Month + "/" + p.Verlauf_date.Day + "/" + p.Verlauf_date.Year + " 00:00:00'";

            I don´t know, how the format of the string has to look like.

            D Offline
            D Offline
            Dino Mulahusic
            wrote on last edited by
            #5

            look at your table data and see what format it is and try something like this

            string str_Verlaufdate = "'" + p.Verlauf_date.ToString("mm/dd/yyyy HH:mm:ss") + "'";

            or some other format do some research here[^]

            D 1 Reply Last reply
            0
            • D Dino Mulahusic

              look at your table data and see what format it is and try something like this

              string str_Verlaufdate = "'" + p.Verlauf_date.ToString("mm/dd/yyyy HH:mm:ss") + "'";

              or some other format do some research here[^]

              D Offline
              D Offline
              der flori
              wrote on last edited by
              #6

              thanks a lot, this link solved my problem :)

              D 1 Reply Last reply
              0
              • D der flori

                thanks a lot, this link solved my problem :)

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                Now, forget everything you just learned and do it the correct way using parameterized queries[^] instead. If you did that first, this would never have been a problem.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007, 2008

                P 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  Now, forget everything you just learned and do it the correct way using parameterized queries[^] instead. If you did that first, this would never have been a problem.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007, 2008

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  Hear hear! There's no substitute for doing it the right way.

                  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