Add integer value from another column to date in postgresql [duplicate] - postgresql

This question already has answers here:
Postgres INTERVAL using value from table
(3 answers)
Closed last year.
I want to add the value of another colum (which is an an integer representing miliseconds) to a date value in an update query.
I tried what this answer suggests but I can't find a way to not hardcode the value in the query but instead read it from the other column.
The two relevant fields are:
ScheduledExecuteDate: DATE(6)
RecurringInterval: INTEGER representing the number of miliseconds to add to the date
My query looks something like this:
UPDATE "Tasks" SET
"ScheduledExecuteDate"="ScheduledExecuteDate" + interval ("RecurringInterval" / 1000) second
WHERE "id" = 62
I've also tried 'second'. In both cases it throws an syntax error "at or near second"
Also note that I have to wrap the column names in quotes since they are case sensitive.
I also tried
"scheduledExecuteDate"=DATE_ADD("scheduledExecuteDate", interval ("Tasks"."recurringInterval" / 1000) seconds)
like answered here

You can use make_interval() passing fractional seconds:
UPDATE "Tasks"
SET "ScheduledExecuteDate" = "ScheduledExecuteDate"
+ make_interval(secs => "RecurringInterval"::double precision / 1000) second
WHERE "id" = 62

Related

How to replace last 5 digits of afield with '99999' using update query?

