Adding decimal hours using DATEADD in Oracle SQL - amazon-redshift

I have the following piece of query which works fine for adding hours to a time value:
to_char(date_add('h', trunc(o.tt_real)::integer, d.pickup_date::timestamp),'HH24:MI') injection_time
In the above tt_real is an integer. Now lets assume I have a tt with decimal hours like this 2.98, 3.55 etc. Can someone suggest an updated version of the above query to incorporate decimal hours ?
An example of the output expected
10:30 + 2.25 = 12:45

rather add minutes instead of hours: (o.tt_real*60)::integer

Related

Total today, total yesterday ... total 7 days ago, total last week, average this month -data awareness in Power BI

I want an easy way to have a sum of yesterday, the day before and the day before that total 7 columns (past 7 days)
Then i would like average for L7D, Average LM.
I have made a column in my date that indicate what "today" is and then my idea was to have it sum if today, sum if today-1, sum if today-2 but this does not seem to work
)
Yesterday= CALCULATE(SUM('DanvægtLines'[NAV_Qty]);FILTER('DanvægtLines';'DanvægtLines'[Dato_Anden]>=TODAY()-1)
(It take the sum of quanto, then filters if the 2.date on the order is today)
It is only working with ">=" not if i only use "=" ... witch is ok for yesterday, but the if i want to have only 4 days ago i have to filter yesterday, the day before, and 3 days ago... witch make a very very long code line i dont get why
=(today()-4 wont work)
Ok so after about 15 hours of hair pulling it dawned on me that the table I was referring to was in the date+time format, so fixing that I could use =today()-1

What time format is this?

I'm going through this list of dates and times and I've been googling for a few days now and haven't figured out this time format:
201612102200038814 -> 10.12.2016 19:01
201611182200029224 -> 18.11.2016 11:55
201611162200028449 -> 16.11.2016 19:06
2016080521998919 -> 5.8.2016 19:47
2016091522320593 -> 15.9.2016 18:53
It's obvious that the first 8 digits (20161210) are YYYYMMDD, but latter part doesn't make any sense to me....
Do you have any ideas? Is it even a valid time format?
It seems to be some kind of timestamp, since 2,200,029,224 is very close to 2,200,028,449, and the dates of each are only 2 days apart. So the difference is 775, and the date difference is roughly 1.75 days.
The difference between the first two is 9590, which is larger, which makes sense since the difference between those dates is almost a month.
The oldest date you have in the list also has the smallest timestamp, which is consistent with this theory.
The ratios also match up. 775 / 1.75 days = 442. 9590 / 22 days = 435. If I calculated exact seconds differences I bet these ratios would match up nearly perfectly. So then each hour the timestamp goes up by 18 (roughly), or 3 every 10 minutes. This is a pretty bizarre timestamp, but it does fit, and can be used to calculate dates from the given timestamp.

How to tweak the SET intervalstyle (change the Interval Output) in PostgreSQL?

I have read in this online PostgreSQL documentation... http://www.postgresql.org/docs/9.4/static/datatype-datetime.html#INTERVAL-STYLE-OUTPUT-TABLE
in the point 8.5.5 something about how to tweak the default Interval Output. . I mean the default interval is shown like this...
00:00:00.000 (if the timedifference is lower than a day or month or year)
1 day 00:00:00.000 (if the timedifference reaches days, but is lower than a month or a year)
1 month 1 day 00:00:00.000 (if the timediffence reaches months, but is lower than a year)
1 year 1 month 1 day 00:00:00.000 (if it reaches years, months, days)
it evens uses plurarl cases (years, mons, days) when their values are greater than one.
All these variations make difficult to any other app when SELECTing (query) this interval values (as text) to convert it to a proper time. So I would like postgresql to always show year, month n days, even if their value are 0 (it could be even better if it could show the date part of the interval like this... 01-11-30, adding zeros to the left side when values are less than ten)
I know I can change the interval to text, using to_char() but I really would like to avoid that, I would like some good fellow postgresql programmer to tell me if it is true that there is a way to tweak the Interval Output as is said in the postgresql documentation.
Thanks Advanced.
PD: two more links about the subject
https://my.vertica.com/docs/7.1.x/HTML/Content/Authoring/SQLReferenceManual/Statements/SET/SETDATESTYLE.htm
http://my.vertica.com/docs/6.1.x/HTML/index.htm#13874.htm
You can set the interval output style, but only to one of a few pre-defined formats that are unambigious on input, and that PostgreSQL knows how to parse back into intervals. Per the documentation these are the SQL standard interval format, two variants of PostgreSQL specific syntax, and iso_8601 intervals.
If you want something familiar and easy to parse, consider using:
SET intervalstyle = 'iso_8601'
and using an off-the-shelf parser for ISO1601 intervals.

kdb casting date to day count yield incorrect result

KDB for mortals , so KDB can flexibly convert date to underlying day count.
Advanced: The underlying day count can be obtained by casting to int.
`int$2000.02.01
31
This doesn't look correct to me , why convert Feb month yield 31 days ? or I am reading the document incorrectly
It yields the day count since the epoch date (2000.01.01) in kdb+.
Between 2000.01.01 to 2000.02.01, there are 31 days.
WookiKent's answer is correct. To add to this however, if you're looking for some date object type attributes to extract year, month, day as ints, the following may be helpful:
dateObject:{`date`year`month`day!x,"I"$"."vs string[x]}
dt:dateObject[2000.02.01]
q)dt[`date]
2000.02.01
q)dt[`year]
2000
q)dt[`month]
2
q)dt[`day]
1

Microsoft Hex dates

I have the following from a Microsoft SQL Server database for date/time value:
0x00009CEF00A25634
I found this post:
Help me translate long value, expressed in hex, back in to a date/time
Which seemed to be on the right track but by using the code I didn't get the right dates, are my hex dates in a different format? How would I convert them to a normal date, I am using PHP/PostgreSQL.
select CAST (0x00009CEF00A25634 as datetime) gives 2009-12-30 09:51:03.000
This is two integers. One for the date part 0x00009CEF (decimal 40175) and one for the time part 00A25634 (decimal 10638900). The date part is a signed integer giving number of days since 1 Jan 1900. The time part is an integer representing number of ticks.
There are 300 ticks in a second.
It can be seen that the following also returns the same result
SELECT DATEADD(MILLISECOND,10638900*10/3.0, DATEADD(DAY,40175, '19000101'))
You will need to figure out how to apply this to postgres.
Edit: an answer here apparently does this. I haven't tested it myself.
This works for me while migrating from SQL to MySQL :
SELECT (CAST('1900-01-01 00:00:00' + INTERVAL CAST(CONV(substr(HEX( 0x0000A249004576D0 ),1,8), 16, 10) AS SIGNED) DAY + INTERVAL CAST(CONV(substr(HEX( 0x0000A249004576D0 ),9,8), 16, 10) AS SIGNED)* 10000/3 MICROSECOND AS DATETIME)) AS newdate