My database currently has a column which looks like this:
id | timestamp
-- | ---------
1 | 07/29/2014
2 | 07/30/2014
3 | 07/30/2014
4 | 2014-07-30
5 | 2014-07-30
I am trying to format any rows of timestamp in the old format, to the new format
in this example, rows 1-3 need to be reformatted
07/29/2014 -> 2014-07-29
07/30/2014 -> 2014-07-30
07/30/2014 -> 2014-07-30
I am not that familiar with SQL/SQLite but and from to come up with some sql i can execute once to see if there are any rows in the old format, and if they are, replace them with the equivalent of the new format.
any help would be greatly appreciated.
I believe I am trying to do what this user here wrote but couldn't get it to work
https://stackoverflow.com/a/7781473/433866
Thanks!
Here are the results I am getting with some real data:
timestamp column old / timestamp converted
07/25/2014 -> 01/04/0031
05/21/2014 -> 11/04/0026
01/25/2013 -> 07/06/0030
09/25/2000 -> 02/21/0031
07/25/2015 -> 01/05/0031
07/25/2016 -> 01/06/0031
07/25/1999 -> 12/20/0030
07/25/1986 -> 12/07/0030
and here is the method i am using
// REFORMAT DATES
public void reformatDates(String table){
SQLiteDatabase db = this.getWritableDatabase();
String selectQuery = "SELECT * FROM " + table;
Cursor cursor = db.rawQuery(selectQuery, null);
cursor.moveToFirst();
if (!cursor.isAfterLast()) {
do {
db.execSQL("update Food set timestamp = substr(timestamp,4,2) || '-' || substr(timestamp, 1,2) || '-' || substr(timestamp, 7) where timestamp LIKE '%/%'");
}
while (cursor.moveToNext());
}
cursor.close();
db.close();
}