473,395 Members | 1,941 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,395 software developers and data experts.

various working with dates issues

I've got a table with some datetime fields in it.

One field (call it field 1) is of the form mm/dd/yyyy and the other two
(fields 2 and 3) are in the form of hh:mm:ss:xx where xx is hundreths
of a second.

I'm getting the difference between field 2 and 3 using (datediff(ms,
access_time, release_time )/1000/60.). This seems to work fine.

However, in some other cases I'd like to add field 1 to field 2 and
then manipulate the result. This is where it gets weird.

If I do it like this: Convert(varchar 20),record_date+access_time,100),
it adds field 2 ok but subtracts two days. So for example 3/1/05 +
10:30:00 AM = 2/27/05 10:30:00 AM. So it effectively subtracts two
additional days for no apparent reason. If is use
record_date+2+access, then this returns the correct answer.

If I try to use Convert(varchar(20),dateadd(ss,record_date,
access_time),100) sql server complains Argument data type datetime is
invalid for argument 2 of the dateadd function.

Basically I'd just like to know how to add and subtract fields 1
(mm/dd/yyyy format) and 2 (hh:mm:ss:xx format).

As a bonus question, is it possible to get an average time for several
different times? For example the average time between 10:30 and 11:00
would be 10:45.

regards,
-David

Jul 23 '05 #1
2 4972
On 7 Mar 2005 11:09:35 -0800, wi*********@yahoo.com wrote:

Hi David,

Since you have various datetime related questions, I'll first provide
the URL to Tibor Karaszi's Ultimate guide to datetime. According to my
notes, the URL is http://www.karaszi.com/SQLServer/info_datetime.asp. I
can't open the page right now, so it appears that Tibor's server is
down; let's hope it gets back up and running soon!
I've got a table with some datetime fields in it.

One field (call it field 1) is of the form mm/dd/yyyy and the other two
(fields 2 and 3) are in the form of hh:mm:ss:xx where xx is hundreths
of a second.
This is not correct. If you define columns as datetime, they are stored
in an internal format (consisting of two integers). What you see on your
screen is the result of editing by your client application. Based on
your description, I think you are using Enterprise Manager to check your
data; my advise is not to do that. Use Query Analyzer instead - this
gives the most vanilla view of the data. QA uses one standard format for
all datetime values and doesn't attempt to think for you.

EM does attempt to think for you. This is what cuases your confusion.
You see, each datetime value has both a date and a time portion. If the
time portion is not set when the value is supplied, SQL Server defaults
to midnight. EM will suppress the display of the time part when it is
equal to midnight, thinking that you probably are interested in the date
only. EM could be correct of course, but it could be wrong as well.

A similar thing happens if you supply only the time portion for a
datetime value: SQL Server will default the date to the base date for
datetime calculations: January 1st 1900 (or, in standard YYYYMMDD
notation, 19000101). And EM will attempt to suppress this portion,
showing the time only. Unfortunately, this part of EM is not only trying
to think for you, it is also bugged. You see, EM thinks the base date is
not 19000101, but 18991229. So if a datetime column holds 18991229
12:45, is will show in EM as just 12:45; if a datetime column holds
19000101 12:45 (what SQL Server will use if you supply '12:45' as
input), EM will show both date and time!

I'm getting the difference between field 2 and 3 using (datediff(ms,
access_time, release_time )/1000/60.). This seems to work fine.

However, in some other cases I'd like to add field 1 to field 2 and
then manipulate the result. This is where it gets weird.

If I do it like this: Convert(varchar 20),record_date+access_time,100),
it adds field 2 ok but subtracts two days. So for example 3/1/05 +
10:30:00 AM = 2/27/05 10:30:00 AM. So it effectively subtracts two
additional days for no apparent reason. If is use
record_date+2+access, then this returns the correct answer.
Obviously, you're not only using EM for display, but for data entry as
well. So you get doubly bitten by EM's datetime bug. If you manually
enter only a time in EM, EM will happily add the date that it thinks is
the base date (18991229) before sending it to SQL Server. SQL Server
gets a complete datetime and has no need to add the base date. The
datetime value gets stored at two days before SQL Server's base date.

