简体   繁体   中英

SQL Convert String to Date

I am trying to find a way of extracting the first part of line of string and separating as a date. The following is an example of some of the data.

17/10/12 lskell Still waiting for one more signature on the 

I have tried casting the whole field as a date, and converting to a date, but these fail?

Would anyone have any ideas?

试试这个MySql

DATE_FORMAT(STR_TO_DATE(SUBSTRING_INDEX(columnname,' ',1), '%d/%m/%y'), '%Y-%m-%d')

(assuming date comes in this format)

-- MYSQL:

SELECT Str_to_Date(Left(yourstring,8),'%d/%m/%y') from yourtable;

-- Oracle

SELECT TO_DATE(left(yourstring,8),'dd/mm/yy')
from your table;

--- sql server
SELECT CONVERT(DATETIME,left(yourstring,8),120) from your table;

If you know that

  • the first "word" in your string will always be a date
  • the date will always be separated from the rest of the string by a space
  • the date will have a consistent format
  • you are working in MySQL

then try this:

SELECT CAST(SUBSTRING_INDEX(myfield, ' ', 1) AS DATE) adate FROM mytable;

尝试:

SELECT SUBSTRING('10/17/12 lskell Still waiting for one more signature on the', 1, 8)

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