Depending on the internal encoding of the database or the kind of database it may just be possible to set the encoding through the datbase client. This is what I did with IBM DB2 rather than have to convert encoding in my script. There may be something comparable in the DB you are using. This way I didn't have to worry about which encoding the DB is sending to me. Just add something like this before making the DB connection and issuing SQL.