2013-03-22 75 views
1

我可以用strtime功能SQLITE日期格式轉換爲任何格式的字符串 即字符串轉換爲日期SQLite中

select strftime('%Y-%d-%m-%Y', '2012-09-13 12:44:22'); 

但我有字符串,它是在dd-MM-yyyy格式,我怎麼能轉換爲yyyy-MM-dd格式sqlite,這樣我就可以在日期比較中使用它。

+1

的可能的複製http://stackoverflow.com/questions/3968618/sqlite-轉換-MM-DD-YYYY-格式化字符串最新 – 2013-03-22 05:58:44

回答

8

使用這樣的事情:

select datetime(substr(col, 7, 4) || '-' || substr(col, 4, 2) || '-' || substr(col, 1, 2)) from table; 

其中山坳是具有YYYY-MM-DD格式的日期的

2

你可以試試這個

String date = "23-03-2013"; 
try { 
    SimpleDateFormat format = new SimpleDateFormat(""dd-MM-yyyy"); 

    SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd"); 

    date = df2.format(format.parse(str)); 

} catch (java.text.ParseException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
0

我有一個類似的問題,其中日期字符串不一致輸入,所以月份的長度天,年不一致。下面是我做的轉換不一致格式爲M/d/YYYY始終如一格式化字符串日期YYYY-MM-DD:

SELECT "DISP DATE", DATE(year||'-'||month||'-'||day) as dt_frmtd 
    FROM 
    (
    SELECT * 
, CASE WHEN LENGTH(substr("DISP DATE", 1, instr("DISP DATE",'/')-1)) = 2 
     THEN substr("DISP DATE", 1, instr("DISP DATE",'/')-1) 
     ELSE '0'|| substr("DISP DATE", 1, instr("DISP DATE",'/')-1) 
     END as month 
, CASE WHEN LENGTH(substr(substr("DISP DATE", instr("DISP DATE",'/')+1), 1, instr(substr("DISP DATE", instr("DISP DATE",'/')+1),'/')-1)) = 2 
     THEN substr(substr("DISP DATE", instr("DISP DATE",'/')+1), 1, instr(substr("DISP DATE", instr("DISP DATE",'/')+1),'/')-1) 
     ELSE '0'|| substr(substr("DISP DATE", instr("DISP DATE",'/')+1), 1, instr(substr("DISP DATE", instr("DISP DATE",'/')+1),'/')-1) 
     END AS day   
, CASE WHEN LENGTH(substr(substr("DISP DATE", instr("DISP DATE",'/')+1), instr(substr("DISP DATE", instr("DISP DATE",'/')+1),'/')+1)) = 4 
     THEN substr(substr("DISP DATE", instr("DISP DATE",'/')+1), instr(substr("DISP DATE", instr("DISP DATE",'/')+1),'/')+1) 
     ELSE '20'|| substr(substr("DISP DATE", instr("DISP DATE",'/')+1), instr(substr("DISP DATE", instr("DISP DATE",'/')+1),'/')+1) 
     END AS year 
    FROM DISP 
    )