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. How do I query dates stored in a varchar field?

How do I query dates stored in a varchar field?

Scheduled Pinned Locked Moved Database
questiondatabasehelp
8 Posts 3 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.
  • I Offline
    I Offline
    Icharus
    wrote on last edited by
    #1

    Hi everyone, I am attempting to read records from a table based on a date range, however, the dates are stored in a varchar field. The format of the date is 2005-09-01 10:50:00. How can I get around the fact that the vendor used the wrong data type? Your help is greatly appreciated.

    R 1 Reply Last reply
    0
    • I Icharus

      Hi everyone, I am attempting to read records from a table based on a date range, however, the dates are stored in a varchar field. The format of the date is 2005-09-01 10:50:00. How can I get around the fact that the vendor used the wrong data type? Your help is greatly appreciated.

      R Offline
      R Offline
      Rob Graham
      wrote on last edited by
      #2

      Depends on what daabase you are using. If it's MSSQL you could cast/convert the varchar to a date before comparing it in the query: select * from [PoorlyDesignedTable] where Cast([ShouldBeADateTimeField] as Datetime) = MyDate Absolute faith corrupts as absolutely as absolute power Eric Hoffer All that is necessary for the triumph of evil is that good men do nothing. Edmund Burke

      I A 2 Replies Last reply
      0
      • R Rob Graham

        Depends on what daabase you are using. If it's MSSQL you could cast/convert the varchar to a date before comparing it in the query: select * from [PoorlyDesignedTable] where Cast([ShouldBeADateTimeField] as Datetime) = MyDate Absolute faith corrupts as absolutely as absolute power Eric Hoffer All that is necessary for the triumph of evil is that good men do nothing. Edmund Burke

        I Offline
        I Offline
        Icharus
        wrote on last edited by
        #3

        Rob thanks for your quick response! Yes it is MSSQL 2000. Now please pardon my ignorance, but, if I have records with dates greater than 9/1/05, the following statement should return all those records rigt? SELECT field7, field13, field14 FROM myTable WHERE (CAST(field14 AS Datetime) >= '2005-09-01') AND (field7 = 'Work Order') But my result set is empty. What am I missing?

        A 2 Replies Last reply
        0
        • I Icharus

          Rob thanks for your quick response! Yes it is MSSQL 2000. Now please pardon my ignorance, but, if I have records with dates greater than 9/1/05, the following statement should return all those records rigt? SELECT field7, field13, field14 FROM myTable WHERE (CAST(field14 AS Datetime) >= '2005-09-01') AND (field7 = 'Work Order') But my result set is empty. What am I missing?

          A Offline
          A Offline
          airbus380
          wrote on last edited by
          #4

          SQL Server will return those records for which CAST(field14 AS Datetime) is greather than '2005-09-01' only if value for filed7 is equal with 'Work Order' Your result may be empty if settings for database COLLATION or field7 COLLATION is case sensitive CS. Try to use this condition: ... AND UPPER(field7) = 'WORK ORDER' Be attention at spaces between WORK and ORDER ! Also, the condition for date may be rewrite thus: MONTH( field4 ) >= 9 AND YEAR( field4 ) >= 2005

          I 1 Reply Last reply
          0
          • R Rob Graham

            Depends on what daabase you are using. If it's MSSQL you could cast/convert the varchar to a date before comparing it in the query: select * from [PoorlyDesignedTable] where Cast([ShouldBeADateTimeField] as Datetime) = MyDate Absolute faith corrupts as absolutely as absolute power Eric Hoffer All that is necessary for the triumph of evil is that good men do nothing. Edmund Burke

            A Offline
            A Offline
            airbus380
            wrote on last edited by
            #5

            If all the values from field4 are in Y-M-D HH:MM format then SQL Server will implicit conversion VARCHAR field to DATETIME.

            A 1 Reply Last reply
            0
            • A airbus380

              If all the values from field4 are in Y-M-D HH:MM format then SQL Server will implicit conversion VARCHAR field to DATETIME.

              A Offline
              A Offline
              airbus380
              wrote on last edited by
              #6

              See SQL Server Books Online topic: CAST and CONVERT

              1 Reply Last reply
              0
              • I Icharus

                Rob thanks for your quick response! Yes it is MSSQL 2000. Now please pardon my ignorance, but, if I have records with dates greater than 9/1/05, the following statement should return all those records rigt? SELECT field7, field13, field14 FROM myTable WHERE (CAST(field14 AS Datetime) >= '2005-09-01') AND (field7 = 'Work Order') But my result set is empty. What am I missing?

                A Offline
                A Offline
                airbus380
                wrote on last edited by
                #7

                What is datetime format setting for your server ? Use DBCC USEROPTIONS in SQL Analyzer ! VB6,C#

                1 Reply Last reply
                0
                • A airbus380

                  SQL Server will return those records for which CAST(field14 AS Datetime) is greather than '2005-09-01' only if value for filed7 is equal with 'Work Order' Your result may be empty if settings for database COLLATION or field7 COLLATION is case sensitive CS. Try to use this condition: ... AND UPPER(field7) = 'WORK ORDER' Be attention at spaces between WORK and ORDER ! Also, the condition for date may be rewrite thus: MONTH( field4 ) >= 9 AND YEAR( field4 ) >= 2005

                  I Offline
                  I Offline
                  Icharus
                  wrote on last edited by
                  #8

                  Thanks everyone for your help! I ended up using the following statement: WHERE (UPPER(field7) = 'WORK ORDER') AND (field14 BETWEEN @startdate AND @enddate) Following airbus' suggestion to use UPPER, and documentation I found in SQL Sever Bible Thanks again!

                  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