简体   繁体   中英

Substring_Index to Date

I have problem,, so i have table let said name is table1, that table1 have filed name UserName. And the value like this " Jiyo-192.168.10.22 Date 31-Aug-2016 Jam 11:35:36 ", so the format value like this &Name&-&ipaddress& Date &datetime& . Cause i wanna UserName became date i use subtring_index like this SUBSTRING_INDEX(SUBSTRING_INDEX(UserName,'Date ',-1),' Jam',1) AS date1 But my problem is i need query where date1 between two date, i just use my code like this

SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(UserName,'Date ',-1),' Jam',1) FROM table1 
WHERE SUBSTRING_INDEX(SUBSTRING_INDEX(UserName,'Date ',-1),' Jam',1) BETWEEN DATE_FORMAT('2016-11-01','%d-%b-%Y') AND DATE_FORMAT('2016-11-18','%d-%b-%Y')

But is not wokring the result showing from 12-Mar-2007 until 17-Nov-2016. Did i get something wrong or substring_index really not working to get value date??

I think you have to use CAST() to perform a BETWEEN like that:

SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(UserName,'Date ',-1),' Jam',1) FROM table1 
  WHERE SUBSTRING_INDEX(SUBSTRING_INDEX(UserName,'Date ',-1),' Jam',1)
    BETWEEN CAST('2016-11-01' AS DATETIME)
      AND CAST('2016-11-18' AS DATETIME)

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