简体   繁体   中英

Convert varchar to date Sql

I have table data like this

date       time       data 
25/09/07   07:49:08   A 
25/09/07   08:49:08   b 
25/09/07   08:50:08   c 
25/09/07   09:49:08   d 
26/09/07   05:21:08   e 
26/09/07   07:10:08   f 
26/09/07   09:21:08   g 

Format date in table sql is varchar

I want to show table like this

date       time      data 
25/09/07   07:49:08   A 
25/09/07   08:49:08   b 
25/09/07   08:50:08   c 
25/09/07   09:49:08   d 

How to query using where date between 25/09/07 00:00:00 and 25/09/07 23:59:00?

This should be very simple as you can just compare strings:

SELECT *
FROM myTable
WHERE date = '25/09/07'
AND time <= '23:59:00'
  • Since you are just checking for one date and not a range, you can just use a string = comparison. Any row with a different date will not be equal.

  • Since time is in 24-hour format arranged hh:MM:ss , you can just do a lexical comparison. That is, for example, "23:58:03" is will be less than "23:59:00", as will "02:03:59".


To select a range of dates, the easiest way would probably be:

SELECT *
FROM myTable
WHERE (CONVERT(datetime, date, 3) >= '2007-09-25'
       AND CONVERT(datetime, date, 3) < '2007-10-25')
OR    (CONVERT(datetime, date, 3) = '2007-10-25' AND time <= '23:59:00')

Just use the '=' operator on the date. You might not need to include the time.

SELECT *
FROM   table1 t1
WHERE  t1.date = '25/09/07'

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM