Link to home
Start Free TrialLog in
Avatar of formadmirer
formadmirerFlag for United States of America

asked on

VFP 9 DateTime and MySQL

Hi all. I use VFP to connect to a MySQL db.

How would I insert into the db the value "11/9/2010" when the db field is set to DATETIME?
I'm assuming a FP function will be needed to append time as "00:00:00"  in order for the field to accept it?

If so, how then would I retrieve the date from the db for use in a VFP form where only the date in 11/9/2010 format is required?

In short what I'm asking is what are the two FP functions to 1) convert the date to a DATETIME for insertion into the db, and 2) on retrieval, strip the time portion so only the date is left.

Thanks!

Avatar of Pavel Celba
Pavel Celba
Flag of Czechia image

1. Convert the date to datetime: DTOT()

aaa = {^2010.11.09}
bbb = DTOT(aaa)
? bbb

2. Strip the time portion: TTOD()

aaa = {^2010.11.09 23:37:50}
bbb = TTOD(aaa)
? bbb

I would guess the ODBC driver is able to convert some values automatically. So, if you are inserting date to the datetime column then the time part is added automatically and vice versa.

Avatar of formadmirer

ASKER

Thanks. I've been working for hours on this and can't seem to get it right.

I have yet to tackle the retrieving part, I'm still stuck on inserting.

In the csv source file the following dates are provided. Five records total, two with the dates below and the remaining three empty:

11/09/10

11/12/10

When I run the dates through my code below, the wait window shows every one with today's date/time.

I am trying to get it so that IF the supplied date is more than 90 days in the future, datetime for that item is set to datetime() + 90.  It's not working.





DO CASE
	CASE !EMPTY(lcETA)
		ldETA = CTOT(lcETA)
	OTHERWISE
		ldETA = DATETIME()
ENDCASE
	
DO CASE
	CASE ldETA <= DATETIME()
		ldETA = DATETIME()
	CASE ldETA >= (DATETIME() + 90)
		ldETA = (DATETIME() + 90)
	OTHERWISE
		ldETA = DTOT(ldETA)
ENDCASE

WAIT WINDOW "ldETA = " + TTOC(ldETA)

Open in new window

Sorry, forgot - lcETA is the variable assigned to the date from the csv file.

FWIW, if I simply directly insert the date into the MySQL db it doesn't work either.

A character date of '11/12/10', inserted, becomes '12/10/2011 12:00:00 AM' in the MySQL db.
It is probably caused by line 10: ldETA = DATETIME()

Also SET DATE setting could affect results.

The best way you can do it to place SET STEP ON as the first statement of your code. Then you will see what line of your code is executed and what values are in your variables.
ASKER CERTIFIED SOLUTION
Avatar of Pavel Celba
Pavel Celba
Flag of Czechia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks pcelba.

I've finally got both insert working, thanks to your suggestions.

As for retrieval, I simply DTOS the value from the MySQL db, and hacked the string using three different SUBSTR to finally arrive at a usable yyyy-mm-dd string.

Not elegant I know, but it works.

Thanks again!

btw - what is the purpose of the ^ as you used it above?
{^yyyy.mm.dd} is a standard VFP format for date constant - it has date data type and you don't need any conversion.

Similarly {^yyyy.mm.dd hh:mm:ss}  is a datetime literal constant in VFP.

The caret symbol (or Circumflex accent in Unicode terminology) just says "This constant is in unambiguous format". Without that character the constant would be interpreted based on the SET DATE setting during COMPILE time.

To obtain date string in YYYY-MM-DD format you may also use:

SET DATE YMD
SET MARK TO "-"
SET CENTURY ON
? DATE()
? DTOC(DATE())

DTOS() is better in certain situations because it is SET DATE independent.
Just FYI, FoxPro checks date constants and conversions validity and you may affect the strictness of these checks by appropriate SET STRICTDATE TO ... setting. See more: http://msdn.microsoft.com/en-us/library/wbzwk155(VS.80).aspx