I have one column. In that column all rows are having 10 digits i.e. 1234567890. By using PostgreSQL update query, I need to update last 5 digits to 99999. i.e. 1234599999
Can any one provide me update query for above requirement.
Integer divide your number by 100,000 (i.e. discard the remainder), multiply it by 100,000 then add 99,999:
UPDATE table SET field = FLOOR(field / 100000) * 100000 + 99999;
UPDATE table name SET column name = column name:: int / 10000 * 10000 + 9999 WHERE column name!=''''
Here column name having varchar data type I converted to int as per my requirement.

numeric range data type postgresql

I have a strange situation in the desing of my DB. I have the case that the type of value of a field can be a normal integer or a number between a range. I explain myself with a example:
the column age can be a number (18) or a range between (18-30). How I can represent this with postgresql?
Thx!
An integer range can represent both a single integer value and a range. The single value:
select int4range(18,18,'[]');
int4range
-----------
[18,19)
The ")" in the result above means exclusive.
The range:
select int4range(18,30,'[]');
int4range
-----------
[18,31)
There are a couple different ways to do this.
Store a VARCHAR
Store two values lower bound and upper bound
If there are only a select set of ranges you can create a lookup table for that set and store a foreign key to that lookup table.
You can make a bigger number, for example 18 x 1000 + 0 = 18000 for 18 and 18 x 1000 + 30 = 18030 for (18, 30).
When you retrieve it, you do first = round(number/1000) for the first number and second = number - first for the second number.
You can also store them as a point http://www.postgresql.org/docs/9.4/static/datatype-geometric.html#AEN6730.

Postgresql - VALUE between two columns

I have a long list of six digit numbers (e.g. 123456)
In my postgresql DB I have a table with two columns start_value and end_value. The table has rows with start and end values which are 9 digits in length and represent a range of numbers i.e. start_value might be 123450000 and end_value might be 123459999.
I need to match each of the six digit numbers with it's row in the DB table which falls in its range.
For many numbers in my list I can simply run the following
SELECT * FROM table WHERE start_value=(number + 000)
However, this does not cover numbers which fall inside a range, but do not match this pattern.
I have been trying statements such as:
SELECT * FROM table WHERE start_value > (number + 000) AND end_value < (number + 999)
But this doesn't work because some rows cover larger ranges than xxxxx0000 to xxxxx9999 and so the statement above may return 20 rows or none.
Any points would be most welcome!
EDIT: the Data Type of the columns are numeric(25)
Assuming number is numeric:
select *
from table
where number * 1000 between start_value and end_value
Ok, so if I'm understanding correctly, first you need to pad your search value to 9 digits. You can do that with this - 12345 * (10 ^ (9 - length(12345::text))).
length(12345::text) gets the number of digits you currently have, then it subtracts that from 9 and multiplies your search value by 10 to the power of the result. Then you just throw it in your search. The resulting query looks something like this -
SELECT * FROM table WHERE (12345 * (10 ^ (9 - length(12345::text)))) > start_value AND (12345 * (10 ^ (9 - length(12345::text)))) < end_value
You could also use the BETWEEN operator, but it is inclusive, which doesn't match the example query you have.
POSTGRESQL
Some time we stuck in data type casting problems and null value exceptions.
SELECT *
FROM TABLE
WHERE COALESCE(number::int8, 0::int8) * 1000 BETWEEN start_value::int8 AND end_value::int8
;
number::int8 type cast to integer
start_value::int8 type cast to integer
COALESCE(number::int8, 0::int8)
return number or zero if value is empty to avoid exceptions

TSQL Syntax, Replace Existing "Wrong Value" with previous "Correct Value"

I have an application that makes an entry every hour in a MS SQL database.
The last entry on the 12th FEB is a zero value and is showing in my weekly report.
What I want to do is take the value from the previous count and enter into the filed instead of the zero value.
Can someone offer some advice on how to this because it is beyond my TSQL skills?
SELECT * FROM [dbo].[CountDetails]
WHERE [updateTime] < '2013-02.13'
AND [updateTime] > '2013-02.12'
AND ( DATEPART(hh,[updateTime])= '22' OR DATEPART(hh,[updateTime])= '23' )
Note: The application is supposed to zero the count a Midnight but on the 12th FEB it happened early and I know why.
EDIT: There are 5 IP addresses in total and 6 counters in total because 192.168.168.11 has 2 counters. So 2111 to 2116 is an entire entry for all available counters at 22:58 and 2117 to 2122 is an entire entry for all available counters at 23:58. I need to replace the 23:58 values with the corresponding value from 22:58.
Guessing here, but an update that joins on the ipAddress, counterNumber, and the datetime excluding fractional seconds, separated by an hour (do the SELECT part first for safety):
UPDATE b
SET count = a.count
-- SELECT *
FROM dbo.CountDetails a
JOIN dbo.CountDetails b ON a.ipAddress = b.ipAddress AND a.counterNumber = b.counterNumber
AND CONVERT(VARCHAR(20),b.updateTime,120) = CONVERT(VARCHAR(20),DATEADD(HOUR,1,a.updateTime),120)

Conversion between timestamp to milliseconds in DB2

I have a column of datatype timestamp. Now I need to convert it to MiiliSeconds and put in another column. How can I do that.
the input is of the format 2011-10-04 13:54:50.455227 and the output needs to be 1317900719
There's a function called timestampdiff. Using it against January 1st 1970 would work otherwise but the function gives approximate results. If you want accuracy you will want to calculate the correct answer with something like
create function ts2millis(t timestamp)
returns bigint
return (
(
(bigint(year(t-1970))*bigint(31556926000))+
(bigint(month(t))*bigint(2629743000))+
(bigint(day(t))*bigint(86400000))+
(bigint(hour(t))*bigint(3600000))+
(bigint(minute(t))*bigint(60000))+
(bigint(second(t))*bigint(1000))+
(bigint(microsecond(t))/bigint(1000))
)
)
#
Your requested output is not miliseconds, but the equivalent to CLib localtime(), here's how to do it:
SELECT
86400*
(
DAYS(TIMESTAMP(v_timestamp))
-
DAYS(TIMESTAMP('1970-01-01-00:00:00'))
)
+
MIDNIGHT_SECONDS(timestamp(v_timestamp))
FROM
SYSIBM.SYSDUMMY1;
where v_timestamp is the variable or column to be calculated.