2

Not sure how to change a date in my csv file when importing into MySQL that is in 12/1/2011(mm/dd/yyyy) format. MySQL is in yyyy/mm/dd.

How can I do this?

My rake file

require 'csv'
require 'date'

desc "Import gac from csv file"
task :import => [:environment] do
  Dir.chdir("#{Rails.root}/lib/assets")
  csv_file = "file.csv" 
  CSV.foreach(csv_file, :headers => true) do |row|
    Institution.create({
      :company => row[0],
      :solveid => row[2],
      :phone => row[5],
      :other => row[6],
      :clientdate => (DateTime.strptime row[7], "%m/%d/%Y").strftime "%Y/%m/%d", 
      :cunumber => row[1],
      :street => row[8], 
      :city => row[9], 
      :state_id => row[4], 
      :zip => row[10],
      :source => row[11], 
      :source2 => row[12],
      :demodate1 => (DateTime.strptime row[13], "%m/%d/%Y").strftime "%Y/%m/%d",
      :demodate2 => (DateTime.strptime row[14], "%m/%d/%Y").strftime "%Y/%m/%d",
      :demodate3 => (DateTime.strptime row[15], "%m/%d/%Y").strftime "%Y/%m/%d",
      :client => row[17],
      :prospect => row[19], 
      :alliedlead => row[16], 
      :notcontacted => row[18]
    })
  end
end

This is giving me an error.

rake import
rake aborted!
/Users/dave/rails_projects/allied_contest/lib/tasks/intitutions.rake:14: syntax error, unexpected tSTRING_BEG, expecting '}'
... row[7], "%m/%d/%Y").strftime "%Y/%m/%d", 
...                               ^
/Users/dave/rails_projects/allied_contest/lib/tasks/intitutions.rake:14: syntax error, unexpected ',', expecting keyword_end
...%m/%d/%Y").strftime "%Y/%m/%d", 
...                               ^

2 Answers 2

3
   if row[7].present?
      date = row[7].split("/")
      new_date = "#{date[2]}/#{date[0]}/#{date[1]}"
   else
     new_date = "N/A"
   end

EDIT: The error you are getting is because the row is nil. I've added a check for nil.

Sign up to request clarification or add additional context in comments.

3 Comments

John, How would I do this with a row? Should it be row[7] = row[7].split("/") or something similar?
John getting the following: rake aborted! undefined method `split' for nil:NilClass
updated my answer. There must be some rows in your csv file that do not have dates.
2

If you need to validate the dates then you can use the DateTime::strptime and DateTime#strftime methods:

require 'date'

# ... snip ...
:clientdate => DateTime.strptime(row[7], "%m/%d/%Y").strftime("%Y/%m/%d")

6 Comments

Sean...I keep getting the following error: rake aborted! /Users/dave/rails_projects/allied_contest/lib/tasks/intitutions.rake:14: syntax error, unexpected tSTRING_BEG, expecting '}' ... row[7], "%m/%d/%Y").strftime "%Y/%m/%d", ... ^ /Users/dave/rails_projects/allied_contest/lib/tasks/intitutions.rake:14: syntax error, unexpected ',', expecting keyword_end ...%m/%d/%Y").strftime "%Y/%m/%d",
@DaveG - This may be one of those cases where you need parenthesis for method calls - I updated my answer.
I'm sure this works. I guess I need to check for nil values since the date isn't on all fields. Following error: "can't convert nil into String"
You can handle nil case this way: :clientdate => row[7].nil? ? nil : DateTime.strptime(row[7], "%m/%d/%Y").strftime("%Y/%m/%d")
Thanks Sean, both great answers, and both work. I felt like this was more concise. Thanks Again!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.