Bad status request on Impala - sql

Bad status for request 37563: TGetOperationStatusResp(status=TStatus(errorCode=None, errorMessage=None, sqlState=None, infoMessages=None, statusCode=0), operationState=5, errorMessage=None, sqlState=None, errorCode=None)
I am facing this when i tried to run the following query.
refresh sst_spot_quotes;
select
case
when maxrecordforSST >= hours_sub (CURRENT_TIMESTAMP(), 4) then ''
else 'error'
end as msg
from
(
select
to_timestamp (
CONCAT (
max(rdate),
MAX(
CONCAT (
LPAD (CAST(rhour as STRING), 2, '0'),
':',
LPAD (CAST(rminute as STRING), 2, '0'),
':',
LPAD (CAST(rsecond as STRING), 2, '0')
)
)
),
'yyyy-mmm-ddhh:mm:ss'
) as maxrecordforSST
from
sst_spot_quotes
where
rdate >= days_sub (to_date (CURRENT_TIMESTAMP()), 1)
) a
Could someone help to resolve this error.

The date format must be yyyy-MMM-ddhh:mm:ss. Pls note i made MMM as uppercase.
Could you please change and try?
Also, its not a good idea to get the max and then to_timestamp but reverse. Because calculating max on date will give correct maximum date than string.
You can give below SQL a try.
select
case
when maxrecordforSST >= hours_sub (CURRENT_TIMESTAMP(), 4) then ''
else 'error'
end as msg
from
(
select
MAX(to_timestamp (
CONCAT (
(rdate),
(
CONCAT (
LPAD (CAST(rhour as STRING), 2, '0'),
':',
LPAD (CAST(rminute as STRING), 2, '0'),
':',
LPAD (CAST(rsecond as STRING), 2, '0')
)
)
),
'yyyy-MMM-ddhh:mm:ss'
)) as maxrecordforSST
from
sst_spot_quotes
where
rdate >= days_sub (to_date (CURRENT_TIMESTAMP()), 1)
) a

Related

how to sum up minutes and seconds ?in oracle

I have a column called duration_d which is varchar2 and the data in that table looks like below
duration_d
-----------
12:25
01:35
12:10
04:21
12:18
12:24
I tried below query
SELECT SUM( to_date( duration_d, 'mi:ss' ))
FROM table
GROUP BY calling_number;
When I execute it following error is coming
ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
can any one tell me how to make sum it?
To get the total as fractions of a day you can use:
SELECT SUM( TO_DATE( duration_d, 'MI:SS' ) - TO_DATE( '00:00', 'MI:SS' ) ) AS total
FROM your_table
Which gives the result:
TOTAL
------------------------------------------
0.0383449074074074074074074074074074074074
To convert this to an interval data type you can use NUMTODSINTERVAL:
SELECT NUMTODSINTERVAL(
SUM( TO_DATE( duration_d, 'MI:SS' ) - TO_DATE( '00:00', 'MI:SS' ) ),
'DAY'
) AS total
FROM your_table
Which gives the result:
TOTAL
-------------------
+00 00:55:13.000000
Please try below:
with x as
(select sum((regexp_substr(YOUR_COLUMN, '[0-9]+', 1, 1)*60) +
regexp_substr(id, '[0-9]+', 1, 2)) seconds
from YOUR_TABLE)
SELECT
TO_CHAR(TRUNC(seconds/3600),'FM9900') || ':' ||
TO_CHAR(TRUNC(MOD(seconds,3600)/60),'FM00') || ':' ||
TO_CHAR(MOD(seconds,60),'FM00')
FROM x
Will work only if the duration is always [MI:SS].
Also you can add the group by as per your requirement.
Converting Seconds to the required duration format Reference.
Group By
with x as
(select calling_number,sum((regexp_substr(YOUR_COLUMN, '[0-9]+', 1, 1)*60) +
regexp_substr(id, '[0-9]+', 1, 2)) seconds
from YOUR_TABLE
group by calling_number)
SELECT calling_number,
TO_CHAR(TRUNC(seconds/3600),'FM9900') || ':' ||
TO_CHAR(TRUNC(MOD(seconds,3600)/60),'FM00') || ':' ||
TO_CHAR(MOD(seconds,60),'FM00')
FROM x
Use a combination of SUBSTR, to_char, to_date, NVL, INSTR, reverse and SUM.
SELECT "calling_number",
to_char(to_date(SUM(NVL(SUBSTR("duration_d", 0, INSTR("duration_d", ':')-1), "duration_d"))*60 +
SUM(substr("duration_d", - instr(reverse("duration_d"), ':') + 1)),'sssss'),'hh24:mi:ss') AS SUM_DURATION_D
FROM yourtable
GROUP BY "calling_number"
Output
calling_number SUM_DURATION_D
1 00:26:10
2 00:29:03
SQL Fiddle: http://sqlfiddle.com/#!4/9b0a81/33/0
Correct spelling as below
SELECT SUM( TO_DATE( duration_d, 'mi:ss' ) )
FROM YOURTABLE Group By calling_number

