473,626 Members | 3,369 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,10 0),
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+a ccess, then this returns the correct answer.

If I try to use Convert(varchar (20),dateadd(ss ,record_date,
access_time),10 0) 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 4983
On 7 Mar 2005 11:09:35 -0800, wi*********@yah oo.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,10 0),
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),1 00) 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*********@ya hoo.com) writes:
If I do it like this: Convert(varchar 20),record_date +access_time,10 0),
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+a ccess, then this returns the correct answer.


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

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.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
1898
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: ("data", "moredata", "evenmoredata", "date string") The date string is my concern. This is the date stamp from an email. The problem is that I have a whole bunch of variations when it comes to the format that the date string is in. For example...
5
2135
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 newsgroup and others, I'm not the only one. Its just a poorly addressed issue within ASP. So for all your poor buggers out there that are having problems, particularly with european date formats, here is my solution. I have the user enter their...
11
2844
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 loop even though my condition isn't met. Will work for a few loops then steps out often. Are there javascript "date" issues? I have also noticed different results between Firefox and Internet Explorer. thanks for any comments,
5
11368
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 ValidateForm(me){ var dt=me if (isDate(dt.value)==false){ dt.focus()
3
3110
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 working week definition. Lets say I have a working week definition of Monday through Friday, 9 am to 5 pm. Date1 = January 1st, 2005 at 8 am
12
2434
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 dd/mm/yyyy, I have found that the dates I put in my holidays table are reversed into American dates. So, the wrong holiday dates are subtracted from the total workdays between the start and end dates. Is there an easy fix for this? ******Code...
10
5802
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 date format in the table design, such as: mm/dd/yyyy? What I've done for years is to store the date format in date fields, then on the forms, based on their region, I would set the date formats on form_load
11
2642
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
2789
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 the project is projected to enter each stage of the process) and another set for actual dates. For my purposes, I'm only working with the projected dates. There is a formula, based on the size of the project (how many hours it will take), for...
0
8265
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8705
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8504
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5574
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2625
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1808
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.