Page 1 of 1

Safe way to pass a date time value to string in a query

Posted: Wed 25 Mar 2020 06:18
by donaldshimoda
I need to include in a query a date time value.
Need it works on any datetime format, in windows and linux.

Sample

where adate = ''+ dateformated+''

Some regional meetings have mm/dd/yy and another have dd/mm/yy

Theres any way to know which is the expect date format string for postgres server? some way to interrogate the server?

Or any other suggestion?

Bets regards

Re: Safe way to pass a date time value to string in a query

Posted: Wed 25 Mar 2020 10:12
by ertank
Hello,

It is not known your database and version.

There is an SQL standard for date time values.

yyyy-mm-dd hh:mm:ss

It supposed to work in all cases if you format your datetime always like above. That is valid for FirebirdSQL, PostgreSQL, MySQL, SQL Server at least.

SQLite it works if you also insert your records like in above format as SQLite does not have any specific datetime data type.

Re: Safe way to pass a date time value to string in a query

Posted: Wed 25 Mar 2020 11:58
by donaldshimoda
Thanks will try.

Do you mean

'yyyy-mm-dd hh:nn:ss'

rigth?

Re: Safe way to pass a date time value to string in a query

Posted: Wed 25 Mar 2020 12:30
by ertank
I meant to format your date/date time like that and pass as if it is a string parameter (with single quotes)

Today is 25th of March, 2020 15:29

That should be formatted as
2020-03-25 15:29:00

And passed as a string parameter to query.

That format normally accepted by common sql servers. Almost all of them

Re: Safe way to pass a date time value to string in a query

Posted: Wed 25 Mar 2020 12:34
by donaldshimoda
Exactly, but the format to convert is that

'yyyy-mm-dd hh:nn:ss'

and not that you write

yyyy-mm-dd hh:mm:ss

for that reason I pointed out to serve other read it in a future.

Thanks
best regards.

Re: Safe way to pass a date time value to string in a query

Posted: Thu 30 Apr 2020 08:11
by MaximG
Ertrank is right. When passing the date-time parameter as a string, database server settings will be applied rather than the local system settings. The 'yyyy-mm-dd hh:nn:ss' format is supported in many database servers.