简体   繁体   中英

Format Date in OleDB Query

I'm trying to find a way to correctly format a Date in a SELECT statement that loads data from an old Visual Fox Pro Database. I need to do this so that I can load it into a CSV, then load it into MySQL, which takes the date format 'yyyy-MM-dd'.

This is my query at the moment;

var dbfCmd = new OleDbCommand(@"SELECT ct_id, ct_cmpid, ct_empid, ct_pplid, ct_cntid, ct_pplnm, ct_date, ct_time, ct_type, ct_doneby, ct_desc FROM contacts", dbfCon);

I need to find a way to format the ct_date column. I've tried a number of ways so far, including using FDATE and FORMAT , but nothing has worked so far. I've looked through the supported commands for OleDB but still haven't come across anything.

Can anyone inform me of the correct way to format the Date query so that I can import to MySQL?

Do not format the date on the server.

Receive as a date column, then read as a DateTime value when enumerating the query. Finally format it on your client in the specify way when writing the file.

Although it has been suggested to convert the data in C# to date format, if you REALLY want to pull the date formatted from VFP OleDb query, you could do this for your date column

STUFF( STUFF( DTOS( ct_date ), 5, 0, "-"), 8, 0, "-" )

The VFP Function DTOS will convert a date (or datetime) to a string and will always be in the format of YYYYMMDD. The STUFF command will do the inserting of the hyphen character to properly set to YYYY-MM-DD for you.

You could simply use ToString("yyyy-MM-dd") but better do not even do that. You could directly connect to MySQL and insert data into it using VFP commands and call that via an ExecScript. Or you could directly import from VFP within MySQL connection (I don't use MySQL but you can do that with other databases like MS SQL Server, postgreSQL etc so I assume MySQL is also capable doing that). Yet another alternative would be to use an XML format to import/export which is much more reliable than plain text. Yet another way, you could connect to MySQL and VFP, fill MySQL DataTable with your data and submit changes.

IOW using a text file in between would be my last suggestion.

PS: Have a look at FileHelpers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM