473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

a sql query that doesn't return any record (performing date comparison)

hello,

this is antenio. recently i've come to a problem. i got a way through
it, somehow, still it left me in a curious state, so i'm posting it
here, if i can get an answer from some techy,

here is my table structure,

Name: Table1

Name Type Size
~~~~ ~~~ ~~~
szUserID Text 50
szOrgID Text 50
szAccountID Text 50
dtLoginDateTime Date/Time 8
dtLogoutDateTime Date/Time 8
numUnitsUsed Long Integer 4
szSessionID Text 50
sample data

szUserID szOrgID szAccountID dtLoginDateTime dtLogoutDateTime numUnitsUsed szSessionID
sa1111 bioe0001 2006-06-16 14:58:25 2006-06-16
15:00:57 0 g3ew0q55qg1g0x3vayivkx45
sa1111 bioe0001 2006-06-16 15:01:12 2006-06-16
15:04:38 0 g3ew0q55qg1g0x3vayivkx45
sa1111 bioe0001 2006-06-16 15:04:43 2006-06-16
15:06:59 0 g3ew0q55qg1g0x3vayivkx45
sa1111 bioe0001 2006-06-16 15:40:20 2006-06-16
15:41:04 0 g3ew0q55qg1g0x3vayivkx45
sa1111 BGB_Acc001 2006-06-16 16:35:51 2006-06-16
16:35:58 0 n3kpoivtcmfr2xigzyutlazk
sa1111 BGB_Acc001 2006-06-16 17:21:47 2006-06-16
17:26:23 0 u1qk3syh1fwbbk55v41qnayo

Note: I STORE DATETIME IN ISO Format (yyyy-mm-dd hh:nn:ss)

(sorry for disOrganized format, i donno how to paste in bettter way...)

HERE COMES the QUERY.This FAILS to return any record !!!!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SELECT dtLoginDateTime, szUserID, szOrgID, szAccountID
FROM Table1
WHERE dtLoginDateTime=#2006-06-16 14:58:25#
i got results when i tried it this way,.
~~~~~~~~~~~~~~~~~~~~~~~~~~
SELECT dtLoginDateTime, szUserID, szOrgID, szAccountID
FROM Table1
WHERE cdate(format(dtLoginDateTime,'General Date'))=#2006-06-16
14:58:25#
So what's Wrong and where,

thanks in advance.
Best regards,
Antenio.

Jun 26 '06 #1
7 2745
mr*****@gmail.com wrote:
here is my table structure,

Name: Table1

Name Type Size
~~~~ ~~~ ~~~
szUserID Text 50
szOrgID Text 50
szAccountID Text 50
dtLoginDateTime Date/Time 8
dtLogoutDateTime Date/Time 8
numUnitsUsed Long Integer 4
szSessionID Text 50
sample data

szUserID szOrgID szAccountID dtLoginDateTime dtLogoutDateTime numUnitsUsed szSessionID
sa1111 bioe0001 2006-06-16 14:58:25 2006-06-16
15:00:57 0 g3ew0q55qg1g0x3vayivkx45
sa1111 bioe0001 2006-06-16 15:01:12 2006-06-16
15:04:38 0 g3ew0q55qg1g0x3vayivkx45
sa1111 bioe0001 2006-06-16 15:04:43 2006-06-16
15:06:59 0 g3ew0q55qg1g0x3vayivkx45
sa1111 bioe0001 2006-06-16 15:40:20 2006-06-16
15:41:04 0 g3ew0q55qg1g0x3vayivkx45
sa1111 BGB_Acc001 2006-06-16 16:35:51 2006-06-16
16:35:58 0 n3kpoivtcmfr2xigzyutlazk
sa1111 BGB_Acc001 2006-06-16 17:21:47 2006-06-16
17:26:23 0 u1qk3syh1fwbbk55v41qnayo

Note: I STORE DATETIME IN ISO Format (yyyy-mm-dd hh:nn:ss)

(sorry for disOrganized format, i donno how to paste in bettter way...)

HERE COMES the QUERY.This FAILS to return any record !!!!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SELECT dtLoginDateTime, szUserID, szOrgID, szAccountID
FROM Table1
WHERE dtLoginDateTime=#2006-06-16 14:58:25#

i got results when i tried it this way,.
~~~~~~~~~~~~~~~~~~~~~~~~~~
SELECT dtLoginDateTime, szUserID, szOrgID, szAccountID
FROM Table1
WHERE cdate(format(dtLoginDateTime,'General Date'))=#2006-06-16
14:58:25#

So what's Wrong and where,


Congratulations on using ISO format. Would that everyone did.

This works for me:

Sub temp()
Dim r As DAO.Recordset
DBEngine(0)(0).Execute "INSERT INTO Table1 ([text],testdate) VALUES
('d',#2006-06-16 14:58:25#)"
Set r = DBEngine(0)(0).OpenRecordset("SELECT * FROM Table1 WHERE
testdate=#2006-06-16 14:58:25#")
With r
.MoveLast
Debug.Print .RecordCount
End With
Set r = Nothing
End Sub

Sub test()
Dim z As Long
For z = 0 To 10
temp
Next z
End Sub

displaying in the Immediate Window

1
2
3
4
5
6
7
8
9
10
11

As you can see, WHERE testdate=#2006-06-16 14:58:25# seems to "work".

Is this a fair test of what you are saying does not work?
Is it substantively different from what you are doing?
Considering that JET stores datetime data as 8 bytes that are roughly
analagous to a double, what do you mean when you say, "I STORE DATETIME
IN ISO Format (yyyy-mm-dd hh:nn:ss)?"

My Windows local settings for date time are set to the ISO format. I
doubt if this makes a difference to JET's interpretation of #2006-06-16
14:58:25# but perhaps we should consider it.

Jun 26 '06 #2
Internally, Access stores dates as floating point numbers, where the integer
part represents the date, and the fraction represents the time of day (.5 =
noon; .25 = 6am (one quarter of a day), and so on.) That means date/time
values are subject to the same rounding issues that plague floating point
values.

The reason your workaround succeeds is that it forces the date value to be
the value to the nearest second that you are trying to match.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

<mr*****@gmail.com> wrote in message
news:11**********************@b68g2000cwa.googlegr oups.com...

this is antenio. recently i've come to a problem. i got a way through
it, somehow, still it left me in a curious state, so i'm posting it
here, if i can get an answer from some techy,

here is my table structure,

Name: Table1

Name Type Size
~~~~ ~~~ ~~~
szUserID Text 50
szOrgID Text 50
szAccountID Text 50
dtLoginDateTime Date/Time 8
dtLogoutDateTime Date/Time 8
numUnitsUsed Long Integer 4
szSessionID Text 50
sample data

szUserID szOrgID szAccountID dtLoginDateTime dtLogoutDateTime numUnitsUsed
szSessionID
sa1111 bioe0001 2006-06-16 14:58:25 2006-06-16
15:00:57 0 g3ew0q55qg1g0x3vayivkx45
sa1111 bioe0001 2006-06-16 15:01:12 2006-06-16
15:04:38 0 g3ew0q55qg1g0x3vayivkx45
sa1111 bioe0001 2006-06-16 15:04:43 2006-06-16
15:06:59 0 g3ew0q55qg1g0x3vayivkx45
sa1111 bioe0001 2006-06-16 15:40:20 2006-06-16
15:41:04 0 g3ew0q55qg1g0x3vayivkx45
sa1111 BGB_Acc001 2006-06-16 16:35:51 2006-06-16
16:35:58 0 n3kpoivtcmfr2xigzyutlazk
sa1111 BGB_Acc001 2006-06-16 17:21:47 2006-06-16
17:26:23 0 u1qk3syh1fwbbk55v41qnayo

Note: I STORE DATETIME IN ISO Format (yyyy-mm-dd hh:nn:ss)

(sorry for disOrganized format, i donno how to paste in bettter way...)

HERE COMES the QUERY.This FAILS to return any record !!!!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SELECT dtLoginDateTime, szUserID, szOrgID, szAccountID
FROM Table1
WHERE dtLoginDateTime=#2006-06-16 14:58:25#

i got results when i tried it this way,.
~~~~~~~~~~~~~~~~~~~~~~~~~~
SELECT dtLoginDateTime, szUserID, szOrgID, szAccountID
FROM Table1
WHERE cdate(format(dtLoginDateTime,'General Date'))=#2006-06-16
14:58:25#

So what's Wrong and where,

Jun 26 '06 #3

Lyle Fairfield wrote:
mr*****@gmail.com wrote:
here is my table structure,

Name: Table1

Name Type Size
~~~~ ~~~ ~~~
szUserID Text 50
szOrgID Text 50
szAccountID Text 50
dtLoginDateTime Date/Time 8
dtLogoutDateTime Date/Time 8
numUnitsUsed Long Integer 4
szSessionID Text 50
sample data

szUserID szOrgID szAccountID dtLoginDateTime dtLogoutDateTime numUnitsUsed szSessionID
sa1111 bioe0001 2006-06-16 14:58:25 2006-06-16
15:00:57 0 g3ew0q55qg1g0x3vayivkx45
sa1111 bioe0001 2006-06-16 15:01:12 2006-06-16
15:04:38 0 g3ew0q55qg1g0x3vayivkx45
sa1111 bioe0001 2006-06-16 15:04:43 2006-06-16
15:06:59 0 g3ew0q55qg1g0x3vayivkx45
sa1111 bioe0001 2006-06-16 15:40:20 2006-06-16
15:41:04 0 g3ew0q55qg1g0x3vayivkx45
sa1111 BGB_Acc001 2006-06-16 16:35:51 2006-06-16
16:35:58 0 n3kpoivtcmfr2xigzyutlazk
sa1111 BGB_Acc001 2006-06-16 17:21:47 2006-06-16
17:26:23 0 u1qk3syh1fwbbk55v41qnayo

Note: I STORE DATETIME IN ISO Format (yyyy-mm-dd hh:nn:ss)

(sorry for disOrganized format, i donno how to paste in bettter way...)

HERE COMES the QUERY.This FAILS to return any record !!!!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SELECT dtLoginDateTime, szUserID, szOrgID, szAccountID
FROM Table1
WHERE dtLoginDateTime=#2006-06-16 14:58:25#

i got results when i tried it this way,.
~~~~~~~~~~~~~~~~~~~~~~~~~~
SELECT dtLoginDateTime, szUserID, szOrgID, szAccountID
FROM Table1
WHERE cdate(format(dtLoginDateTime,'General Date'))=#2006-06-16
14:58:25#

So what's Wrong and where,


Congratulations on using ISO format. Would that everyone did.

This works for me:

Sub temp()
Dim r As DAO.Recordset
DBEngine(0)(0).Execute "INSERT INTO Table1 ([text],testdate) VALUES
('d',#2006-06-16 14:58:25#)"
Set r = DBEngine(0)(0).OpenRecordset("SELECT * FROM Table1 WHERE
testdate=#2006-06-16 14:58:25#")
With r
.MoveLast
Debug.Print .RecordCount
End With
Set r = Nothing
End Sub

Sub test()
Dim z As Long
For z = 0 To 10
temp
Next z
End Sub

displaying in the Immediate Window

1
2
3
4
5
6
7
8
9
10
11

As you can see, WHERE testdate=#2006-06-16 14:58:25# seems to "work".

Is this a fair test of what you are saying does not work?
Is it substantively different from what you are doing?
Considering that JET stores datetime data as 8 bytes that are roughly
analagous to a double, what do you mean when you say, "I STORE DATETIME
IN ISO Format (yyyy-mm-dd hh:nn:ss)?"

My Windows local settings for date time are set to the ISO format. I
doubt if this makes a difference to JET's interpretation of #2006-06-16
14:58:25# but perhaps we should consider it.


i forget to mension that,
My windows (XP SP2) Regional settings are English (US).
In database i've set format of datetime field as "yyyy-mm-dd hh:nn:ss",
And Whenever i store dates, i convert them in ISO Format. viceversa.
I'm Using ADO.NET (Oledb Provider)

thanks for your time,

best regards,
Antenio.

Jun 26 '06 #4
Allen Browne wrote:
Internally, Access stores dates as floating point numbers, where the integer
part represents the date, and the fraction represents the time of day (.5 =
noon; .25 = 6am (one quarter of a day), and so on.) That means date/time
values are subject to the same rounding issues that plague floating point
values.
Allen Browne wrote: Internally, Access stores dates as floating point numbers, where the integer
part represents the date, and the fraction represents the time of day (.5 =
noon; .25 = 6am (one quarter of a day), and so on.) That means date/time
values are subject to the same rounding issues that plague floating point
values.


The rounding issues that "plague!" [in my opinion this is like saying
that there are only two halves to every orange is a BIG problem]
floating point values occur when we perform arithmetic operations on
them, or when we try to compare them with some literal (string). Unless
we are so foolish as to try to perform arithmetic on date-times these
issues should never cause problems. The format in which dates are
stored can differentiate to a degree one and half million times greater
than that required to differentiate between seconds. Surely, the rules
for storage are simply the rules of recovery reversed! I very much
doubt that any rounding issue exists in the storage, retrieval or
comparison of dates which have been dealt with properly as dates and
not suffered from coercion into doubles. The OP's post gives no
indication that his/her dates have been handled carelessly.

The OP now indicates he is using ADO.Net. I guess I am not sufficiently
versed in ADO.Net to have considered using VBA functions within SQL (as
he does in his second SQL string) used in it, and I think I will keep
that position for a long time.

In any case, I believe that within Access/JET/DAO/ADO there is no
difficulty in recovering a record based on its meeting the criteria of
a date-time field being equal to a date-time expressed as #YYYY-MM-DD
hh:nn:ss#.

For anyone who is bored and wants to spend a little time reflecting on
date-times in VBA I offer:

Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(Destination As Any, _
Source As Any, _
ByVal Length As Long)

Public Sub ExploreDateStorage()
Dim b(8) As Byte
Dim d As Date
Dim z As Long

d = DateSerial(2006, 6, 16) + TimeSerial(14, 58, 25)

For z = 0 To 9
CopyMemory b(0), d, 8
Debug.Print _
Format(b(0), "000\."); _
Format(b(1), "000\."); _
Format(b(2), "000\."); _
Format(b(3), "000\."); _
Format(b(4), "000\."); _
Format(b(5), "000\."); _
Format(b(6), "000\."); _
Format(b(7), "000\ "); _
StrConv(b, vbUnicode)
d = DateAdd("s", 1, d)
Next z

d = DateSerial(2006, 6, 16) + TimeSerial(14, 58, 25)

For z = 0 To 9
CopyMemory b(0), d, 8
Debug.Print _
Format(b(0), "000\."); _
Format(b(1), "000\."); _
Format(b(2), "000\."); _
Format(b(3), "000\."); _
Format(b(4), "000\."); _
Format(b(5), "000\."); _
Format(b(6), "000\."); _
Format(b(7), "000\ "); _
StrConv(b, vbUnicode)
d = d + 1 / (2 ^ 37)
Next z

Debug.Print CDec(1 / (2 ^ 37))

Debug.Print (CDec(1 / 86400)) / (CDec(1 / (2 ^ 37)))

End Sub

Jun 26 '06 #5
mr.n...@gmail.com wrote:
I'm Using ADO.NET (Oledb Provider)


Perhaps, we could see the exact and complete line of code, or property
assignment where the SQL statement is used.

Jun 26 '06 #6

Lyle Fairfield wrote:
mr.n...@gmail.com wrote:
I'm Using ADO.NET (Oledb Provider)


Perhaps, we could see the exact and complete line of code, or property
assignment where the SQL statement is used.

hello,
here is the code that uses the query.

Private Sub BindData()

Dim cn As New OleDbConnection

Try
cn.ConnectionString = sConnectionString

Dim da As New OleDbDataAdapter(sSQLQuery, cn)
Dim ds As New DataSet

da.Fill(ds)

dbGrid.DataSource = ds

dbGrid.DataBind()

lblCount.Text = "Details found" +": " +
ds.Tables(0).Rows.Count.ToString
Catch ex As Exception
lblError.Text += "Error description" + ": " +
ex.Message() + "<p>"
Finally
cn.Close()
End Try

end sub

AND, here is extra stuff,

1) "ConnectionString"="Provider=Microsoft.Jet.OLEDB.4 .0;Data
Source="db\usrmgtVer1.mdb";User Id=admin;Password="/>

(taken from web.config file of my ASP.net Site, altered it from
original string, removed some text)

2) sSQLQuery = same sql that i mentioned in my post.

Jun 26 '06 #7
Nimz wrote:
Lyle Fairfield wrote:
mr.n...@gmail.com wrote:
I'm Using ADO.NET (Oledb Provider)


Perhaps, we could see the exact and complete line of code, or property
assignment where the SQL statement is used.

hello,
here is the code that uses the query.

Private Sub BindData()

Dim cn As New OleDbConnection

Try
cn.ConnectionString = sConnectionString

Dim da As New OleDbDataAdapter(sSQLQuery, cn)
Dim ds As New DataSet

da.Fill(ds)

dbGrid.DataSource = ds

dbGrid.DataBind()

lblCount.Text = "Details found" +": " +
ds.Tables(0).Rows.Count.ToString
Catch ex As Exception
lblError.Text += "Error description" + ": " +
ex.Message() + "<p>"
Finally
cn.Close()
End Try

end sub

AND, here is extra stuff,

1) "ConnectionString"="Provider=Microsoft.Jet.OLEDB.4 .0;Data
Source="db\usrmgtVer1.mdb";User Id=admin;Password="/>

(taken from web.config file of my ASP.net Site, altered it from
original string, removed some text)

2) sSQLQuery = same sql that i mentioned in my post.


