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. LINQ
  4. 'Date syntax error in query expression'

'Date syntax error in query expression'

Scheduled Pinned Locked Moved LINQ
databasehelpquestion
13 Posts 6 Posters 59 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.
  • M Member 12332478

    I getting this error and cant find reason why ? System.Data.OleDb.OleDbException: 'Date syntax error in query expression' datumdos BETWEEN # 9.8.2019. # AND # 18.8.2019. # '.' sql1 = "SELECT * FROM tblpromet WHERE datumdos BETWEEN # 1.1.2019.ToShortDateString() # AND # 10.5.2019.ToShortDateString() #" da1 = New OleDb.OleDbDataAdapter(sql1, con) da1.Fill(ds, "PrometBase") ' This Table name

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    You are converting the dates to strings, so the comparison will not work as it requires Date types.

    M 1 Reply Last reply
    0
    • L Lost User

      You are converting the dates to strings, so the comparison will not work as it requires Date types.

      M Offline
      M Offline
      Member 12332478
      wrote on last edited by
      #3

      If I change string to : sql1 = "SELECT * FROM tblpromet WHERE datumdos BETWEEN # 1.1.2019. # AND # 10.5.2019. #" I have same message, and this are not a string, any suggestion ?

      M 1 Reply Last reply
      0
      • M Member 12332478

        If I change string to : sql1 = "SELECT * FROM tblpromet WHERE datumdos BETWEEN # 1.1.2019. # AND # 10.5.2019. #" I have same message, and this are not a string, any suggestion ?

        M Offline
        M Offline
        Maciej Los
        wrote on last edited by
        #4

        You should use [ISO format](https://en.wikipedia.org/wiki/ISO\_8601) of date:

        sql1 = "SELECT * FROM tblpromet WHERE datumdos BETWEEN #2019-01-01# AND #2019-05-10#"

        M 1 Reply Last reply
        0
        • M Maciej Los

          You should use [ISO format](https://en.wikipedia.org/wiki/ISO\_8601) of date:

          sql1 = "SELECT * FROM tblpromet WHERE datumdos BETWEEN #2019-01-01# AND #2019-05-10#"

          M Offline
          M Offline
          Member 12332478
          wrote on last edited by
          #5

          Thanks that is working now I need to change this date with datetimepicker in which is date visible in Eu format dd.MM.yyyy

          M 1 Reply Last reply
          0
          • M Member 12332478

            I getting this error and cant find reason why ? System.Data.OleDb.OleDbException: 'Date syntax error in query expression' datumdos BETWEEN # 9.8.2019. # AND # 18.8.2019. # '.' sql1 = "SELECT * FROM tblpromet WHERE datumdos BETWEEN # 1.1.2019.ToShortDateString() # AND # 10.5.2019.ToShortDateString() #" da1 = New OleDb.OleDbDataAdapter(sql1, con) da1.Fill(ds, "PrometBase") ' This Table name

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #6

            Don't do it like that! In this particular instance, you might be OK, since the values are dates. But concatenating values into your query is a bad habit which will lead to SQL Injection vulnerabilities in your code. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query. Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^] How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^] Query Parameterization Cheat Sheet | OWASP[^]

            Const query As String = "SELECT * FROM tblpromet WHERE datumdos BETWEEN ? AND ?"

            da1 = New OleDb.OleDbDataAdapter(query, con)
            da1.SelectCommand.Parameters.AddWithValue("min", minDatePicker.Value)
            da1.SelectCommand.Parameters.AddWithValue("max", maxDatePicker.Value)
            da1.Fill(ds, "PrometBase")


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            M 2 Replies Last reply
            0
            • M Member 12332478

              Thanks that is working now I need to change this date with datetimepicker in which is date visible in Eu format dd.MM.yyyy

              M Offline
              M Offline
              Maciej Los
              wrote on last edited by
              #7

              No, you don't! All you have to do is to pass proper date from DateTimePicker, see:

              sql1 = String.Format("SELECT * FROM tblpromet WHERE datumdos BETWEEN #{0}# AND #{1}#", DateTimePicker1.Value, DateTimePicker2.Value)

              Date is a date. Its format is for string representation only!

              Richard DeemingR 1 Reply Last reply
              0
              • M Maciej Los

                No, you don't! All you have to do is to pass proper date from DateTimePicker, see:

                sql1 = String.Format("SELECT * FROM tblpromet WHERE datumdos BETWEEN #{0}# AND #{1}#", DateTimePicker1.Value, DateTimePicker2.Value)

                Date is a date. Its format is for string representation only!

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #8

                That's a SQL query you've just injected user-controlled values into. It might be OK with dates, but it's still a bad habit and should be discouraged.


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                L 1 Reply Last reply
                0
                • Richard DeemingR Richard Deeming

                  Don't do it like that! In this particular instance, you might be OK, since the values are dates. But concatenating values into your query is a bad habit which will lead to SQL Injection vulnerabilities in your code. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query. Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^] How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^] Query Parameterization Cheat Sheet | OWASP[^]

                  Const query As String = "SELECT * FROM tblpromet WHERE datumdos BETWEEN ? AND ?"

                  da1 = New OleDb.OleDbDataAdapter(query, con)
                  da1.SelectCommand.Parameters.AddWithValue("min", minDatePicker.Value)
                  da1.SelectCommand.Parameters.AddWithValue("max", maxDatePicker.Value)
                  da1.Fill(ds, "PrometBase")


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  M Offline
                  M Offline
                  Member 12332478
                  wrote on last edited by
                  #9

                  That's it thanks a lot

                  1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    Don't do it like that! In this particular instance, you might be OK, since the values are dates. But concatenating values into your query is a bad habit which will lead to SQL Injection vulnerabilities in your code. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query. Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^] How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^] Query Parameterization Cheat Sheet | OWASP[^]

                    Const query As String = "SELECT * FROM tblpromet WHERE datumdos BETWEEN ? AND ?"

                    da1 = New OleDb.OleDbDataAdapter(query, con)
                    da1.SelectCommand.Parameters.AddWithValue("min", minDatePicker.Value)
                    da1.SelectCommand.Parameters.AddWithValue("max", maxDatePicker.Value)
                    da1.Fill(ds, "PrometBase")


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    M Offline
                    M Offline
                    Member 12332478
                    wrote on last edited by
                    #10

                    This is perfect only how to add more filters like : FROM tblpromet WHERE kod like '" & txttrkod.Text & "%'and obrada like '" & txtobrada & "%' order by kod"

                    Richard DeemingR 1 Reply Last reply
                    0
                    • M Member 12332478

                      This is perfect only how to add more filters like : FROM tblpromet WHERE kod like '" & txttrkod.Text & "%'and obrada like '" & txtobrada & "%' order by kod"

                      Richard DeemingR Offline
                      Richard DeemingR Offline
                      Richard Deeming
                      wrote on last edited by
                      #11

                      In exactly the same way:

                      Const query As String = "SELECT * FROM tblpromet WHERE kod like ? AND obrada like ? ORDER BY kod"

                      da1 = New OleDb.OleDbDataAdapter(query, con)
                      da1.SelectCommand.Parameters.AddWithValue("kod", txttrkod.Text & "%")
                      da1.SelectCommand.Parameters.AddWithValue("obrada", txtobrada.Text & "%")
                      da1.Fill(ds, "PrometBase")

                      You just have to make sure to add the parameters in the same order that the ? placeholders appear in the query, since you can't use named parameters.


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                      1 Reply Last reply
                      0
                      • Richard DeemingR Richard Deeming

                        That's a SQL query you've just injected user-controlled values into. It might be OK with dates, but it's still a bad habit and should be discouraged.


                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                        L Offline
                        L Offline
                        Laura Wilmer
                        wrote on last edited by
                        #12

                        haha your right like Homer :laugh: :laugh: :laugh:

                        1 Reply Last reply
                        0
                        • M Member 12332478

                          I getting this error and cant find reason why ? System.Data.OleDb.OleDbException: 'Date syntax error in query expression' datumdos BETWEEN # 9.8.2019. # AND # 18.8.2019. # '.' sql1 = "SELECT * FROM tblpromet WHERE datumdos BETWEEN # 1.1.2019.ToShortDateString() # AND # 10.5.2019.ToShortDateString() #" da1 = New OleDb.OleDbDataAdapter(sql1, con) da1.Fill(ds, "PrometBase") ' This Table name

                          H Offline
                          H Offline
                          Hailu Worku Obsse
                          wrote on last edited by
                          #13

                          The error is with in the query string. Between keyword does not work well with string datatype.

                          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