Is there a way to modify the timestamp alone for an existing datetime attribute, for example changedate currently holds the value 2002-10-18 00:00:00.0 and would like to change it to 2002-10-18 08:00:00.0 The date would remain the same and time would only change.
If you know the time portion of the timestamp is 00:00:00... and you want it to be a a set difference away, then you can simply add the appropriate number of hours (and/or minutes and or seconds)
-- just updating the hour
update myTable
set myTS = myTs + 8 hours;
-- updating hour, minutes, seconds
update mytable
set myTS = myTs + 8 hours + 35 minutes + 22 seconds;
If it's possible that the time portion has something besides 00:00:00, then you can make use the scalar timestamp() function which returns a timestamp from individual date and time parameters.
update mytable
set myTS = timestamp(date(myTS), time('08:00:00'));
You can use a regular UPDATE statement, then set the column value to the new timestamp value. To do so, you could utilize the date / time / timestamp-related scalar functions.
Not tested, but something like:
new value = DATE(timestamp_column)+ 8 HOURS
It would extract the date portion and add the 8 hours. Else, use TIMESTAMP_FORMAT to compose the new value by extracting what you need and pass it in.
Related
I'm trying to get some (application) performance data from a PostgreSQL database by looking at two dates in a record, subtracting them and getting the result as a number of seconds with fractions.
It seems I'm able to get a timestamp which includes hours, minutes, seconds, and milliseconds, or I can get just the seconds (and fractions) without e.g. the minutes or I can get a UNIX timestamp which is only seconds (without the milliseconds). Is there a convenient way to convert a timestamp to seconds+millis?
Example:
SELECT extract(SECOND FROM TIME '00:01:02.1234') as secs;
secs
--------
2.1234
I was hoping to get 62.1234 (that's 1 * 60 + 02.1234) as the return value for overall seconds, not just the "seconds component" from the time value.
Is there an existing function to do that, or do I have to EXTRACT each part, multiply, and add each component together?
Use EPOCH:
SELECT extract(EPOCH FROM TIME '00:01:02.1234') as secs; 62.1234
I have this doubt on how to compare DB2 date, time fields with RPG date and time fields
If (Zpschdt < CurDat or (ZPschdt = Curdat and
(%Time() - ZPschtm) > 30));
For example in the above piece of code, Curdat is a character field which contains date value populated using below line
CurDat = %CHAR(%DATE():*MDY);
%Time() that is the current time needs to be subtracted from ZPschtm which is a DB2 time field and needs to be checked if the difference is greater that 30 minutes. How can this be achieved?
You can't compare a character variable to a date variable.
Generally, you have to either convert the character to date, or the date to character.
Since you character variable is formatted *MDY, it'd be easier to convert it to date; otherwise you'd need to change the format to YYYYMMDD in order to compare it as a numeric or character.
If Zpschdt < %Date(CurDat:*MDY)...
If you want to find schedule dates and times that are more than 30 minutes old, and the timeframe needs to cross days, you are going to have to use timestamps rather than dates and times. To deal with your situation, define a timestamp field, assign the schedule date and time, add 30 minutes to the timestamp, and compare that to the current timestamp. Like this:
dcl-s ts Timestamp;
ts = zschdt + zschtm + %minutes(30);
if ts < %timestamp();
// do something here
endif;
Think I have found a good enough solution for this as below:
If ( %Diff(%Date():Zpschdt:*Days) > 1 or
(%Diff(%Date():Zpschdt:*Days) = 0 and
%Diff(%Time():ZPschtm:*Minutes) > 30));
The first condition of Or - %Diff(%Date():Zpschdt:*Days) > 1 checks if Current date is ahead of the change date at least by one day.
The Second condition :
(%Diff(%Date():Zpschdt:*Days) = 0 and
%Diff(%Time():ZPschtm:*Minutes) > 30))
checks if the Change date is same as current day and if Current time is ahead of change time by at least 30 minutes..
Does this look good enough? Seems to do the job..
I have a date field, which is just text, and a field showing seconds since midnight.
CREATE TABLE t ("s" text, "d" text, "ts" timestamp)
;
INSERT INTO t ("s", "d")
VALUES
(24855, 20130604),
(24937.7, 20130604)
;
How can I convert these fields into a timestamp, so I can run time-based queries?
See a demo with SQL Fiddle:
http://sqlfiddle.com/#!15/67018/2
Since your data includes fractional seconds, you need to adjust for those:
UPDATE t
SET ts = to_timestamp(d||s, 'YYYYMMDDSSSS.MS');
Assuming milliseconds as the maximum precision. Else employ US for microseconds.
Consider the fine print in the manual:
In a conversion from string to timestamp, millisecond (MS) or
microsecond (US) values are used as the seconds digits after the
decimal point. For example to_timestamp('12:3', 'SS:MS') is not 3
milliseconds, but 300, because the conversion counts it as 12 + 0.3
seconds. This means for the format SS:MS, the input values 12:3,
12:30, and 12:300 specify the same number of milliseconds. To get
three milliseconds, one must use 12:003, which the conversion counts
as 12 + 0.003 = 12.003 seconds.
Also note that to_timestamp() returns timestamp with time zone, which is coerced to timestamp in the context of the update, which turns out all right. For other contexts, you may want to cast explicitly or use this alternative, yielding timestamp:
SELECT d::date + interval '1s' * s::numeric AS ts FROM t;
The plain cast to date (d::date) works because your dates are in standard ISO 8601 format - or so I assume.
Concatenate the two fields together and then use to_timestamp()
UPDATE t SET ts = to_timestamp(d||s, 'YYYYMMDDSSSS');
For a discussion of the formatting string used in to_timestamp, see Table 9-21: Template Patterns for Date/Time Formatting in the PostgreSQL documentation.
Is there a way to update only Hour part in a DateTime field?? If not, how do I update the time part in Oracle? I tried this->
update tab_name
set C_Name=to_date('04/03/2012 00:31:00','MM/DD/YYYY HH:MI:SS AM')
where C_Name1=10484;
didn't work as I'm updating '00' in Hour part.
If I knew I wanted to update just one part of the time I'd probably convert to a string with the value I want in the appropriate place, then convert back to a date. Say I wanted the minutes to be "31":
update tab_name
set C_Name=
to_date(
to_char(C_Name, 'MM/DD/YYYY HH24:"31":SS'),
'MM/DD/YYYY HH24:MI:SS'
)
where C_Name1=10484;
If you want to modify the time portion relative to its current value (to add 2 hours or subtract 3 seconds, for example) then there are some choices for date arithemtic. The Oracle documentation is very good for these things.
I need to go through every row in a table and set every date in a particular column to the date before its current value (minus 14 hours, previous day, etc).
I could write a script to do this but I was wondering if there was a better SQL method?
Thanks!
UPDATE yourtable SET thefield = thefield - interval '14 hour';
relevant docs here, which should have been your first place to check.