Problem with SQL Native
Problem with SQL Native
Hello Friends,
With this command in oQry:Query() in MySQL
WHERE cases.date BETWEEN '2022-01-01 00:00:00' AND '2022-01-12 23:59:59';
Return 31 records, the last two with date '2022-01-13 00:47:50'
Using the same Select in HeidiSql, 29 records are returned correctly within the period 01 to 12 January.
Has anyone had this same problem with the native class?
Thanks.
With this command in oQry:Query() in MySQL
WHERE cases.date BETWEEN '2022-01-01 00:00:00' AND '2022-01-12 23:59:59';
Return 31 records, the last two with date '2022-01-13 00:47:50'
Using the same Select in HeidiSql, 29 records are returned correctly within the period 01 to 12 January.
Has anyone had this same problem with the native class?
Thanks.
Oscar Ribeiro
OASyS Informática
Fwh18.02 + xHarbour 1.2.3 + Bcc72
OASyS Informática
Fwh18.02 + xHarbour 1.2.3 + Bcc72
- nageswaragunupudi
- Posts: 10721
- Joined: Sun Nov 19, 2006 5:22 am
- Location: India
- Been thanked: 8 times
- Contact:
Re: Problem with SQL Native
Has anyone had this same problem with the native class?
No.
I will prepare a sample to test and post here soon.
One point, though not relating to your issue:
Do not use BETEEN while dealing with DateTime Fields.
For example, "AND 2022-01-12 23:59:59" excludes timestamp 2022-01-12 23:59:59.100, though this is still 12th Jan.
Better <field> >= <date1> and <field> < ( <date2> + 1 )
Regards
G. N. Rao.
Hyderabad, India
G. N. Rao.
Hyderabad, India
Re: Problem with SQL Native
Thanks Raw,
I modified to:
WHERE date(casos.data)>=date('2022-01-01') AND date(casos.data)<date('2022-01-12')+1;
The result is 31 records, like before.
There are two records in casos.data = date('2022-01-13') in the result.
I don´t know why.
Do you have another idea?
I modified to:
WHERE date(casos.data)>=date('2022-01-01') AND date(casos.data)<date('2022-01-12')+1;
The result is 31 records, like before.
There are two records in casos.data = date('2022-01-13') in the result.
I don´t know why.
Do you have another idea?
Oscar Ribeiro
OASyS Informática
Fwh18.02 + xHarbour 1.2.3 + Bcc72
OASyS Informática
Fwh18.02 + xHarbour 1.2.3 + Bcc72
- nageswaragunupudi
- Posts: 10721
- Joined: Sun Nov 19, 2006 5:22 am
- Location: India
- Been thanked: 8 times
- Contact:
Re: Problem with SQL Native
Is casos.data a DateTime field?
Why do you use Date(casos.data) ?
Please let us know the exact field type of casos.data
Why do you use Date(casos.data) ?
Please let us know the exact field type of casos.data
Regards
G. N. Rao.
Hyderabad, India
G. N. Rao.
Hyderabad, India
Re: Problem with SQL Native
Hi Raw,
The type of field is TIMESTAMP.
If I don´t put date(casps.data) the query does´t work and return all records.
The type of field is TIMESTAMP.
If I don´t put date(casps.data) the query does´t work and return all records.
Oscar Ribeiro
OASyS Informática
Fwh18.02 + xHarbour 1.2.3 + Bcc72
OASyS Informática
Fwh18.02 + xHarbour 1.2.3 + Bcc72
Re: Problem with SQL Native
Hello,
I would like to know if this issue in select with TIMESTAMP field has been fixed in newer versions of FWH.
The same SELECT in SQLRDD and HeidiSQL brings the correct records, but in native mode it brings records from other dates.
Thanks.
I would like to know if this issue in select with TIMESTAMP field has been fixed in newer versions of FWH.
The same SELECT in SQLRDD and HeidiSQL brings the correct records, but in native mode it brings records from other dates.
Thanks.
Oscar Ribeiro
OASyS Informática
Fwh18.02 + xHarbour 1.2.3 + Bcc72
OASyS Informática
Fwh18.02 + xHarbour 1.2.3 + Bcc72
- nageswaragunupudi
- Posts: 10721
- Joined: Sun Nov 19, 2006 5:22 am
- Location: India
- Been thanked: 8 times
- Contact:
Re: Problem with SQL Native
Code: Select all | Expand
"SELECT ...... WHERE casps.data >= '2022-01-01' AND casps.data < '2022-01-13'"
Regards
G. N. Rao.
Hyderabad, India
G. N. Rao.
Hyderabad, India
Re: Problem with SQL Native
Unfortunately it didn't work.
Could the problem be with my DLL version?
Could the problem be with my DLL version?
Oscar Ribeiro
OASyS Informática
Fwh18.02 + xHarbour 1.2.3 + Bcc72
OASyS Informática
Fwh18.02 + xHarbour 1.2.3 + Bcc72
Re: Problem with SQL Native
hi,
did you use
when "compare" TIMESTAMP
as i know ORDER BY use UTC but you want "Local Time" so try
did you use
Code: Select all | Expand
ORDER BY mytimestamp
when "compare" TIMESTAMP

