In clickhouse, is there a function can parse date/datetime in a given format, like
SELECT TO_TIMESTAMP('2022-01-17 11:37:00','YYYY-MM-DD HH12') FROM TEST_DATETIME_FUNC_1;
Expected result: 2022-01-17 11:00:00
Parse or convert related functions, such as: toDateTime64(), parseDateTimeBestEffort() doesn't provide format function; formatDateTime() will get "2022-01-17 11"
These functions convert STRING type to DATETIME type.
Clickhouse does not have a type for time without minutes and seconds.
There is function toStartOfHour
SELECT toStartOfHour(toDateTime('2022-01-17 11:37:00'))
┌─toStartOfHour(toDateTime('2022-01-17 11:37:00'))─┐
│ 2022-01-17 11:00:00 │
└──────────────────────────────────────────────────┘
https://clickhouse.com/docs/en/sql-reference/functions/date-time-functions/#tostartofhour
Also check other functions
toStartOfDay
toStartOfHour
toStartOfMinute
toStartOfSecond
toStartOfFiveMinute
toStartOfTenMinutes
toStartOfFifteenMinutes
toStartOfInterval(time_or_data, INTERVAL x unit [, time_zone])
toTime
Moreover, if you have string with other format try to use parseDateTimeBestEffort function https://clickhouse.com/docs/en/sql-reference/functions/type-conversion-functions/#parsedatetimebesteffort
Related
How to convert yyyy-mm-ddThh:mm:ss [+-] hh:mm to timestamp yyyy-mm-dd hh:mm:ss in Datastge?
Eg:
From
2021-01-26T01:07:00-05:00
To
2021-01-26 01:07:00
Depending on the original data type you could use string functions
and StringToTimestamp
Otherwise you could check out Date and Time functions as well.
If the data type is string, convert the "T" to a space " " using Convert() function.
Use Left() function to get rid of the "+/- hh:mm".
If the data type is timestamp, do nothing until you need to display it; the internal representation of a timestamp does not have a "T" in it. If you're writing it to a text file, for example, use TimestampToString() function to specify your desired format, although the default format may well suit your need.
I've created_date column as a string and value of it like 2018-10-04 15:42:19.000404667 +0000 UTC m=+103.387519062 which I got from mongo db columns and now I inserted it into mysql table and of course it is of string type. Now the problem is that I can't parse it and formatted it as well, here I tried with below code to parse but can't get solution.
tm, err := time.Parse("2006-02-01", "2018-10-04 15:42:19.000404667 +0000 UTC m=+103.387519062")
if err != nil {
fmt.Println(err)
}
And it printed with some errors like :
parsing time "2018-10-04 15:42:19.000404667 +0000 UTC m=+103.387519062" as "2006-01-02T15:04:05.999999999Z07:00": cannot parse " 15:42:19.000404667 +0000 UTC m=+103.387519062" as "T"
I tried with different const of time package but still digging into it, what I'm missing here or am I doing parse with wrong date type.
Thanks.
Unless the m=... part is known to be constant (in which case you should include it in your format string as-is), you will have to strip it from the input string before passing it to time.Parse(). There is no way to tell time.Parse() that it should ignore a part of the input (except if it is always the same string).
NOTE you should provide a complete time specifier as the format, not just "2006-02-01", because if you don't, the library will extend it in its own way to include hours, minutes and seconds - which isn't necessarily the way you want it (in your case it automatically added T15:04:05.999999999Z07:00 - but your input data contains a space after the date, not T, so it won't match). In your case (judging by your example input data), the format string should be like this: 2006-02-01 15:04:05.000000000 -0700 MST
I'm trying to comvert a string in my logs to a date object.
My string is 2018-09-18 11:42:50,286000201 which is the format YYYY-MM-DD hh:mm:ss,nnnnnnnnn
I'm trying to convert in to an object using the time library in ruby. The function I am using is Time.strptime('2018-09-18 11:42:50,286000201, '%Y-%m-%d %H:%M:%S,%9N')
Ruby is giving me an invalid striptime format. Any help would be appreciated. Cheers.
remove the 9, %N expects 9 digits bt default
Time.strptime('2018-09-18 11:42:50,286000201, '%Y-%m-%d %H:%M:%S,%N')
doc: http://ruby-doc.org/stdlib-2.1.1/libdoc/time/rdoc/Time.html#method-c-strptime
I've seen that you can use an ".isValid()" function to check that a given string is in a date format:
moment('2007-05-05', 'YYYY-MM-DD', true).isValid()
But is there a way to confirm that the format is correct? For example:
'YYYY-MM-DD' should return true, but
'YYYY-MM-DDsadsadl' should return false since the characters at the end of the string aren't valid DateTime chars.
We're working on a tool that allows a user to input an existing date format, and then a second input to enter the desired format, but we need validation to ensure the string can properly parse and convert, but they aren't entering a specific date.
The application must accept any and all possible date formats.
Use the following function to validate your format.
validFormat = function(inputFormat){
var validation = moment(moment('2017-06-17').format(inputFormat), inputFormat).inspect();
if(validation.indexOf('invalid') < 0)
return true;
else
return false;
}
Do spend some time to understand this. This simply does a reverse verification using inspect(). The date 2017-06-17 can be replaced by any valid date.
This Moment Js Docs will help you identify the valid formats.
Just make a call to this function as
validFormat('YYYY MM DD')
const getIsValid = inputFormat => moment(moment().format(inputFormat), inputFormat).isValid()
Explanation:
moment().format(inputFormat) - Create a date string from the current time from that format
This is then wrapped with moment() to make that string a moment date object, defining the format to parse it with. Finally we call the isValid() property on that moment date object. This ensures we are able to both create and parse a moment with our custom format.
Want to use the
<input name=attendance[something] type="time">
For time input. However cant make it seem to match with a Datetime object; for manipulation before insertion to ActiveRecord
myTimeIn = Time.strptime(params[attendance][something],"%H:%M")
keeps getting
invalid strptime format - `%H:%M'
What is the correct format for a input type=time field?
Looks like the value of params[attendance][something] may be blank or not of the correct format. You could do something like below to avoid the error:
t = params[attendance][something]
myTimeIn = Time.strptime(t,"%H:%M") if t =~ /\d{1,2}:\d{2}/
As per this HTML example, the value produced by <input/> of type time is HH:MM