简体   繁体   中英

SQL: Casting text to date

I have a TEXT column of dates and need to convert them to dates, but the two methods I'm using are not working correctly. See below.

SELECT CAST("12/01/2009" as date);
12

This only returns the first digit before stoping at the '/'.

SELECT DATE("12/01/2009");
Returns nothing

I also tried CONVERT, but I'm using SQLite and it doesn't appear to support it as I'm getting a syntax error. Any suggestions on how to solve this?

Try using STR_TO_DATE function

SELECT STR_TO_DATE('12/01/2009','%m/%d/%Y');

SqLite doesn't have date type. You need to do string manipulation do achieve this.

SELECT CAST('2009-01-12' AS DATE);

Use it. It returns 2014-02-28

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