Problem with SQL Native

User avatar
ORibeiro
Posts: 187
Joined: Mon Oct 20, 2008 6:33 pm
Location: Itu, Sao Paulo, Brazil

Problem with SQL Native

Post by ORibeiro »

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
User avatar
nageswaragunupudi
Posts: 10721
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 8 times
Contact:

Re: Problem with SQL Native

Post by nageswaragunupudi »

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
User avatar
ORibeiro
Posts: 187
Joined: Mon Oct 20, 2008 6:33 pm
Location: Itu, Sao Paulo, Brazil

Re: Problem with SQL Native

Post by ORibeiro »

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
User avatar
nageswaragunupudi
Posts: 10721
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 8 times
Contact:

Re: Problem with SQL Native

Post by nageswaragunupudi »

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
User avatar
ORibeiro
Posts: 187
Joined: Mon Oct 20, 2008 6:33 pm
Location: Itu, Sao Paulo, Brazil

Re: Problem with SQL Native

Post by ORibeiro »

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
User avatar
ORibeiro
Posts: 187
Joined: Mon Oct 20, 2008 6:33 pm
Location: Itu, Sao Paulo, Brazil

Re: Problem with SQL Native

Post by ORibeiro »

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
User avatar
nageswaragunupudi
Posts: 10721
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 8 times
Contact:

Re: Problem with SQL Native

Post by nageswaragunupudi »

Code: Select all | Expand


"SELECT ...... WHERE casps.data >= '2022-01-01' AND casps.data < '2022-01-13'"
 
Regards

G. N. Rao.
Hyderabad, India
User avatar
ORibeiro
Posts: 187
Joined: Mon Oct 20, 2008 6:33 pm
Location: Itu, Sao Paulo, Brazil

Re: Problem with SQL Native

Post by ORibeiro »

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
User avatar
Jimmy
Posts: 1740
Joined: Thu Sep 05, 2019 5:32 am
Location: Hamburg, Germany
Has thanked: 2 times

Re: Problem with SQL Native

Post by Jimmy »

hi,

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
User avatar
nageswaragunupudi
Posts: 10721
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 8 times
Contact:

Re: Problem with SQL Native

Post by nageswaragunupudi »

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.

Image

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:
Image

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
User avatar
nageswaragunupudi
Posts: 10721
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 8 times
Contact:

Re: Problem with SQL Native

Post by nageswaragunupudi »

Jimmy wrote:hi,

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)

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
User avatar
nageswaragunupudi
Posts: 10721
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 8 times
Contact:

Re: Problem with SQL Native

Post by nageswaragunupudi »

Jimmy wrote:hi,

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)

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
User avatar
Rick Lipkin
Posts: 2668
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: Problem with SQL Native

Post by Rick Lipkin »

Jimmy

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
User avatar
nageswaragunupudi
Posts: 10721
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 8 times
Contact:

Re: Problem with SQL Native

Post by nageswaragunupudi »

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: 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

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
User avatar
ORibeiro
Posts: 187
Joined: Mon Oct 20, 2008 6:33 pm
Location: Itu, Sao Paulo, Brazil

Re: Problem with SQL Native

Post by ORibeiro »

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
Post Reply