Results 1 to 6 of 6

Thread: SQL SELECT with DateTime

  1. #1
    Join Date
    Nov 2008
    Posts
    85

    SQL SELECT with DateTime

    hi there

    I have a MS Access database table with a DateTime field in it. The following INSERT statement works:

    INSERT INTO Table VALUES(...., '4/26/2007 5:25:59 PM', ....)

    but the SELECT statement never works when I specified the same string (it gives me Data Type Mismatch error):

    SELECT * FROM Table WHERE--- AND Time = '4/26/2007 5:25:59 PM' ...

    What is the correct way to do this?

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: SQL SELECT with DateTime

    Time" is a reserved word AND a function in access and shouldn't be used by itself as a field name, if possible, change it to TheTime or MyTime. Otherwise you need to surround it with brackets as shown.

    The reason your insert statement works is because you're inserting the date as a text string, but happens to fit the General Date/Time format.

    The select doesn't work the same way, it's value based and therefor the criteria has to match the data type for the field. Surrounding the date/time with # # tells Access Jet that it's a date.

    SELECT * FROM Table WHERE .... AND [Time] = #4/26/2007 5:25:59 PM# ...

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: SQL SELECT with DateTime

    Create Table #Temp(DateTimeColumn DateTime)

    Insert Into #Temp
    Select '3/23/2006' Union All
    Select '3/24/2006' Union All
    Select '3/24/2006 3:33:25 PM'

    -- This returns 2 records because of the >
    Select *
    From #Temp
    Where DateTimeColumn > '3/23/2006'

    -- This returns 3 records because of the >=
    Select *
    From #Temp
    Where DateTimeColumn >= '3/23/2006'

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: SQL SELECT with DateTime

    select slot_item.* from item_master, slot_item WITH (NOLOCK)
    WHERE (slot_item.sku_id=item_master.sku_id
    AND slot_item.sku_id <> 0 AND item_master.whse_code = 'SE')
    AND ( ((slot_item.slot_id is null)
    -- and (item_master.ex_recpt_date>={d '2006-03-23'})
    and (item_master.ex_recpt_date<={d '2006-04-28'})
    and ( slot_item.sku_id
    IN ( SELECT sku_id FROM item_cat_xref WITH (NOLOCK)
    WHERE cat_code_id=8) ) ) )
    ORDER BY item_master.sku_name ASC

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: SQL SELECT with DateTime

    create table tt (d datetime)
    insert into tt
    select '3/23/2006 9:18:57 PM' union all
    select '3/24/2006 0:00:00 AM'

    select * from tt

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: SQL SELECT with DateTime

    SELECT * FROM DATE_SAMPLE WHERE SAMPLE_DATE = '2003-04-09

    SELECT * FROM DATE_SAMPLE WHERE SAMPLE_DATE between '2003-04-09'
    AND '2009-02-10'
    SELECT * FROM DATE_SAMPLE
    WHERE SAMPLE_DATE >= '2009-02-09'
    AND SAMPLE_DATE <'2009-02-10'

Similar Threads

  1. Datetime Perl module not installing
    By Wampanoag in forum Software Development
    Replies: 6
    Last Post: 19-06-2010, 12:31 AM
  2. How to convert string into DateTime
    By Segvoia in forum Software Development
    Replies: 7
    Last Post: 05-03-2010, 02:55 PM
  3. Compare two DateTime objects
    By teena_pansare in forum Software Development
    Replies: 3
    Last Post: 28-11-2009, 05:38 PM
  4. out-of-range datetime value
    By MobilePhoneGuru in forum Software Development
    Replies: 3
    Last Post: 09-10-2009, 10:05 AM
  5. DateTime Format String
    By Zool in forum Software Development
    Replies: 3
    Last Post: 17-07-2009, 03:20 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,879,973.31945 seconds with 17 queries