I can't duplicate this problem. From what you have said, the
OleDbDataAdapter does not seem to have a problem with #YYYY-MM-DD
hh:nn:ss#. It seems to have a problem with your date-time field. I
cannot think of any reason that this should be so.
I suppose this situation underlines the advisability, in this case and
most or all others, of declaring properly typed variables, in this case
Parameters, and using them. VBA is an evil language in that it actively
abets non-typed coding; I suspect that God has a special place in Hell
for its creators, but the type of punishment will constantly shift from
fire to starvation to thirst to beatings to listening to religious
fundamentalists with neither rhyme nor reason.

Jun 26 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: DBNovice | last post by:
I have a database that keeps records on the issue and failure of an item. Currently, the database is poorly desisned; therefore I'm performing queries to break the data into normalized tables and...
6
by: paii | last post by:
I have a table that stores job milestone dates. The 2 milestones I am interested in are "Ship Date" TypeID 1 and "Revised Ship Date" TypeID 18. All jobs have TypeID 1 only some jobs have TypeID 18....
3
by: John Ortt | last post by:
> I have a table of dates in ascending order but with varying intervals. I > would like to create a query to pull out the date (in field 1) and then pull > the date from the subsequent record...
2
by: rivka.howley | last post by:
I wrote some code that creates a table with a date/time field at 15-minute intervals. Here's how I create and populate the table With tblDataTemp ..Fields.Append .CreateField("CT_ID", dbLong)...
3
by: Wired Hosting News | last post by:
Lets say I have 10 products in 10 different stores and every week I get a report from each store telling me how many items they have left for each of the 10 products. So each week I enter in 100...
5
by: Wired Hosting News | last post by:
I tried to be breif and give a scenario so as not to be overlooked because it was soooo long. Let me give you real world. I am a manufacturer of goods and produce 11 items that are distributed...
19
by: Lysander | last post by:
I have written a query that takes three integers representing day,month and year, forms a date from them and compares this date to the date the record was entered and returns any records where the...
0
by: sara | last post by:
Hi - I have a table keeping track of employees' jobs in a location. So, Store #1 Manager #353, AsstMgr #556 Store #2 Manager #776, AsstMgr #132 etc. The table is Date StoreNum MgrNum ...
0
by: sara | last post by:
Hi - I posted this earlier and didn't get any response. Hopefully someone can help?? I have a table keeping track of employees' jobs in a location. So, Store #1 Manager #353, AsstMgr #556...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.