0

I'm inserting some strings (that look dates) into a sqlite3 database via my ActiveRecord and the database is altering the field and making it look like a date. For example:

"4-2" => "2-Apr"  #Not only a date but the order has been reversed.

"6-7-12" => "7/12/2006"  #Again not only a date but the order has been changed.

I looked for a ruby string function that would prevent this from happening but haven't been able to come up with anything.

Has anyone else come across something like this and are you aware of any workarounds or fixes.

Mutuelinvestor
  • 3,384
  • 10
  • 44
  • 75
  • Is the column that you are storing the strings in a date column? sqlite is going to try to coerce whatever string you send into a date. If you want to store the literals, you'll need to put them in a string column. – Marc Talbot Feb 21 '12 at 14:53
  • Thanks Marc, they actually are string columns. – Mutuelinvestor Feb 21 '12 at 17:52

1 Answers1

1

If you can predict the format of the string, use the following code to parse the string into date object before saving it.

Date.strptime '6-7-12', '%m-%d-%y'
=> Thu, 07 Jun 2012
nkm
  • 5,844
  • 2
  • 24
  • 38