as i know ORDER BY use UTC but you want "Local Time" so try
Code: Select all | Expand
ORDER BY CAST(mytimestamp AS datetime)
greeting,
Jimmy
Jimmy
- nageswaragunupudi
- Posts: 10721
- Joined: Sun Nov 19, 2006 5:22 am
- Location: India
- Been thanked: 8 times
- Contact:
Re: Problem with SQL Native
We created a small table `test_timestamp` on our Fivewin Cloud server, which is accessible to all of us for demo purposes.
This is the full table data.

We want to extract rows with timestamp between 01-02-2022 and 04-02-2022 (both days inclusive). We used where clause like this:
This is the result:

You can yourself run this program. Copy the program given below to \fwh\samples folder and build and run with buildh.bat or buildx.bat.
You can even modify the program and test whatever you like.
This is the structure of the table:
This is the full table data.

We want to extract rows with timestamp between 01-02-2022 and 04-02-2022 (both days inclusive). We used where clause like this:
Code: Select all | Expand
SELECT * FROM test_timestamp WHERE col_timestamp >= '2022-02-01' AND col_timestamp < '2022-02-05'
This is the result:

You can yourself run this program. Copy the program given below to \fwh\samples folder and build and run with buildh.bat or buildx.bat.
Code: Select all | Expand
#include "fivewin.ch"
function Main()
local oCn, oRs
local t
SET DATE ITALIAN
SET CENTURY ON
oCn := FW_DemoDB( 6 )
if oCn == nil
? "connect fail"
return nil
endif
oCn:lShowErrors := .t.
oRs := oCn:RowSet( "SELECT * FROM test_timestamp" )
XBROWSER oRs TITLE "FULL TABLE"
oRs:Close()
oRs := oCn:RowSet( "SELECT * FROM test_timestamp WHERE col_timestamp >= '2022-02-01' AND col_timestamp < '2022-02-05'" )
XBROWSER oRs TITLE "01 FEB TO 04 FEB"
oRs:Close()
oCn:Close()
return nil
You can even modify the program and test whatever you like.
This is the structure of the table:
Code: Select all | Expand
CREATE TABLE `test_timestamp` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`COL_DATETIME` DATETIME(3) DEFAULT NULL,
`COL_TIMESTAMP` TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Regards
G. N. Rao.
Hyderabad, India
G. N. Rao.
Hyderabad, India
- nageswaragunupudi
- Posts: 10721
- Joined: Sun Nov 19, 2006 5:22 am
- Location: India
- Been thanked: 8 times
- Contact:
Re: Problem with SQL Native
Jimmy wrote:hi,
did you useCode: Select all | Expand
ORDER BY mytimestamp
when "compare" TIMESTAMP![]()
as i know ORDER BY use UTC but you want "Local Time" so tryCode: Select all | Expand
ORDER BY CAST(mytimestamp AS datetime)
It is not necessary to ORDER BY THE field for setting WHERE clause on that field.
We need to consider UTC offset if and only if we are accessing the same database over internet from different timezones/countries.
Regards
G. N. Rao.
Hyderabad, India
G. N. Rao.
Hyderabad, India
- nageswaragunupudi
- Posts: 10721
- Joined: Sun Nov 19, 2006 5:22 am
- Location: India
- Been thanked: 8 times
- Contact:
Re: Problem with SQL Native
Jimmy wrote:hi,
did you useCode: Select all | Expand
ORDER BY mytimestamp
when "compare" TIMESTAMP![]()
as i know ORDER BY use UTC but you want "Local Time" so tryCode: Select all | Expand
ORDER BY CAST(mytimestamp AS datetime)
It is not necessary to ORDER BY THE field for setting WHERE clause on that field.
We need to consider UTC offset if and only if we are accessing the same database over internet from different timezones/countries.
Regards
G. N. Rao.
Hyderabad, India
G. N. Rao.
Hyderabad, India
- Rick Lipkin
- Posts: 2668
- Joined: Fri Oct 07, 2005 1:50 pm
- Location: Columbia, South Carolina USA
Re: Problem with SQL Native
Jimmy
I know you mentioned Sql Native .. MS Access has a bit different syntax .. you have to put # around your date variables
Otherwise
You have to use ' ' around your date variables.
Rick Lipkin
I know you mentioned Sql Native .. MS Access has a bit different syntax .. you have to put # around your date variables
Code: Select all | Expand
"[InvoiceDate] >= #"+dtoc(lDate)+"# And [InvoiceDate] <= #"+dtoc(hDate)+"#"
Otherwise
Code: Select all | Expand
"[InvoiceDate] >= '"+DTOC(LDATE)+"' and [InvoiceDate] <= '"+DTOC(HDATE)+"'"
You have to use ' ' around your date variables.
Rick Lipkin
- nageswaragunupudi
- Posts: 10721
- Joined: Sun Nov 19, 2006 5:22 am
- Location: India
- Been thanked: 8 times
- Contact:
Re: Problem with SQL Native
Rick Lipkin wrote:Jimmy
I know you mentioned Sql Native .. MS Access has a bit different syntax .. you have to put # around your date variablesCode: Select all | Expand
"[InvoiceDate] >= #"+dtoc(lDate)+"# And [InvoiceDate] <= #"+dtoc(hDate)+"#"
OtherwiseCode: Select all | Expand
"[InvoiceDate] >= '"+DTOC(LDATE)+"' and [InvoiceDate] <= '"+DTOC(HDATE)+"'"
You have to use ' ' around your date variables.
Rick Lipkin
DTOC() does not work for MySql.
Another thing, there a few things which work for Americans but not others in the world. because some database servers accept American notation of dates.
For example your syntax of DTOC() does not work for me in India even on MSACCESS.
In these forums, please post solutions which are Universal.
Regards
G. N. Rao.
Hyderabad, India
G. N. Rao.
Hyderabad, India
Re: Problem with SQL Native
Raw,
I tried to run your example but returned the error: 1045 Acess denied for user 'sql7148817@'189-46-180-40.dsl.telesp.net.br (using password: YES).
Thanks
I tried to run your example but returned the error: 1045 Acess denied for user 'sql7148817@'189-46-180-40.dsl.telesp.net.br (using password: YES).
Thanks
Oscar Ribeiro
OASyS Informática
Fwh18.02 + xHarbour 1.2.3 + Bcc72
OASyS Informática
Fwh18.02 + xHarbour 1.2.3 + Bcc72