FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Problem with SQL Native
Posts: 187
Joined: Mon Oct 20, 2008 06:33 PM
Problem with SQL Native
Posted: Wed Feb 02, 2022 12:52 PM

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.

Oscar Ribeiro

OASyS Informática

Fwh18.02 + xHarbour 1.2.3 + Bcc72
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Problem with SQL Native
Posted: Wed Feb 02, 2022 03:50 PM

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
Posts: 187
Joined: Mon Oct 20, 2008 06:33 PM
Re: Problem with SQL Native
Posted: Wed Feb 02, 2022 04:37 PM

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?

Oscar Ribeiro

OASyS Informática

Fwh18.02 + xHarbour 1.2.3 + Bcc72
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Problem with SQL Native
Posted: Fri Feb 04, 2022 04:20 AM

Is casos.data a DateTime field?
Why do you use Date(casos.data) ?
Please let us know the exact field type of casos.data

Regards



G. N. Rao.

Hyderabad, India
Posts: 187
Joined: Mon Oct 20, 2008 06:33 PM
Re: Problem with SQL Native
Posted: Fri Feb 04, 2022 02:24 PM

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.

Oscar Ribeiro

OASyS Informática

Fwh18.02 + xHarbour 1.2.3 + Bcc72
Posts: 187
Joined: Mon Oct 20, 2008 06:33 PM
Re: Problem with SQL Native
Posted: Sat Feb 05, 2022 12:14 PM

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.

Oscar Ribeiro

OASyS Informática

Fwh18.02 + xHarbour 1.2.3 + Bcc72
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Problem with SQL Native
Posted: Sat Feb 05, 2022 06:05 PM
Code (fw): Select all Collapse
"SELECT ...... WHERE casps.data >= '2022-01-01' AND casps.data < '2022-01-13'"
Regards



G. N. Rao.

Hyderabad, India
Posts: 187
Joined: Mon Oct 20, 2008 06:33 PM
Re: Problem with SQL Native
Posted: Sun Feb 06, 2022 10:30 AM

Unfortunately it didn't work.
Could the problem be with my DLL version?

Oscar Ribeiro

OASyS Informática

Fwh18.02 + xHarbour 1.2.3 + Bcc72
Posts: 1772
Joined: Thu Sep 05, 2019 05:32 AM
Re: Problem with SQL Native
Posted: Sun Feb 06, 2022 11:26 AM
hi,

did you use
Code (fw): Select all Collapse
ORDER BY mytimestamp

when "compare" TIMESTAMP :-)

as i know ORDER BY use UTC but you want "Local Time" so try
Code (fw): Select all Collapse
ORDER BY CAST(mytimestamp AS datetime)
greeting,

Jimmy
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Problem with SQL Native
Posted: Mon Feb 07, 2022 02:01 PM
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:
Code (fw): Select all Collapse
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 (fw): Select all Collapse
#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 (fw): Select all Collapse
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
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Problem with SQL Native
Posted: Mon Feb 07, 2022 02:09 PM
Jimmy wrote:hi,

did you use
Code (fw): Select all Collapse
ORDER BY mytimestamp

when "compare" TIMESTAMP :-)

as i know ORDER BY use UTC but you want "Local Time" so try
Code (fw): Select all Collapse
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
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Problem with SQL Native
Posted: Mon Feb 07, 2022 02:09 PM
Jimmy wrote:hi,

did you use
Code (fw): Select all Collapse
ORDER BY mytimestamp

when "compare" TIMESTAMP :-)

as i know ORDER BY use UTC but you want "Local Time" so try
Code (fw): Select all Collapse
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
Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Re: Problem with SQL Native
Posted: Mon Feb 07, 2022 02:12 PM
Jimmy

I know you mentioned Sql Native .. MS Access has a bit different syntax .. you have to put # around your date variables

Code (fw): Select all Collapse
 "[InvoiceDate] >= #"+dtoc(lDate)+"# And [InvoiceDate] <= #"+dtoc(hDate)+"#"


Otherwise
Code (fw): Select all Collapse
 "[InvoiceDate] >= '"+DTOC(LDATE)+"' and [InvoiceDate] <= '"+DTOC(HDATE)+"'"


You have to use ' ' around your date variables.

Rick Lipkin
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Problem with SQL Native
Posted: Mon Feb 07, 2022 02:23 PM
Rick Lipkin wrote:Jimmy

I know you mentioned Sql Native .. MS Access has a bit different syntax .. you have to put # around your date variables

Code (fw): Select all Collapse
 "[InvoiceDate] >= #"+dtoc(lDate)+"# And [InvoiceDate] <= #"+dtoc(hDate)+"#"


Otherwise
Code (fw): Select all Collapse
 "[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
Posts: 187
Joined: Mon Oct 20, 2008 06:33 PM
Re: Problem with SQL Native
Posted: Mon Feb 07, 2022 04:56 PM

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

Oscar Ribeiro

OASyS Informática

Fwh18.02 + xHarbour 1.2.3 + Bcc72