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. Strange strings in SQL statement

Strange strings in SQL statement

Scheduled Pinned Locked Moved Database
helpcsharpdatabase
7 Posts 5 Posters 2 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.
  • T Offline
    T Offline
    tnook
    wrote on last edited by
    #1

    Hi, I'm having a problem with an sql select statement from some c# code. The function is meant to count the number of entries in a table where the users dates of birth are between two dates. The two birthday ranges are taken from two integers userAgeFrom and userAgeTo. When I run the code I get the following error: "The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value." The weird thing is if I manually populate the strings userDOBFrom and userDOBTo with dates (eg userDOBTo = "12/09/1971") the select statement works and returns the number of users. When I try to populate the strings from a DateTime object, as below, the function fails. Any help would be much appreciated! Kevin Here is the code: int userAgeFrom = 18; int userAgeTo = 30; int numUsers; string selectStatement; string userDOBFrom; string userDOBTo; DateTime dtUserDOBFrom = DateTime.Now; DateTime dtUserDOBTo = DateTime.Now; userDOBFrom = dtUserDOBFrom.AddYears(-userAgeFrom).ToShortDateString(); userDOBTo = dtUserDOBFrom.AddYears(-userAgeTo).Date.ToShortDateString(); selectStatement = "select count(*) from USERS where date_of_birth between '" + userDOBFrom + "' and '"+ userDOBTo + "'"; sqlDBConnection.Open(); sqlDataAdapter.SelectCommand.CommandText = selectStatement; numUsers = (int)sqlDataAdapter.SelectCommand.ExecuteScalar(); sqlDBConnection.Close();

    S M M G 4 Replies Last reply
    0
    • T tnook

      Hi, I'm having a problem with an sql select statement from some c# code. The function is meant to count the number of entries in a table where the users dates of birth are between two dates. The two birthday ranges are taken from two integers userAgeFrom and userAgeTo. When I run the code I get the following error: "The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value." The weird thing is if I manually populate the strings userDOBFrom and userDOBTo with dates (eg userDOBTo = "12/09/1971") the select statement works and returns the number of users. When I try to populate the strings from a DateTime object, as below, the function fails. Any help would be much appreciated! Kevin Here is the code: int userAgeFrom = 18; int userAgeTo = 30; int numUsers; string selectStatement; string userDOBFrom; string userDOBTo; DateTime dtUserDOBFrom = DateTime.Now; DateTime dtUserDOBTo = DateTime.Now; userDOBFrom = dtUserDOBFrom.AddYears(-userAgeFrom).ToShortDateString(); userDOBTo = dtUserDOBFrom.AddYears(-userAgeTo).Date.ToShortDateString(); selectStatement = "select count(*) from USERS where date_of_birth between '" + userDOBFrom + "' and '"+ userDOBTo + "'"; sqlDBConnection.Open(); sqlDataAdapter.SelectCommand.CommandText = selectStatement; numUsers = (int)sqlDataAdapter.SelectCommand.ExecuteScalar(); sqlDBConnection.Close();

      S Offline
      S Offline
      Steven Campbell
      wrote on last edited by
      #2

      Hmm, better would be to use command parameters. Otherwise though, I see nothing wrong with your code, other than the misplaced ".Date." in userDOBTo = dtUserDOBFrom.AddYears(-userAgeTo).Date.ToShortDateString();

      T 1 Reply Last reply
      0
      • S Steven Campbell

        Hmm, better would be to use command parameters. Otherwise though, I see nothing wrong with your code, other than the misplaced ".Date." in userDOBTo = dtUserDOBFrom.AddYears(-userAgeTo).Date.ToShortDateString();

        T Offline
        T Offline
        tnook
        wrote on last edited by
        #3

        Hi, thanks for the tip, I've tried it with and without the extra .Date and get the same error message. Can you elaborate on the command parameters? Cheers!

        S 1 Reply Last reply
        0
        • T tnook

          Hi, thanks for the tip, I've tried it with and without the extra .Date and get the same error message. Can you elaborate on the command parameters? Cheers!

          S Offline
          S Offline
          Steven Campbell
          wrote on last edited by
          #4

          Sorry, don't have time for a full explanation. Its in the Help. Basic idea is that instead of quoting the dates, you would put a placeholder there instead, like "... BETWEEN @P1 and @P2". Then you can create and add parameters to your command. When you do it this way, it has the benefit of strong-typing your parameters, which avoids nasty surprises like the one you are getting, as well as sneaky SQL-Injection attacks. It sounds more complex than it is...

          1 Reply Last reply
          0
          • T tnook

            Hi, I'm having a problem with an sql select statement from some c# code. The function is meant to count the number of entries in a table where the users dates of birth are between two dates. The two birthday ranges are taken from two integers userAgeFrom and userAgeTo. When I run the code I get the following error: "The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value." The weird thing is if I manually populate the strings userDOBFrom and userDOBTo with dates (eg userDOBTo = "12/09/1971") the select statement works and returns the number of users. When I try to populate the strings from a DateTime object, as below, the function fails. Any help would be much appreciated! Kevin Here is the code: int userAgeFrom = 18; int userAgeTo = 30; int numUsers; string selectStatement; string userDOBFrom; string userDOBTo; DateTime dtUserDOBFrom = DateTime.Now; DateTime dtUserDOBTo = DateTime.Now; userDOBFrom = dtUserDOBFrom.AddYears(-userAgeFrom).ToShortDateString(); userDOBTo = dtUserDOBFrom.AddYears(-userAgeTo).Date.ToShortDateString(); selectStatement = "select count(*) from USERS where date_of_birth between '" + userDOBFrom + "' and '"+ userDOBTo + "'"; sqlDBConnection.Open(); sqlDataAdapter.SelectCommand.CommandText = selectStatement; numUsers = (int)sqlDataAdapter.SelectCommand.ExecuteScalar(); sqlDBConnection.Close();

            M Offline
            M Offline
            Michael Potter
            wrote on last edited by
            #5

            Try: userDOBFrom.ToShortDateString() userDOBTo.ToShortDateString() when you build the SQL statement. The default ToString() method may generate the wrong syntax. Normally I would insert: MessageBox.Show(selectStatement); To see what is going on in this case.

            1 Reply Last reply
            0
            • T tnook

              Hi, I'm having a problem with an sql select statement from some c# code. The function is meant to count the number of entries in a table where the users dates of birth are between two dates. The two birthday ranges are taken from two integers userAgeFrom and userAgeTo. When I run the code I get the following error: "The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value." The weird thing is if I manually populate the strings userDOBFrom and userDOBTo with dates (eg userDOBTo = "12/09/1971") the select statement works and returns the number of users. When I try to populate the strings from a DateTime object, as below, the function fails. Any help would be much appreciated! Kevin Here is the code: int userAgeFrom = 18; int userAgeTo = 30; int numUsers; string selectStatement; string userDOBFrom; string userDOBTo; DateTime dtUserDOBFrom = DateTime.Now; DateTime dtUserDOBTo = DateTime.Now; userDOBFrom = dtUserDOBFrom.AddYears(-userAgeFrom).ToShortDateString(); userDOBTo = dtUserDOBFrom.AddYears(-userAgeTo).Date.ToShortDateString(); selectStatement = "select count(*) from USERS where date_of_birth between '" + userDOBFrom + "' and '"+ userDOBTo + "'"; sqlDBConnection.Open(); sqlDataAdapter.SelectCommand.CommandText = selectStatement; numUsers = (int)sqlDataAdapter.SelectCommand.ExecuteScalar(); sqlDBConnection.Close();

              M Offline
              M Offline
              mlsteeves
              wrote on last edited by
              #6

              This might be a long shot, but we've had problems like this with different regional settings. The ToShortDateString() "uses formatting information derived from the current culture." Another thing to try (even longer shot), is to make sure that the collation on your server is expecting your dates in the format that ToShortDateString() is returning.

              1 Reply Last reply
              0
              • T tnook

                Hi, I'm having a problem with an sql select statement from some c# code. The function is meant to count the number of entries in a table where the users dates of birth are between two dates. The two birthday ranges are taken from two integers userAgeFrom and userAgeTo. When I run the code I get the following error: "The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value." The weird thing is if I manually populate the strings userDOBFrom and userDOBTo with dates (eg userDOBTo = "12/09/1971") the select statement works and returns the number of users. When I try to populate the strings from a DateTime object, as below, the function fails. Any help would be much appreciated! Kevin Here is the code: int userAgeFrom = 18; int userAgeTo = 30; int numUsers; string selectStatement; string userDOBFrom; string userDOBTo; DateTime dtUserDOBFrom = DateTime.Now; DateTime dtUserDOBTo = DateTime.Now; userDOBFrom = dtUserDOBFrom.AddYears(-userAgeFrom).ToShortDateString(); userDOBTo = dtUserDOBFrom.AddYears(-userAgeTo).Date.ToShortDateString(); selectStatement = "select count(*) from USERS where date_of_birth between '" + userDOBFrom + "' and '"+ userDOBTo + "'"; sqlDBConnection.Open(); sqlDataAdapter.SelectCommand.CommandText = selectStatement; numUsers = (int)sqlDataAdapter.SelectCommand.ExecuteScalar(); sqlDBConnection.Close();

                G Offline
                G Offline
                Grimolfr
                wrote on last edited by
                #7

                I agree with the general consensus. Most likely ToShortDateString() is returning a string representation of a date that SQL Server can't parse. I would use the formatted ToString() instead: userDOBFrom = dtUserDOBFrom.AddYears(-userAgeFrom).ToString("yyyy-MM-dd"). This should put it in an ISO standard format that SQL Server should understand, regardless of culture/regional settings.


                Grim

                (aka Toby)

                MCDBA, MCSD, MCP+SB

                Need a Second Life?[^]

                SELECT * FROM user WHERE clue IS NOT NULL GO

                (0 row(s) affected)

                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