473,395 Members | 1,474 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.

How to find a lower date

I have following problem:

table includes times for startup and end of operation as datetime field
related to daily shift operations:

dateid date starttime endtime

458 2006-12-29 22:00 23:15
458 2006-12-29 00:15 01:30
459 2006-12-30 20:00 21:10
459 2006-12-30 22:15 23:35
459 2006-12-30 23:30 00:40
459 2006-12-30 01:50 02:30

records are inserted for a date related to begining of the shift, although
some operations are performed also past the midnight (actualy next day, ex:
2006-12-31), but belongs to same shift (group)

Now I need to build a function that corrects (updates) the date of every
operation recorded after midnight to a date+1 value, so all records related
to same groups (458, 459, etc) that starts after midnight has correct date.

The procedure has to update already exiting table.

Any solution?
Grey
Jan 3 '07 #1
8 2861
I'm a bit confused. You mention that it uses a datetime data type,
but the data shows appears to be a column with a date and no time, and
two columns with time but no date. Datetime always has both.

Roy Harvey
Beacon Falls, CT

On Thu, 4 Jan 2007 00:26:39 +0100, "Grey" <nu**@null.nullwrote:
>I have following problem:

table includes times for startup and end of operation as datetime field
related to daily shift operations:

dateid date starttime endtime

458 2006-12-29 22:00 23:15
458 2006-12-29 00:15 01:30
459 2006-12-30 20:00 21:10
459 2006-12-30 22:15 23:35
459 2006-12-30 23:30 00:40
459 2006-12-30 01:50 02:30

records are inserted for a date related to begining of the shift, although
some operations are performed also past the midnight (actualy next day, ex:
2006-12-31), but belongs to same shift (group)

Now I need to build a function that corrects (updates) the date of every
operation recorded after midnight to a date+1 value, so all records related
to same groups (458, 459, etc) that starts after midnight has correct date.

The procedure has to update already exiting table.

Any solution?
Grey
Jan 4 '07 #2

"Grey" <nu**@null.nullwrote in message
news:en**********@news.dialog.net.pl...
>I have following problem:

table includes times for startup and end of operation as datetime field
related to daily shift operations:

dateid date starttime endtime

458 2006-12-29 22:00 23:15
458 2006-12-29 00:15 01:30
459 2006-12-30 20:00 21:10
459 2006-12-30 22:15 23:35
459 2006-12-30 23:30 00:40
459 2006-12-30 01:50 02:30

records are inserted for a date related to begining of the shift, although
some operations are performed also past the midnight (actualy next day,
ex: 2006-12-31), but belongs to same shift (group)

Now I need to build a function that corrects (updates) the date of every
operation recorded after midnight to a date+1 value, so all records
related to same groups (458, 459, etc) that starts after midnight has
correct date.

The procedure has to update already exiting table.
Assuming no operation will exceed 24 hours it might look something like
this:

SELECT dateid,
date as startdate,
starttime,
CASE
WHEN endtime starttime
THEN convert(datetime,date)
ELSE convert(datetime,date)+ 1
END AS EndDate,
endtime
FROM Table1
>
Any solution?
Grey


Jan 4 '07 #3
I'm a bit confused. You mention that it uses a datetime data type,
but the data shows appears to be a column with a date and no time, and
two columns with time but no date. Datetime always has both.

Roy Harvey
Beacon Falls, CT
The fields are presented for simplicity. they have both date and time, but
the front end adds only time, so the datepart of the filed is disregarded.
Any way it contains a wrong value for operations past midnight.

Grey
Jan 4 '07 #4
Assuming no operation will exceed 24 hours it might look something like
this:

SELECT dateid,
date as startdate,
starttime,
CASE
WHEN endtime starttime
THEN convert(datetime,date)
ELSE convert(datetime,date)+ 1
END AS EndDate,
endtime
FROM Table1
Thats too easy. Table contains values from serveral hundreds of days. each
day has an id dateid like 458, 459, etc.
I have to go trough all table finding times past midninght for given group
and change the date to correct one.

Grey

>

>>
Any solution?
Grey



Jan 4 '07 #5
OK, let me see if I understand this. Have I identified the row that
need fixing correctly? I chose the one row that ended before it
started.
>dateid date starttime endtime

458 2006-12-29 22:00 23:15
458 2006-12-29 00:15 01:30
459 2006-12-30 20:00 21:10
459 2006-12-30 22:15 23:35
459 2006-12-30 23:30 00:40 --Problem?
459 2006-12-30 01:50 02:30
If that is not correct, please mark which ones have the problem and
what rule was made it so.

Roy Harvey
Beacon Falls, CT

On Thu, 4 Jan 2007 14:47:28 +0100, "Grey" <nu**@null.nullwrote:
>
>I'm a bit confused. You mention that it uses a datetime data type,
but the data shows appears to be a column with a date and no time, and
two columns with time but no date. Datetime always has both.

Roy Harvey
Beacon Falls, CT

The fields are presented for simplicity. they have both date and time, but
the front end adds only time, so the datepart of the filed is disregarded.
Any way it contains a wrong value for operations past midnight.

Grey
Jan 4 '07 #6
>>dateid date starttime endtime

