An example of this field is "/products/106017388" in the table.
What SQL query shall I write to get the number 106017388 from the field.
Many thanks.
You can try hive function regexp_extract
Something like
select regexp_extract(field_name, "([0-9]+)$", 1) from table_name;
Debuggex Demo for the description about the regex ([0-9]+)$
Documentation
You may use the split command in hive to extract the required value.Like below;
select * from test_stackoverflow;
1 /products/106017388
2 /products1/06017388
Time taken: 0.66 seconds, Fetched: 2 row(s)
select split(value,'[/]')[2] from test_stackoverflow;
OK
106017388
06017388
Time taken: 0.105 seconds, Fetched: 2 row(s)
Hope this helps!
SUBSTR('/products/106017388',11)
to get only the integer part..
Related
I have a requirement to convert the mentioned input string format and produce the desired output in timestamp as shown below.
Input: 16AUG2001:23:46:32.876086
Desired Output: 2001-08-16 23:46:32.876086
Output which is coming by running the below code: 2001-08-17 00:01:08
Query:
select '16AUG2001:23:46:32.876086' as row_ins_timestamp,
from_unixtime(unix_timestamp('16AUG2001:23:46:32.876086',
'ddMMMyyyy:HH:mm:ss.SSSSSS')) as row_ins_timestamp
from temp;
Milliseconds part is not getting converted as required. Please suggest.
unix_timestamp function does not preserve milliseconds.
Convert without milliseconds, then concatenate with millisecond part:
with your_data as (
select stack(3,
'16AUG2001:23:46:32.876086',
'16AUG2001:23:46:32',
'16AUG2001:23:46:32.123'
) as ts
)
select concat_ws('.',from_unixtime(unix_timestamp(split(ts,'\\.')[0],'ddMMMyyyy:HH:mm:ss')),split(ts,'\\.')[1])
from your_data;
Result:
2001-08-16 23:46:32.876086
2001-08-16 23:46:32
2001-08-16 23:46:32.123
Time taken: 0.089 seconds, Fetched: 3 row(s)
How to return month from varchar column and values like "20180912" in hive?
It's strange that it worked fine with function month() on string type in hive,however it returns null now.
And month(from_unixtime(unix_timestamp)(date,'yyyymmdd')) return vaules that do not match the real month
Use substr():
hive> select substr('20180912',5,2);
OK
09
Time taken: 1.675 seconds, Fetched: 1 row(s)
I'm having a table x it contain the column resource_name in this column I'm having data like NASRI(SRI).
I'm applying initcap on this column it's giving output Nasri(sri). But my expected output is Nasri(Sri).
How I can achieve the desired result?
Thank you
One possible solution is to use split() with concat_ws(). If value does not contain '()', then it will also work correctly. Demo with ():
hive> select concat_ws('(',initcap(split('NASRI(SRI)','\\(')[0]),
initcap(split('NASRI(SRI)','\\(')[1])
);
OK
Nasri(Sri)
Time taken: 0.974 seconds, Fetched: 1 row(s)
And for value without () it also works good:
hive> select concat_ws('(',initcap(split('NASRI','\\(')[0]),
initcap(split('NASRI','\\(')[1])
);
OK
Nasri
Time taken: 0.697 seconds, Fetched: 1 row(s)
I want the time portion from a timestamp column.
E.g. if load_ts= 2016-01-04 11:34:35, then I want only 11:34:35. I have tried using select concat(hour(load_ts),':',minute(load_ts),':',second(load_ts)) as note_time from rushisourcepart; but it is not giving correct output.
Please give your suggestions.
You can use substr(load_ts, 12)
hive> select substr('2016-01-04 11:34:35', 12);
OK
11:34:35
How can I get the difference in minutes between 2 timestamp fields in google bigquery?
The only function I know is Datediff which gives the difference in day
Thanks
Pentium10's answer works for Legacy SQL. If you're using Standard SQL you can use TIMESTAMP_DIFF and it will do the math for you:
TIMESTAMP_DIFF(timestamp_1, timestamp_2, MINUTE)
https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#timestamp-functions
Use TIMESTAMP_TO_USEC(<timestamp>) and do the math.
https://developers.google.com/bigquery/query-reference#datetimefunctions
DATETIME_DIFF(datetime_1, datetime_2, MINUTE/SECOND)