I'm trying to display data from database ( two different tables) where two columns are type date. I have a problem with formatting: I need to get date and time but I'm getting date and 00:00:00 for time. Does any one know how to use to_char: I tried this but it doesn't work (query laid out for readability in SO):
query.append("SELECT CONCAT(ins.id,'_'
,com.compte,'_'
,com.cin,'_'
,com.nomAff,'_'
,ins.to_char(dateDemande,'DD/MM/YYYY HH:MI:SS ') ,'_'
,ins.typeTPEdemande ,'_'
,ins.statut,'_'
,ins.to_char(dateStatut,'DD/MM/YYYY HH:MI:SS '))
FROM InstallationTPE ins , Commercant com
WHERE com.compte=ins.compte
AND ins.statut = ?");
Thank you
This is wrong: ins.to_char(dateDemande,'DD/MM/YYYY HH:MI:SS '). The TO_CHAR call must wrap the entire name, including the table alias. So do this:
to_char(ins.dateDemande,'DD/MM/YYYY HH:MI:SS ')
Same for dateStatut.
Related
In SAP HANA I can use filters as SQL or Column Store expression within a modelling view (e.g. Calculation View).
I'm trying to convert the time string like 08:00:00 into timestamp which should always take today's date.
The single hana routing I've found until now is the function TO_TIMESTAMP( [, ]).
Is there any build in function for my requrement or how else would you solve this problem?
Thanks and BR.
EDIT: this is how I try to concatenate the today's date and entered time into timestamp:
longdate(string(date(now())) +string(' ')+string(time($$P_StartShift$$))+string('.123456'))
HANA doesn't have a neat construction API for datetime data types.
So, this is somewhat a workaround.
The idea here is that you have the time of the day provided as user input.
With that, we can calculate the seconds since midnight, which in turn can be added to the current_date.
In HANA SQL this is what it looks like:
select
to_time ('13:01', 'HH24:MI') user_input_time
, current_date
, seconds_between ('00:00'
, to_time ('13:01', 'HH24:MI')) secs_since_midnight
, add_seconds (current_date
, seconds_between ('00:00'
, to_time ('13:01', 'HH24:MI')
)
) current_date_user_time
from
dummy;
/*
USER_INPUT_TIME CURRENT_DATE SECS_SINCE_MIDNIGHT CURRENT_DATE_USER_TIME
1:01:00 PM 25/08/2020 46,860 25/08/2020 1:01:00.0 PM
*/
I´m trying to calculate the difference between two dates in Oracle and getting the result as a TimeStamp. This is the easiest thing to do in SQL Server, but it seems that Oracle does not have a easy way to solve this. I refuse to believe that I have to write that much code to get what I need. Can someone tell me if there is a easier way to get that difference?:
SELECT TO_CHAR(EXTRACT(HOUR FROM NUMTODSINTERVAL(enddate-startdate, 'DAY')), 'FM00')
|| ':' ||
TO_CHAR(EXTRACT(MINUTE FROM NUMTODSINTERVAL(enddate-startdate, 'DAY')), 'FM00')
|| ':' ||
TO_CHAR(EXTRACT(SECOND FROM NUMTODSINTERVAL(enddate-startdate, 'DAY')), 'FM00')
I need the result be something like:
enddate = '2017-03-01 17:30:00'
startdate = '2017-03-01 10:00:00'
difference: 07:30:00
Substract the two dates. Add the result to the current date (without any time component, trunc(sysdate)) and show only the time.
select to_char(trunc(sysdate) + (to_date('2017-03-01 17:30:00', 'YYYY-MM-DD HH24:MI:SS') -
to_date('2017-03-01 10:00:00', 'YYYY-MM-DD HH24:MI:SS'))
,'HH24:MI:SS')
from dual
I have table 'PORT_DTLS' with following fields: TN, Status_id, Req_Start_Time. Req_Start_Time has the time in this below format '8/12/2017 2:22:14.490361 PM'.
I have tried with this query..
select TN,Status_id
from PORT_DTLS
where Req_Start_Time between '8/11/2017 2:22:14.490361 PM'
and '8/12/2017 2:22:14.490361 PM';
But I am getting 'not a valid month' error.
Anyone please help me.
Your data looks like the column is a TIMESTAMP not a DATE. You should use an explicit formatting to cast those strings appropriately.
select TN,Status_id
from PORT_DTLS
where Req_Start_Time between to_timestamp('8/11/2017 14:22:14.490361' , 'DD/MM/YYYY HH24:MI:SS.FF')
and to_timestamp('8/12/2017 14:22:14.490361','DD/MM/YYYY HH24:MI:SS.FF');
I've assumed your dates are DD/MM; perhaps they are really MM/DD, in which case you need to tweak the format masks I posted. Most likely that ambiguity is why you're getting that ORA-01843 error.
i want to convert date to some other format.
Below is the example 04/03/10 09:00:50.000000000 AMto YYYYMM
Iam not able to get this , below is the query which i used to convert.
select to_char(to_date('04/03/10 09:00:50.000000000 AM','MM/DD/YYYY HH:MM:SS AM'),'YYYYMM') from table;
Iam getting exception as below
ORA-01810: format code appears twice
01810. 00000 - "format code appears twice"
Format Code for Minutes is MI, not MM. MM is for months.
You are using 2-digit year. Better to use RR for this. Even better use 4-digit year.
TO_DATE doesn't store fractional seconds. You need to use TO_TIMESTAMP and use the FF as format code.
So, your query would be
select to_char(to_timestamp('04/03/10 09:00:50.000000000 AM','MM/DD/RR HH:MI:SS.FF9 AM'),'YYYYMM')
from table;
To achieve your goal there are many issues to resolve ;)
Finally I made this like that:
select to_char(
to_timestamp('04/03/10 09:00:50.000000000 AM','MM/DD/YYYY HH:MI:SS.FF9 PM',
'nls_date_language = ENGLISH'),
'YYYYMM') from dual;
I wanna catch the date in my table which is written like this "20140205101309" I tried to get by many ways, but i was not able to catch it so how to modifiy my query to catch it ?
select * from my.DETAIL a where A.DATE
between to_DATE('YYYYMMDD hh24:mi:ss','28-dec-2013 12:00:00')
and to_date ('YYYYMMDD hh24:mi:ss','2-feb-2014 00:00:00');
thank you in advance
Make a.call_date a date before comparing:
select *
from operation.reject_detail a
where to_date(a.call_date, 'YYYYMMDDHHMISS') between
to_date('28-12-2013 12:00:00','DD-MM-YYYY HH24:MI:SS') and
to_date('02-02-2014 00:00:00','DD-MM-YYYY HH24:MI:SS' );
Assuming 20140205101309 is: 2014, februari, 5 10:13:09
If you are not interested in the time part do this:
select *
from operation.reject_detail a
where to_date(substr(a.call_date,1,8), 'YYYYMMDD') between
to_date('28-12-2013','DD-MM-YYYY') and
to_date('02-02-2014','DD-MM-YYYY');
Storing dates as strings is bad practice, but since that is what you have and it's in a relatively sensible format, you can just compare it as a string:
select * from my.DETAIL a
where A.DATE between '20131228120000' and '20140202000000'
Or to ignore the time part on the table and include the full second date:
select * from my.DETAIL a
where A.DATE between '20131228000000' and '20140202235959'
There doesn't seem to be much point converting everything to DATE types, particularly if your column is indexed. If it isn't indexed than you could just look at the date part:
where substr(A.DATE, 1, 8) between '20131228' and '20140202'