458 2006-12-29 22:00 23:15
458 2006-12-29 00:15 01:30 --Problem?
>>459 2006-12-30 20:00 21:10
459 2006-12-30 22:15 23:35
459 2006-12-30 23:30 00:40
459 2006-12-30 01:50 02:30 --Problem?
459 2006-12-30 03:30 04:30 --Problem?
The start time is relevant - marked records should have a date + 1

Grey
Jan 4 '07 #7
Something like this might do what you want.

First the simple way, with two seperate UPDATEs, one for each column:

UPDATE Operations
SET starttime = DATEADD(day,1,starttime)
WHERE datepart(hour,starttime) < 12

UPDATE Operations
SET endtime = DATEADD(day,1,endtime)
WHERE datepart(hour,endtime) < 12

Note that I chose hour 12 as an arbitrary point in the day such that
times before that are "after midnight" and times after that are
"before midnight". Choose whatever time you prefer.

Also note that this "fixes" (or destroys, you tell me) the second date
in the sample row:
>>>459 2006-12-30 23:30 00:40
This was not designated as a problem row, but I thought the second
date would be. Please clarify if this second date should not be
changed.

Anyway, the two-UPDATE approach is innefficient, as most of the
changes happen to both columns on the same row. So we combine them,
but it becomes rather more complicated.

UPDATE Operations
SET starttime = CASE WHEN datepart(hour,starttime) < 12
THEN DATEADD(day,1,starttime)
ELSE starttime
END,
endtime = CASE WHEN datepart(hour,endtime) < 12
THEN DATEADD(day,1,endtime)
ELSE starttime
END
WHERE datepart(hour,starttime) < 12
OR datepart(hour,endtime) < 12

Hope that helps.

Roy Harvey
Beacon Falls, CT

On Thu, 4 Jan 2007 16:17:13 +0100, "Grey" <nu**@null.nullwrote:
>
>>>dateid date starttime endtime

458 2006-12-29 22:00 23:15
458 2006-12-29 00:15 01:30 --Problem?
>>>459 2006-12-30 20:00 21:10
459 2006-12-30 22:15 23:35
459 2006-12-30 23:30 00:40
459 2006-12-30 01:50 02:30 --Problem?
459 2006-12-30 03:30 04:30 --Problem?

The start time is relevant - marked records should have a date + 1

Grey
Jan 4 '07 #8
OK, now I have solved the problem by an approach I found at some other post
presented by Joe Celko:

CREATE TABLE Foobar

(id INTEGER NOT NULL PRIMARY KEY,

name CHAR(10) NOT NULL);

INSERT INTO Foobar VALUES (12, 'Johnson');

INSERT INTO Foobar VALUES (57, 'Nelson');

INSERT INTO Foobar VALUES (11, 'Roberts');

INSERT INTO Foobar VALUES (68, 'Smith');

SELECT F1.id, F1.name,

(SELECT MAX (id)

FROM Foobar AS F2

WHERE F2.id < F1.id) AS prev_id,

(SELECT MIN(id)

FROM Foobar AS F3

WHERE F3.id F1.id) AS next_id

FROM Foobar AS F1;

These subquery expressions are the LUB (least upper
bound) and the GLB (greatest lower bound).

Thats finds the lower records. Than a simply comparation like this:

SELECT CASE WHEN f1.id< (select id from foobar WHERE id =
dbo.get_prev_id(F1.id)) THEN date+1 ELSE date END AS date_past_midnight from
foobar as f1

Function get_prev_id is like:

SELECT (SELECT MAX (id) FROM foobar AS F2 WHERE F2.id < F1.id) AS prev_id

FROM foobar AS F1

where F1.id = @id <--- (argument passed to function)

Thanks for all help

GREY
Jan 7 '07 #9

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

Similar topics

12
by: hokiegal99 | last post by:
The below code does what I need it to do, but I thought that using something like ext = os.path.splitext(fname) and then searching ext for '.mp3' would be a much more accurate approach to solving...
17
by: Justin Emlay | last post by:
I'm hopping someone can help me out on a payroll project I need to implement. To start we are dealing with payroll periods. So we are dealing with an exact 10 days (Monday - Friday, 2 weeks). ...
3
by: Vinay Jain | last post by:
select *, lower(name) from student; gives error: could not find block containing chunk 0x8483530 what does this error mean & what may be the cause? template1=# \d student Table...
18
by: dfetrow410 | last post by:
Anyone have some code that will do this? Dave
11
by: Johny | last post by:
Is there a good way how to use string.find function to find a substring if I need to you case insensitive substring? Thanks for reply LL
0
by: Sarah McLead | last post by:
If you want to catch more visitors for your ads, adultfriendfinder or backlinks, it will be useful when your website is filled with good content. If you copy text from a website to place it on yours,...
2
by: Olumide | last post by:
Hello, I've got this nice inner class that I'm holds a set of "FrontPoint" objects as shown below. Unfortunately, the find and insert methods trigger massive C2784 errors. Would someone please...
2
Thekid
by: Thekid | last post by:
I had made a post about making a loop using letters instead of numbers and dshimer gave me this solution: for i in range(65,70): for j in range(65,70): for k in range(65,70): ...
5
by: ziycon | last post by:
I have a string and want to find any character that is lower case, any ideas how to do this, is there a function that will decipher between upper and lower case?
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.