Problem with SQL Native

Problem with SQL Native

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

Re: Problem with SQL Native

Postby nageswaragunupudi » Wed Feb 02, 2022 3: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
User avatar
nageswaragunupudi
 
Posts: 10690
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Problem with SQL Native

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

Re: Problem with SQL Native

Postby nageswaragunupudi » Fri Feb 04, 2022 4: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
User avatar
nageswaragunupudi
 
Posts: 10690
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Problem with SQL Native

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

Re: Problem with SQL Native

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

Re: Problem with SQL Native

Postby nageswaragunupudi » Sat Feb 05, 2022 6:05 pm

Code: Select all  Expand view  RUN

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

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10690
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Problem with SQL Native

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

Re: Problem with SQL Native

Postby Jimmy » Sun Feb 06, 2022 11:26 am

hi,

did you use
Code: Select all  Expand view  RUN
ORDER BY mytimestamp

when "compare" TIMESTAMP :?:

as i know ORDER BY use UTC but you want "Local Time" so try
Code: Select all  Expand view  RUN
ORDER BY CAST(mytimestamp AS datetime)
greeting,
Jimmy
User avatar
Jimmy
 
Posts: 1733
Joined: Thu Sep 05, 2019 5:32 am
Location: Hamburg, Germany

Re: Problem with SQL Native

Postby nageswaragunupudi » Mon Feb 07, 2022 2: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.

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 view  RUN

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 view  RUN

#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 view  RUN

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: 10690
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Problem with SQL Native

Postby nageswaragunupudi » Mon Feb 07, 2022 2:09 pm

Jimmy wrote:hi,

did you use
Code: Select all  Expand view  RUN
ORDER BY mytimestamp

when "compare" TIMESTAMP :?:

as i know ORDER BY use UTC but you want "Local Time" so try
Code: Select all  Expand view  RUN
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: 10690
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Problem with SQL Native

Postby nageswaragunupudi » Mon Feb 07, 2022 2:09 pm

Jimmy wrote:hi,

did you use
Code: Select all  Expand view  RUN
ORDER BY mytimestamp

when "compare" TIMESTAMP :?:

as i know ORDER BY use UTC but you want "Local Time" so try
Code: Select all  Expand view  RUN
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: 10690
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Problem with SQL Native

Postby Rick Lipkin » Mon Feb 07, 2022 2: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: Select all  Expand view  RUN
"[InvoiceDate] >= #"+dtoc(lDate)+"# And [InvoiceDate] <= #"+dtoc(hDate)+"#"


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


You have to use ' ' around your date variables.

Rick Lipkin
User avatar
Rick Lipkin
 
Posts: 2666
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: Problem with SQL Native

Postby nageswaragunupudi » Mon Feb 07, 2022 2: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: Select all  Expand view  RUN
"[InvoiceDate] >= #"+dtoc(lDate)+"# And [InvoiceDate] <= #"+dtoc(hDate)+"#"


Otherwise
Code: Select all  Expand view  RUN
"[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
nageswaragunupudi
 
Posts: 10690
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Problem with SQL Native

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

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 86 guests