You'd see this immediately if you use Query Analyzer to query the table,
but EM will hind the incorrect data from view. Until you start using it
in calculations, of course - then you suddenly see a two day difference
in your results!!

You can use various ways to get the correct result. One kludge is to
siimply add two days to the result; you've already found that. Another
kludge is to use COVNERT with styule parameters to convert both values
to character strings, one in the format yyyy-mm-dd, one in the format
hh:mm:ss (or hh:mm:ss.ttt), then concatenate them, together with a
capital T to get the ISO standard yyyy-mm-ddThh:mm:ss.ttt or
yyyy-mm-ddThh:mm:ss format and convert that string back to datetime. But
the only good way to fix this is to make sure that the data in your
table is correct: if you really need the time portion only, make sure
that the date portion is set to the real base date: 19000101.

If I try to use Convert(varchar(20),dateadd(ss,record_date,
access_time),100) sql server complains Argument data type datetime is
invalid for argument 2 of the dateadd function.
Yes, to do this, you would first have to convert access_time to a number
of seconds. But unless you exclude the date part in that conversion,
you'd still be two days off <g>.

Basically I'd just like to know how to add and subtract fields 1
(mm/dd/yyyy format) and 2 (hh:mm:ss:xx format).
Though not documented, using record_date + access_time does work. Just
keep in mind that both date and time portion get added (where adding
dates is in fact adding the number of dates since 19000101).

As a bonus question, is it possible to get an average time for several
different times? For example the average time between 10:30 and 11:00
would be 10:45.


declare @start datetime, @end datetime
set @start = '10:30'
set @end = '11:00'
select dateadd(second, datediff(second, @start, @end) / 2, @start)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #2
(wi*********@yahoo.com) writes:
If I do it like this: Convert(varchar 20),record_date+access_time,100),
it adds field 2 ok but subtracts two days. So for example 3/1/05 +
10:30:00 AM = 2/27/05 10:30:00 AM. So it effectively subtracts two
additional days for no apparent reason. If is use
record_date+2+access, then this returns the correct answer.


SELECT convert(datetime, convert(char(8), field1, 112) + ' ' +
convert(char(12), field2, 118))

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #3

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

Similar topics

11
by: Amy G | last post by:
I have seen something about this beofore on this forum, but my google search didn't come up with the answer I am looking for. I have a list of tuples. Each tuple is in the following format: ...
5
by: PW | last post by:
<rant> Sorry guys, but I just have to whinge. Dates in ASP are a total pain in the butt! I seem to get caught out so many times. I realise its my own fault, but going from the posts in this...
11
by: Peter Pfeiffer | last post by:
I've written several scripts that have "while" blocks which increment a date by one day if the date does not match one of a group of dates. However, sometimes it apparently steps out out the while...
5
by: tshad | last post by:
I have a date validation function that I want to stay at the object I am validating if there is a Validation error, but it always goes to the next object. The Javascript: function...
3
by: | last post by:
Hello, I am hoping someone else has thought about a date time calculation i need to perform. I would like to be able to calculate the number of "working minutes" between 2 dates, given my...
12
by: Dixie | last post by:
I am trying to calculate the number of workdays between two dates with regards to holidays as well. I have used Arvin Meyer's code on the Access Web, but as I am in Australia and my date format is...
10
by: ARC | last post by:
Hello all, General question for back-end database that has numerous date fields where the database will be used in regions that put the month first, and regions that do not. Should I save a...
11
by: lenygold via DBMonster.com | last post by:
Hi everybody! This query is supposed to count consecutive years from the current year without OLAP. Input Table: ID DateCol 1 02/01/2006 1 01/01/2006 1 01/01/2005
2
by: angi35 | last post by:
Hi, I'm working in Access 2000. I have a form with a series of date fields, showing the progress of a project from start to completion. There's a set of fields/controls for projected dates (when...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.