473,325 Members | 2,712 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,325 software developers and data experts.

Timestamp problem!

Hello Group,

I am having a really weird problem... Can anyone tell the difference
between Query 1 and Query 2 below? Why Query 2 excludes '2/28/2005'?
Many thanks!

create table a
(ApplicationID int
,timestamp datetime)

insert into a values(1111,'2/26/05 3:24')
insert into a values(2222,'2/25/05 19:03')
insert into a values(1111,'2/4/05 14:54')
insert into a values(2222,'2/28/05 23:10')

--Query 1--
select year(timestamp) app_year
,month(timestamp) app_month
,day(timestamp) app_day
,count(distinct applicationid) numapps
from a
where year(timestamp)=2005 and month(timestamp)=2
group by year(timestamp)
,month(timestamp)
,day(timestamp)
order by year(timestamp)
,month(timestamp)
,day(timestamp)

--Query 2--
select year(timestamp) app_year
,month(timestamp) app_month
,day(timestamp) app_day
,count(distinct applicationid) numapps
from a
where timestamp between '2/1/2005' and '2/28/2005'
group by year(timestamp)
,month(timestamp)
,day(timestamp)
order by year(timestamp)
,month(timestamp)
,day(timestamp)

Jul 23 '05 #1
2 5159
'2/28/2005' represents midnight on that date whereas the DATETIME you
have in the database is after midnight therefore the BETWEEN predicate
will exclude it.

It's generally more convenient to use >= and < rather than BETWEEN:

....
WHERE x >= '20050201'
AND x < '20050301'

For date literals without times, use the ISO YYYMMDD format. Regional
formats like the one you used will fail under some server and
connection settings.

Don't use TIMESTAMP as a column name. It's a reserved word and anyway
is not a very meaningful name for a column.

--
David Portas
SQL Server MVP
--

Jul 23 '05 #2
Thanks David, that's a big help!
David Portas wrote:
'2/28/2005' represents midnight on that date whereas the DATETIME you
have in the database is after midnight therefore the BETWEEN predicate will exclude it.

It's generally more convenient to use >= and < rather than BETWEEN:

...
WHERE x >= '20050201'
AND x < '20050301'

For date literals without times, use the ISO YYYMMDD format. Regional
formats like the one you used will fail under some server and
connection settings.

Don't use TIMESTAMP as a column name. It's a reserved word and anyway
is not a very meaningful name for a column.

--
David Portas
SQL Server MVP
--


Jul 23 '05 #3

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

Similar topics

2
by: Ben | last post by:
I would like to use php to query a database and retrieve a unix timestamp. The problem is that mysql is storing the data in the date format and not a timestamp. I am sure that I can amend my...
2
by: Astra | last post by:
Hi All I know an SQL Server timestamp seems to be as useful as rocking horse for show jumping, but I'm hoping you know a 'fudge' to get me round a problem or at least confirm that it isn't...
2
by: Alex | last post by:
Hi all, Got a small problem here. I have a number of web caches here that generate loads and loads of log files and instead of keeping them for analysis I want to write them to a db/2...
6
by: Robert Schuldenfrei | last post by:
Dear NG, As expected, when I went to implement TIMESTAMP, I failed. With the help of Kevin Yu, I got the 2 code segments at the bottom working using a WHERE clause that checks all columns. ...
1
by: Roger Twomey | last post by:
I have a database that I don't want to lock. I decided that before any updates can occur I would check a timestamp value and ensure that nobody else updated before I did (avoiding the 'last update...
7
by: mybappy | last post by:
Hi: I am trying to use timestamp field of SQL Server to maintain concurrency. My problem is how do I store the timestamp value in my webform. The hidden field does not work as I get some cast...
0
by: presley2 | last post by:
Hi, I want to create an auto creation date and update date in my mysql table. I am using the "MySQL Cookbook" approach, using the TIMESTAMP field which basically is: - create 2 new fields in the...
5
by: pankaj_wolfhunter | last post by:
Greetings, In Sybase, a timestamp column is automatically loaded by the DB engine and its in some hex format. This is shown below id timestamp_value ----------- ------------------ 1...
22
by: Mal Ball | last post by:
I hope I have the right forum for this question. I have an existing Windows application which uses a SQL Server database and stored procedures. I am now developing a web application to use the same...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.