ClickHouse has function toDateTime64() to convert string into DateTime64 data type.
Example from official documentation:
SELECT * FROM dt WHERE timestamp = toDateTime64('2019-01-01 00:00:00', 3, 'Europe/Moscow')
It takes 3 parameters:
Date string
Integer
Timezone
But there is no info about the second parameter. What does it mean?
That's precision.
3 is milliseconds (2019-01-01 03:00:00.000),
6 is microseconds (2019-01-01 03:00:00.000000)
and so on.
You can find more info in DateTime64 datatype description https://clickhouse.tech/docs/en/sql-reference/data-types/datetime64/
Related
Want to retrieve a date type from a postgres table using liqpq PQexecParams() in binary mode (please humor me).
https://www.postgresql.org/docs/14/datatype-datetime.html says that a date is 4 bytes (4713 BC to 5874897 AD).
src/include/utils/date.h defines:
typedef int32 DateADT;
But obviously given the supported date range it's not a normal int. Something like this:
int32_t haha = be32toh(*((uint32_t *) PQgetvalue(res, 0, 17)));
Gives haha=1466004328 for 2022-10-25.
Which is clearly not a day count and since its not a ratio of 86,400 is also not seconds since an epoch. Number is also too small to be microseconds.
How do I interpret the 4 bytes of postgresql 'date' data?
Added Later:
This question contains an error - PQgetvalue() references column 17 (a text value) instead of column 18 (a date value) - with that corrected haha=8332
Date is an integer day count from POSTGRES_EPOCH_JDATE (2000-01-01).
I'm trying to convert duration in one column which is written in milliseconds (Ex: 600,2101,1110....) to hh:mm:ss.000 format(Ex:00:00:00.600, 00:00:02.101...) using the below formula in google spreadsheets:
=CONCATENATE(TEXT(INT(A1/1000)/86400,"hh:mm:ss"),".",A1-(INT(A1/1000)*1000))
It gives correct values for almost all , but one type of values which is durations having '0' as their second digit (Eg: 2010,3056,1011).
When 0 is the second digit , the after decimal value in hh:mm:ss.000 is rounded to the third digit and 0 is ignored (Example row 1 and 2 in below table). But for other durations it gives right value(row 3).
I need a formula that works well on all type of values i.e 1080 → 00:00:01.080 and not 00:00:01.80 .
Can someone please help with this.
Duration in milliseconds
hh:mm:ss.000 format
1080
00:00:01.80 (wrong)
2010
00:00:02.10 (wrong)
1630
00:00:01.630 (correct)
try:
=INDEX(IF(A2:A="",,TEXT(A2:A/86400000, "hh:mm:ss.000")))
I've a requirement to migrate data from some on-premise databases to the cloud. Some of the data in the tables is stored as 'date' in format yyyy-mm-dd.
We are converting the data stored in the tables into Avro format and then it's copied into Snowflake.
In Avro, date is stored as an integer Avro Date type
When I try to push the data into snowflake, it's unable to convert that integer back into date. I get the following error: 'Failed to case VARIANT 13707 to date'
where 13707 is number of days since Jan 1 1970
Thanks!
You need to calculate the date value based on the variant value. You can use DATEADD for this purpose:
https://docs.snowflake.com/en/sql-reference/functions/dateadd.html
create table avro_test ( x date );
insert into avro_test(x)
select dateadd('day',parse_json('13707'),'1970-01-01');
select * from avro_test;
+------------+
| X |
+------------+
| 2007-07-13 |
+------------+
If the format of the input parameter is a string that contains an integer:
After the string is converted to an integer, the integer is treated as a number of seconds, milliseconds, microseconds, or nanoseconds after the start of the Unix epoch (1970-01-01 00:00:00.000000000 UTC).
If the integer is less than 31536000000 (the number of milliseconds in a year), then the value is treated as a number of seconds.
If the value is greater than or equal to 31536000000 and less than 31536000000000, then the value is treated as milliseconds.
If the value is greater than or equal to 31536000000000 and less than 31536000000000000, then the value is treated as microseconds.
If the value is greater than or equal to 31536000000000000, then the value is treated as nanoseconds.
If more than one row is evaluated (for example, if the input is the column name of a table that contains more than two rows), the first processed value determines whether all subsequent values are treated as seconds, milliseconds, microseconds, or nanoseconds.
If the first value is greater than or equal to 31536000000, then all values will be treated as milliseconds, even if some remaining values are less than 31536000000. Similar logic applies for microseconds and nanoseconds.
In SQL Server, I have a query that returns a value of 177200. I need this value represented as 1772.00 as the last 2 digits are past the decimal. The query below is adding .00 to the end of the full value. I have no experience in this type of SQL statement. Any help would be appreciated.
SELECT
STR(SUM(ActualPrice), 10, 2) AS Total, Department
FROM
#DepartmentSalesData
GROUP BY
Department
The data type you're looking for is called numeric
SELECT CAST(SUM(ActualPrice) / 100.0 AS numeric(18, 2)) AS Total, ...
FROM ...
You're passing in a precision (18 in my example) and a scale 2 in my example, as requested by you.
I received timestamp datasets which are in the format of yyyy-mm-dd HH:MM:SS.ms. I want to convert into yyyy-mm-dd HH:MM:SS format. Is there any way to select only in this format using matlab?
For example:
2012-08-01 00:10:00.0
should be:
2012-08-01 00:10:00
Please note that the millisecond values are all zero.
The general way would be to use datestr to convert it to your desired format.
dates = {'2012-08-01 00:10:00.1';
'2012-08-01 00:10:00.1'};
new = datestr(dates, 'yyyy-mm-dd HH:MM:SS');
% 2012-08-01 00:10:00
% 2012-08-01 00:10:00
Another approach would be that since all of your milliseconds are going to be zero (therefore you don't have to worry about rounding) you can just use a regular expression to remove the milliseconds component (anything after the decimal point)
new = regexprep(dates, '\..*', '')
This is likely going to be more performant as you don't need to perform the intermediate step of converting to either a datetime object or a date number.
Since the input and output format are the same except for the milliseconds, don't use date functions, but simple string operations:
% example dates
C = {'2012-08-01 00:10:00.0'
'2013-08-02 00:11:11.0'
'2014-08-03 00:12:22.0'
'2015-08-04 00:13:33.0'
'2016-08-05 00:14:44.0'};
% method 1
D = cellfun(#(x)x(1:end-2), C, 'UniformOutput', false);
% method 2 (same, but no cellfun)
D = char(C);
D = cellstr(D(:,1:end-2));
% method 3
D = regexp(C, '[^\.]*', 'match', 'once');
% method 4
D = regexprep(C, '\..*$', '');
Lets say you need this data in datetime objects anyway then i would do something like this:
inp = {'2012-08-01 00:10:00.0'; '2012-08-02 04:10:00.0'}; % Some datestrins
t = datetime(inp,'InputFormat','yyyy-MM-dd HH:mm:ss.0'); % Convert to datetimes
datestr(t, 'yyyy-mm-dd HH:MM:SS') % convert back to strings
For the input & output formatter see the documentation. I assume that the last part is always zero.