I am running queries in a hive environment.
I have a column which has a timestamp but is set up a string in the tables. I tried the following : all of them return Null
SELECT
,To_date(activitydate)
Cast:
,cast(activitydate as timestamp)
This is the how the data is set up in the table:
Appreciate any inputs on how I can convert this :
05/12/2017 00:00:00
SELECT
cust_id
,to_date(activitydate) activity_date
,type type_of_contact
FROM repl_task
WHERE to_date(activitydate) BETWEEN '2014-01-01' AND '2017-01-01' ;
I am running out of memory if I run this :
SELECT
cust_id
,activitydate
,SUBSTR(activitydate,4,2) AS DT
,SUBSTR(activitydate,0,2) AS MONTH
,SUBSTR(activitydate,7,4) AS YEAR
,type
FROM task
WHERE activitydate >='01/01/2016 00:00:00'
unix_timestamp function converts given format to unix timestamp and from_unixtime function converts from unix timestamp to given format:
hive> select from_unixtime(unix_timestamp('01/01/2016 00:00:00','MM/dd/yyyy HH:m:ss'),'yyyy-MM-dd');
OK
2016-01-01
Time taken: 0.118 seconds, Fetched: 1 row(s)
Can you try using to_date() with the date portion of your timestamp in ISO format:
SELECT
cust_id,
TO_DATE(CONCAT(SUBSTR(activitydate, 7, 4), '-',
SUBSTR(activitydate, 0, 2), '-',
SUBSTR(activitydate, 4, 2))) activity_date
type type_of_contact
FROM repl_task
WHERE
TO_DATE(CONCAT(SUBSTR(activitydate, 7, 4), '-',
SUBSTR(activitydate, 0, 2), '-',
SUBSTR(activitydate, 4, 2)))
BETWEEN '2014-01-01' AND '2017-01-01';
If this runs, but is not very performant, then you should consider storing your timestamps in ISO format. Storing dates as text, or as text in a non standard format, carries a penalty with most databases.
Related
I have a column ID which has values 10,20,10,10,60,60 and so on. I have another column which is a diff of (modified_date - created_date) as TS which has datetime values like 6 5:28:13.0 (6 is the days), 1 1:34:54.0 and so on for each ID. How can I get the total days and time for each ID value? For example: how can I get the total value of TS for ID = 10?
From the image: for each value of eowstage_nbr column, how can I get total days and time in column TS?
A regular sum() aggregation over the ID column combined with some data type conversions should do it...
Sample data
create table data
(
id int,
created datetime,
updated datetime
);
insert into data (id, created, updated) values
(10, '2020-01-01 00:00:00', '2020-01-01 05:00:10'),
(10, '2020-01-02 00:00:00', '2020-01-03 11:00:03'),
(20, '2020-02-05 00:00:00', '2020-02-05 01:02:03'),
(20, '2020-02-15 00:10:00', '2020-02-17 00:00:10');
Solution
Describing the steps in plain English would sound like:
Take the difference between the created and updated timestamp, expressed in seconds;
add them up the seconds for each id;
convert the seconds to a suitable date format.
I took care of step 3 by adding the seconds up to a reference date 1900-01-01.
Query version:
select d.id,
dateadd(second, sum(datediff(second, d.created, d.updated)), '1900-01-01 00:00:00') as diff_sum
from data d
group by d.id;
Result
The values are not formatted like in your screenshot, but that should boil down to data type selection, datetime formatting and localization.
id diff_sum
-- -----------------------
10 1900-01-02 16:00:13.000
20 1900-01-03 00:52:13.000
Fiddle
Since you did not give any sample data and looking at your immage I assume you have data in TS column as per your image. (I have used dummy data to replicate your TS column). So the steps would be to separate day from time in your TS column, then converting that time into seconds, and doing summation of those seconds and days seperately, and finally combining the days and seconds converted to proper d hh:mm:ss format.
select ID,
cast((sum(noofdays) + SUM(DATEDIFF(S, '00:00:00', noofhrs))/3600/24) as varchar(5)) + ' ' +
right('00'+CAST(SUM(DATEDIFF(S, '00:00:00', noofhrs))/3600%24 AS VARCHAR(2)),2) + ':'
+ right('00'+CAST(SUM(DATEDIFF(S, '00:00:00', noofhrs))%3600/60 AS VARCHAR(2)),2) + ':'
+ right('00'+CAST(((SUM(DATEDIFF(S, '00:00:00', noofhrs))%3600)%60) AS VARCHAR(2)),2)
from
(
select ID, case left(TS,charindex(' ',TS)) when '' then 0 else left(TS,charindex(' ',TS)) end as noofdays,
cast(right(TS,len(TS)-charindex(' ',TS)) as datetime) as noofhrs from
(
select 1 ID,'6 5:28:13.0' TS union
select 1,'1 8:28:13.0' a union
select 1,'2 12:28:13.0' a union
select 2,'5:28:13.0' a union
select 2,'6 5:28:13.0' a
)
tablename
) A
group by ID
I have a data set with the following columns
time date
1310 2020-06-01
1425 2020-06-22
1640 2020-06-29
My desired final output is a column in datetime format that looks as such
datetime
2020-06-01 13:10:00.000
2020-06-22 14:25:00.000
2020-06-29 16:40:00.000
I have used the following command thus far to format the output the way I would like
CONCAT(date_string, ' ', substring(barstarttime, 1, 2), ':', substring(barstarttime, 3, 2), ':00.000'))
However, I have not had success in changing this to a datetime or timestamp.
Is there a function that can help me do so?
Thanks
Get the hours and minutes from the time string with SUBSTR. Then add the according intervals to the date.
I don't know whether Presto allows functions for the interval values. Please try:
select
date +
interval substr(time, 1, 2) hour +
interval substr(time, 3, 2) minute
from mytable;
If this doesn't work, try this instead:
select
date +
((interval '1' hour) * cast(substr(time, 1, 2) as integer)) +
((interval '1' minute) * cast(substr(time, 3, 2) as integer))
from mytable;
As mentioned, I don't know Presto. You may even have to cast the date to a timestamp first:
cast(date as timestamp) +
...
You can use date_parse function.
select date_parse(cast(date as varchar) || ' ' || time, '%Y-%m-%d %H%i') from
(values ('1300', date '2020-06-01')) as t(time, date)
You can use below in order to convert to timestamp
select *,
cast(date_col as timestamp) as ts
from T
Receive date as "19-May" and need to convert it as '05/01/2019', "19-June" to '06/01/2019'.
I have tried various date conversion but it didn't work.
You can try this. Storing date in this format is not a good/suggestion you should use the proper data type which are meant and available for.
You should update the values with proper date time value and then change the data type also. It will save your time & you do not need these conversions every time.
Select Try_Cast('19-May 2019' as Datetime)
OR
Select Try_Cast('19-May' + '2019' as Datetime)
To get the first date of month you can try the below query.
SELECT DATEADD(month, DATEDIFF(month, 0, Try_Cast('19-May 2019' as Datetime)), 0) AS StartOfMonth
Edit
To get the first date of month as per the given data in string, you can use the below query.
declare #dateinStr varchar(20) = '19-May'
Select try_cast('01-' + Replace(#dateinStr, LEFT(#dateinStr, 3), '') + LEFT(#dateinStr, 2) as Datetime) as Date
Here is the demo.
I suppose the months will be 3 chars only, if so then
Select s,
try_Cast(concat(right(s, 3), ' 2019') as Datetime)
from
(
values
('19-May'),
('19-Jun')
) t(s);
If the months is really comes like "June" & "August" then
select s,
try_cast(concat(substring(s, charindex('-',s)+1, 3), ' 2019') as date)
from
(
values
('19-May'),
('19-June'),
('15-August')
) t(s);
If you need to format it as mm/dd/yyyy then use 101 style.
You can do :
SELECT DATEADD(DAY, 1, EOMONTH(CONVERT(DATE, Dates + '-2019'), -1))
FROM ( VALUES ('19-May'), ('19-June')
) t(Dates);
How can I convert 2019-07-01T00:00:00+05:30 to DateTime in SQL?
2019-07-01T00:00:00+05:30 is a varchar field. I need to convert this into DateTime to compare this to a date field.
suggest me a query to Convert (2019-07-01T00:00:00+05:30) into DateTime
Convert To date :
select cast('2019-07-01T00:00:00+05:30' as Date)
Convert To time:
select cast('2019-07-01T00:00:00+05:30' as Time)
Convert To datetime :
select convert(datetime2, '2019-07-01T10:00:30+05:30',0)
Try any of these..
select cast(convert(datetime2, '2019-07-01T10:00:30+05:30',0) as datetime)
select convert(datetime2, '2019-07-01T10:00:30+05:30',0)
One option would be to use a combination of CONVERT on the timestamp without the timezone component, then use TODATETIMEOFFSET with the timezone portion to get the final result:
WITH yourTable AS (
SELECT '2019-07-01T00:00:00+05:30' AS dt
)
SELECT
TODATETIMEOFFSET(CONVERT(datetime, LEFT(dt, 19), 126), RIGHT(dt, 6)) AS output
FROM yourTable;
This outputs:
01/07/2019 00:00:00 +05:30
Demo
Unfortunately, SQL Server truncates the time zone information when converting from datetimeoffset to dateordatetime`. But, you can calculate the offset and add it back in:
select dateadd(minute,
datediff(minute, convert(datetimeoffset, dt), convert(datetime, convert(datetimeoffset, dt))),
convert(datetime, convert(datetimeoffset, dt))
)
from (values ('2019-07-01T00:00:00+05:30')) v(dt);
For your particular timezone, the date at midnight matches the UTC date, so you are safe. I'm on the other side of the world, so this would be a more important consideration in the "western" world ("west" being west of UTC).
The following query will convert the given VARCHAR to DATETIME value:
DECLARE #DateVal AS VARCHAR (30) = '2019-07-01T00:00:00+05:30';
SELECT CAST(REPLACE(SUBSTRING(#DateVal, 0, CHARINDEX('+', #DateVal)), 'T', ' ') AS DATETIME);
I have the data in the following format in varchar form. There are 48 million rows in this format
'2015-09-18 00:00:00.000'
and want to convert it to the following format
'2015-09-18'
Can anyone help me with the code in Oracle
If your column has a timestamp type, you simply need to use to_char to format it properly:
with yourTable(yourDateColumn) as
(
select to_timestamp('2015-09-18 00:00:00.000', 'YYYY-MM-DD HH24:MI:SS.FF') from dual
)
select to_char(yourDateColumn, 'yyyy-mm-dd')
from yourTable
If your column is a string ( and storing dates in string fields is generally a very bad idea) with a fixed format, you simply need a substr:
with yourTable(yourStringColumn) as
(
select '2015-09-18 00:00:00.000' from dual
)
select substr(yourStringColumn, 1, 10)
from yourTable