How to convert String to Datetime including time part in SQL [duplicate]

This question already has answers here:
How to convert datetime string without delimiters in SQL Server as datetime?
(6 answers)
Closed 6 years ago.
I want to convert below string to DateTime in SQL.
20140601152943767
I know convert(date,'20140601152943767') this but I want time part also.
Above function only returns me Date part.
Thanks in advance.
I would use following solution:
SELECT CONVERT(DATETIME, STUFF(STUFF(STUFF(STUFF('20140601152943767', 9, 0, ' '), 12, 0, ':') , 15, 0, ':'), 18, 0, '.'))
Note #0: All those STUFF calls will convert source strings from 20140601152943767 to 20140601 15:29:43.767.
Note #1: SELECT STUFF('abcef', 4, 1, 'DDD') will replace substring starting from index 4 with a length of 1 char (e) with DDD -> abcDDDf
Note #2: SELECT STUFF('abcef', 4, 0, 'DDD') returns abcDDDef
You can try lik ethis:
select
concat(convert(date,LEFT('20140601152943767',8)), ' ' , Convert(time,Dateadd(SECOND,
Right('20140601152943767',2)/1,
Dateadd(MINUTE,
Right('20140601152943767',4)/100,
Dateadd(hour,
Right('20140601152943767',6)/10000,
'1900-01-01')))) )
as myDate
Output:
2014-06-01 22:38:07.0000000
Well, first of all - you need conversion not to date but to datetime type.
Second - you should always specify format as mentioned:
https://msdn.microsoft.com/en-us/library/ms187928(v=sql.120).aspx
e.g.
select convert(datetime, '20140501', 112)
3 - there is no such format supported for your value demonstrated, so you have to modify your value yo something like yyyy-mm-ddThh:mi:ss.mmm (iso) or to make custom conversion with substring and so on.
;WITH myvalues AS (
SELECT '20140601152943767' value
)
SELECT
convert(date, LEFT(mv.value, 8), 112),
cast(STUFF(STUFF(STUFF(STUFF(mv.[value], 1, 8, ''), 7, 0, '.'), 5, 0, ':'), 3, 0, ':') AS TIME)
FROM myvalues mv
Try This one
declare #datetime varchar(20) = '20140601152943767'
select convert(varchar(20),convert(date,LEFT(#datetime,8))) + ' ' + substring(RIGHT(#datetime,9), 1, 2)
+ ':' + substring(RIGHT(#datetime,9), 3, 2)
+ ':' + substring(RIGHT(#datetime,9), 5, 2)
+ '.' + substring(RIGHT(#datetime,9), 7, 3)
I know some might argue that this is not the best way , but still it's an alternative.Few people have already used convert and stuff functions .
Try this out.Easy to understand.Also test your subqueries.
select y1||'-'||M1||'-'||d1||' '||h1||':'||mi||':'||s1||':'||f1 as xdate
from
(
select substr('20140601152943767',1,4) Y1 from dual
),
(
select substr('20140601152943767',5,2)M1 from dual
),
(
select substr('20140601152943767',7,2) d1 from dual
),
(
select substr('20140601152943767',9,2) h1 from dual
),
(
select substr('20140601152943767',11,2) mi from dual
),
(
select substr('20140601152943767',13,2) s1 from dual
),
(
select substr('20140601152943767',15,3) f1 from dual
)
Output:
2014-06-01 15:29:43.767

convert integer to Days

I'm looking to convert an integer to Days using a db2 database. The integers are in this format 20130101 or YYYYMMDD. I believe you have to write a custom function after converting the integer to a char but I was unsure of how to do the second conversion to DAYS. I'm looking for a returned format January, 1, 2013 from 20130101.
WITH
/*****************************************************
*** Sample Data ***
*****************************************************/
sample_data
( START_DATE , END_DATE ) AS
(
VALUES
(20130101, 20131227 )
, (20130930, 20131230 )
, (20130411, 20130912 )
, (20130410, 20140101 )
)
,
t2(START_DATE, END_DATE) AS
( SELECT
CAST(SUBSTR(START_DATE, 1,4) CONCAT '-'
CONCAT SUBSTR(START_DATE, 5,2) CONCAT '-'
CONCAT SUBSTR(START_DATE, 7,2) AS CHAR(15)),
CAST(SUBSTR(END_DATE, 1,4) CONCAT '-'
CONCAT SUBSTR(END_DATE, 5,2) CONCAT '-'
CONCAT SUBSTR(END_DATE, 7,2) AS CHAR(15))
FROM SAMPLE_DATA
)
SELECT
START_DATE,
END_DATE
FROM t2
You can use this:
select monthname(to_date(20130101, 'YYYYMMDD')) || ', ' ||
day(to_date(20130101, 'YYYYMMDD')) || ', ' ||
year(to_date(20130101, 'YYYYMMDD')) from sysibm.sysdummy1
The result is:
January, 1, 2013
Replace the integer 20130101 by your field name.
If you will be using the conversion in several places, it's probably better to create a function to avoid repeating the field and conversions.
You could also cut a few corners using aritmetics to get year and day, such as this:
select monthname(to_date(20130101, 'YYYYMMDD')) || ', ' ||
mod(20130101, 100) || ', ' || to_char(20130101 / 10000) from sysibm.sysdummy1
The result is the same.
It's a lot of casting, but you can use the TIMESTAMP_FORMAT function:
date(timestamp_format(char(start_date),'YYYYMMDD'))
Keep in mind that this just gets you a value that is an actual DATE, not necessarily in the "pretty" format that you list above.

Convert DB2 SQL Decimal to DATE

I need to convert Decimal to date. I have a decimal date field that contains data like this :
1,132,009.00 --1/13/2009
7,152,004.00 --7/15/2004
11,012,005.00 --11/01/2005
etc
I would like it to read as xx/xx/xxxx.
Is there anyway to do this with SQL commands or DB2 logic in a select statement?
SELECT column1 from table1 ;
WITH x(decvalue) AS ( VALUES (DECIMAL(1132009.00)),(DECIMAL(7152004.00)),(DECIMAL(11012005.00)) )
SELECT CAST(
LPAD( RTRIM( CHAR( INTEGER( decvalue/1000000 ))), 2, '0' ) || '/' ||
LPAD( RTRIM( CHAR( MOD( decvalue/10000, 100 ))), 2, '0' ) || '/' ||
MOD( decvalue, 10000 )
AS CHAR(10))
AS chardateresult
FROM x
;
Using the same WITH values as #Fred, I came up with:
WITH x(decvalue) AS ( VALUES (DECIMAL(1132009.00)),(DECIMAL(7152004.00)),(DECIMAL(11012005.00)) )
SELECT TO_DATE(CHAR(CAST(decvalue AS DECIMAL(8,0))), 'MMDDYYYY')
FROM x
This assumes that your input values aren't going to be longer than 8 digits (2 for month, 2 for day, 4 for year), otherwise you'll get an overflow error on the cast. It will also fail if there's not at least some value for each of month, day, and year (00002011 would not work, for example, but 01012011 would).

Regarding sql instr function

Oracle 10g is the db.
The below query fails when extracting the date.
SELECT TO_CHAR ( TO_DATE ( SUBSTR (file_name , INSTR (file_name , '_', -1, 2)+ 2, 8), 'YYYYMMDD'), 'DD-MM-YYYY') from dual;
I noticed we receive the below two file name of different naming formats.
660.ASSD.0063.20100923.0409.PTH2.IAC1.gcr_H201009231416151671_bbor.ddr
660.ASSD.M2K_20110309121547_489.ddr
For one file the above query works . The other file 660.ASSD.M2K_20110309121547_489.ddr
it extracts "01103091" and does a to_date fails. How can i modify this query so it works for both the file formats.
Use REGEXP_SUBSTR
select TO_CHAR(TO_DATE(SUBSTR(REGEXP_SUBSTR('660.ASSD.0063.20100923.0409.PTH2.IAC1.gcr_H201009231416151671_bbor.ddr', '\d{14,17}'), 0, 8), 'yyyymmdd'), 'dd-mm-yyyy')
from dual;
select TO_CHAR(TO_DATE(SUBSTR(REGEXP_SUBSTR('660.ASSD.M2K_20110309121547_489.ddr', '\d{14,17}'), 0, 8), 'yyyymmdd'), 'dd-mm-yyyy')
from dual;
You can also use REGEXP_REPLACE to strip-out letters from the file name.
SELECT TO_CHAR ( TO_DATE ( SUBSTR (regexp_replace(file_name, '[A-Z][a-z]', '')
, INSTR (regexp_replace(file_name, '[A-Z][a-z]', '') , '_', -1, 2)+ 1, 8), 'YYYYMMDD'), 'DD-MM-YYYY')
FROM dual;