Change this date format which is in sqlite db 12/10/11 to 12-10-11 (mm-dd-yy)
I am unable to do so .I am a noob in sqlite and have to parse this value SELECT strftime('%d-%m-%Y',Date) from report
but I am getting null as sqlite db excepts value in mm-dd-yy
so How do I convert format 12/10/11 to 12-10-11 (mm-dd-yy)
.Thanks in advance .Really appreciate the help.
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- Get the NSRange for the visible text after scroll
The short answer:
If you have a text string stored as
"12/10/11"
that you want reported as"12-10-11"
, you should use thereplace(X,Y,Z)
function, to replace occurrences ofY
inX
withZ
. Thus:will return:
The long answer:
First, dates do not actually exist as a proper datatype in SQLite. They're stored as either TEXT, REAL, or INTEGER values. See date and time datatype in SQLite. So it depends upon how your date was stored in the database.
Second, you seem to be implying that you stored the date in a "
mm/dd/yy
" format. That's not a valid/usefulTEXT
format to be storing date/time values (as the date cannot be sorted, cannot used in "greater than" and "less than" operations, cannot be used in SQLite date functions, etc.). You really want to store datetime values in one of the formats listed in the "Time strings" section of the date and time functions document.So, generally you should store your date/time values in one of those formats, use
NSDateFormatter
to convert that to aNSDate
when you retrieve it from the database. And when you want to display the date value in your app, use whatever format you want for output.But, if you don't care that the dates are stored as text strings and are not effectively usable as dates in SQLite, then just treat it as a plain old
TEXT
string and useTEXT
functions, such asreplace(X,Y,Z)
to replace occurrences of"/"
with"-"
, as outlined above.