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. Use Time-Only in query (with parameters)

Use Time-Only in query (with parameters)

Scheduled Pinned Locked Moved Database
databasetutorialsql-serversysadminregex
2 Posts 2 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
    BenKona
    wrote on last edited by
    #1

    Does anyone know how to use just the time portion of a DateTime object when calling an SQL Query (using SQL Server). I have a field in the database that stores only the Time, and want to do a select based on this field, but it forces me to match the Date portion of the field or it will not work. So, as simple example, if wanted to get all records with time of 9:30am, would use a Command something like: DateTime timeToGet = new DateTime(1900,1,1,9,30,0); SqlCommand command = new SqlCommand(); command.CommandText = "Select * from MyTable where MyTime = @TimeParam"; SqlParameter parameter = new SqlParameter(); parameter.ParameterName = "TimeParam"; parameter.SqlDbType = SqlDbType.DateTime; parameter.Value = timeToGet; command.Parameters.Add(parameter); This will work fine as long as Date portion of field in database = 1/1/1900. But, if date portion is something else then will not find record. Maybe it would work if could pass it a Time-Only object (instead of DateTime object) but not sure if object like that exists, and if did, not sure parameter object could handle it. If anyone has any ideas, please let me know. Thanks, BK

    A 1 Reply Last reply
    0
    • B BenKona

      Does anyone know how to use just the time portion of a DateTime object when calling an SQL Query (using SQL Server). I have a field in the database that stores only the Time, and want to do a select based on this field, but it forces me to match the Date portion of the field or it will not work. So, as simple example, if wanted to get all records with time of 9:30am, would use a Command something like: DateTime timeToGet = new DateTime(1900,1,1,9,30,0); SqlCommand command = new SqlCommand(); command.CommandText = "Select * from MyTable where MyTime = @TimeParam"; SqlParameter parameter = new SqlParameter(); parameter.ParameterName = "TimeParam"; parameter.SqlDbType = SqlDbType.DateTime; parameter.Value = timeToGet; command.Parameters.Add(parameter); This will work fine as long as Date portion of field in database = 1/1/1900. But, if date portion is something else then will not find record. Maybe it would work if could pass it a Time-Only object (instead of DateTime object) but not sure if object like that exists, and if did, not sure parameter object could handle it. If anyone has any ideas, please let me know. Thanks, BK

      A Offline
      A Offline
      anandss
      wrote on last edited by
      #2

      SELECT * FROM mytable WHERE CONVERT(CHAR(8), mytime, 8) = CONVERT(CHAR(8), @TimeParam, 8)

      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