Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Convert DateTime to a string to send to SQL serv
Message
General information
Forum:
Visual FoxPro
Category:
Other
Environment versions
Visual FoxPro:
VFP 9 SP1
Miscellaneous
Thread ID:
01480183
Message ID:
01480392
Views:
43
Thank you, Rick.

>Here's what I use that seems to work well:
>
>  RETURN "'" + TRANSFORM(TTOC(ltTime,1),"@R 9999-99-99 99:99:99") + "'"
>
>
>This comes from a stringification routine I use for strict dates that transform for Fox or SQL dates for use in SQL statements.
>
>
>************************************************************************
>FUNCTION TimeToCStrict
>****************************************
>***  Function: Returns time in strict time format or in SQL Server
>***            compliant format (llSql flag set).
>***      Pass: Datetime value
>***    Return: strict DateTime string, or SQL Server time string
>************************************************************************
>LPARAMETER ltTime, llSQL
>
>IF llSQL
>  IF EMPTY(ltTime)
>     RETURN "''"
>  ENDIF
>  
>  RETURN "'" + TRANSFORM(TTOC(ltTime,1),"@R 9999-99-99 99:99:99") + "'"
>ENDIF
>
>IF EMPTY(ltTime)
>   RETURN "{^}"
>ENDIF
>
>RETURN "{^" + TRANSFORM(TTOC(ltTime,1),"@R 9999-99-99 99:99:99") + "}"
>
>
>
>FWIW, I would avoid sending dates in string format to SQL Server whenever possible. Use named parameters and a variable instead:
>
>
>ptTimeStamp = Date() - 30
>lcSQL = "select * from someTable where TimeStamp > ?ptTimeStamp"
>SqlExec(lnHandle,lcSql)
>
>
>Prevents SQL injection and is easier to read and maintain.
>
>+++ Rick ---
>
>>Hi,
>>
>>I want to send to the SQL Server the following update command:
>>
>>
>>"update MyTable Set DateTimeColumn = " + TTOC(DateTime()) + " where Pk = " + nPk
>>
>>
>>The TTOC() does not seem to do the job as SQL Server returns an error. What would be the right syntax for sending the current Date and Time to update SQL Serrver column?
>>
>>TIA.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Reply
Map
View

Click here to load this message in the networking platform