2010-10-19 70 views
4

我在我的android上有一個sqlite數據庫,它帶有一個datetime列,它包含格式爲dd.MM.yyyy的日期。 這不是我的數據庫,我無法更改Dateformat。 我想用字符串來比較數據庫中的日期,該字符串代表了secon日期,但我嘗試的所有內容均失敗。 如何將此列轉換爲有效的可比較日期? date(),dattime()sdfttime()一切都返回NULL。sqlite convert'dd.MM.yyyy'forged String to date

回答

2

你想在數據庫(SQL)或程序代碼中執行此操作嗎?在Java中,你可以使用SimpleDateFormat

SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy"); 
Date d = sdf.parse("21.03.1997"); 
+0

我必須這樣做,像SQL代碼:AN D日期時間(d.objectstop)<='「+ fb_str_date – 2red13 2010-10-19 13:18:11

+0

和'strftime()'也不起作用? – 2010-10-19 13:34:52

+0

沒有strftime需要yyyy-MM-dd格式的輸入值?如果我嘗試它,我也收到零 – 2red13 2010-10-19 15:39:14

1

嘗試此查詢到列更改爲正確的格式文本的日期(假設名爲table和列命名date表):

update table set date = substr(date, 7) || "-" || substr(date,4,2) 
    || "-" || substr(date, 1,2); 

你也可以根據this answer將其轉換爲where子句。

6

我搜索的谷歌「sqlite3的日期DDMMYYYY」和這個職位是最好的解決方案(與只爲我工作在底部列出的職位當中的一個):

問題: 一個SQLite表中包含的日期格式DD/MM/YYYY和需要轉換到SQLite的本機格式YYYY-MM-DD

問題實施例:

sqlite> select id, calendar_day from tbl1; 
id;calendar_day 
4248281;2011-06-19 
4248282;2011-06-19 
4248283;19/06/2011 
4248284;19/06/2011 

解決方案示例:

sqlite> update tbl1 set calendar_day = substr(calendar_day, 7) || "-" || substr(calendar_day,4,2) || "-" || substr(calendar_day, 1,2) where id>4248282; 

結果舉例:

sqlite> select id, calendar_day from tbl1; 
id;calendar_day 
4248281;2011-06-19 
4248282;2011-06-19 
4248283;2011-06-19 
4248284;2011-06-19 

謝謝大家!

其他職位檢查:

  1. Sqlite convert string to date
  2. SOLite:Date formatter in SQLite
  3. How can I convert datetime to date format in SQLite?
  4. Sqlite convert string to date
  5. How to convert a DD-MM-YYYY date format string into a YYYY-MM-DD date format string or into a NSDate object in Objective-C?