Converting a timestamp value to number - oracle

I've a timestamp value in the format 2005-01-31T00:00:00.000-05:00. I want covert it to a number in the format20050131, to equate it to a column which has the datatype of number.
I tried to do it by to_number(to_date(timestamp, 'yyyymmdd')). But it is resulting in error not a valid month.
Could you please help me in resolving it.

try using like below
to_number(to_char(timestamp, 'yyyymmdd'))
first you have to convert to char and then to number.

If your timestamp value is a VARCHAR2 value, maybe this will help:
WITH tstmp AS (
SELECT '2005-01-31T00:00:00.000-05:00' AS val FROM dual
)
SELECT TO_NUMBER(TO_CHAR(FROM_TZ(TO_TIMESTAMP(SUBSTR(val, 1, 23), 'YYYY-MM-DD"T"HH24:MI:SS.FF3'), SUBSTR(val, 24)) , 'YYYYMMDD')) AS newval
FROM tstmp;

Related

Change a decimal string into a timestamp in Impala

How do you convert a string type like
t1.updte_timestamp
2018-06-02-08.18.45.562742
2018-05-26-09.18.16.594824
into a timestamp? SHOULD RESULT IN:
2018-06-02-08.18.45
2018-05-26-09.18.16
ETC
The values had been imported from excel and are in STRING-TYPE
I tried:
SELECT
to_timestamp(cast (t1.updte_timestamp as string), 'yyyy-mm-dd hh:mm:ss') as updted_timestamp FROM OLD;
but results in NULL for all values
thank you
you can substr your string and apply to_timestamp as follow
select to_timestamp(substr('2018-06-02-08.18.45.562742', 1, 19) , 'yyyy-MM-dd-HH.mm.ss');
Make sure you use MM for month and HH for hour in upper case

Always display decimal at the end of the integer in oracle

I have a requirement where i always need to display the number with the decimal point.
The datatype of the db column is that of number.
If the value is 1.25 it gets displayed as 1.25
But if the value is 1 it does gets displayed as 1 and I need to display the value as 1.00.
I need to perform rpad (right padding) operations once I get the result in the decimal format.
Without the decimal, the value of a whole number would be different from what is present in the database.
Example:
SELECT RPAD(ROUND(12,2), 5 ,0) AS test
FROM DUAL;
results in 12000 whereas I am expecting it to be 12.00.
Any pointers on this would help.
Thanks!
Use an appropriate to_char call. Something like
SELECT to_char( <<your number>>, '0.00' )
FROM dual;
That will return the string "1.00" when you pass in a value of 1. If you pass in a value of 0, it will return the string "0.00". If you pass in a a value of 1.25, it will return the string "1.25".
Try using a number format along with the TO_CHAR function, as:
SELECT TO_CHAR(12, 99.99) AS test
FROM DUAL;
Reference:
You can find documentatation related to other ways to format numbers here.
Try this:
select TRIM(to_char(100.5, '99999999.00')) FROM DUAL
The format specifications are here:
http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm#i34570

How to convert timestamp to a 5-6digit number?

I have a tiemstamp date like this 12/31/1999 12:00:00.000000 AM
I want it to turn into this 121999. Basically omit the day.
If the month is between January-September, the number should be 5 digits(IE.21998), October to December should be 6 digits(IE.102014)
I have something that works but it looks like a caveman. Is there a better way?
SELECT TO_NUMBER(SUBSTR(TO_CHAR(EX_DATE,'MMDDYYYY'),1,2))
||TO_NUMBER(SUBSTR(TO_CHAR(EX_DATE,'MMDDYYYY'),5)) AS EX_DATE
FROM TOL
Thanks in advance
I think you just want
SELECT to_char( ex_date, 'fmMMYYYY' ) as ex_date
FROM tol
if you want a 5 or 6 character string or
SELECT to_number( to_char( ex_date, 'fmMMYYYY' )) as ex_date
FROM tol
if you want a number.
Of course, I would hesitate to call either a string or a number ex_date. And I would hesitate to call a column of type timestamp something like ex_date that implies that the data type is really a date. I would either use something that identifies the data type properly, i.e. ex_ts for a timestamp, or I would change the data type to match the name of the column.

"Select Extract Minute" failed in oracle

Please help me to solve this problem.
I need to extract minute from hour ("JAM" column) in a table .
I have try this query :
WITH recordabsen AS
(SELECT userid,
TO_CHAR(checktime,'MM/DD/YYYY') AS tanggal ,
MIN(TO_CHAR(checktime,'hh24:mi')) AS JAM
FROM checkinout
WHERE USERID = '688'
AND (checktime BETWEEN to_date('04/01/2013','MM/DD/YYYY') AND to_date('05/01/2013','MM/DD/YYYY'))
AND checktype = 'I'
GROUP BY userid,
TO_CHAR(checktime,'MM/DD/YYYY')
)
SELECT EXTRACT (MINUTE FROM JAM) AS minute
FROM recordabsen
WHERE to_date(JAM,'hh24:mi') > TRUNC(to_date(JAM,'hh24:mi')) + 8/24
but returns an error :
Invalid Extract field
As soon as you've done:
to_char(checktime,'hh24:mi')
you got a string and not a date, so maybe it's better to use strings functions and not date functions, i.e.:
substr("JAM", 4)
Here is a sqlfiddle demo
Why don't you just pass the checktime from the WITH clause query? Then, if checktime is a date, you can cast it to TIMESTAMP, which will allow you to use EXTRACT MINUTE:
SELECT EXTRACT (MINUTE FROM CAST(SYSDATE AS TIMESTAMP)) FROM dual;
Check at SQLFiddle: http://www.sqlfiddle.com/#!4/d41d8/18054

convert oracle date string to number

I need to convert a date string -'2013-01-01' to number --20130101 type..How can I accomplish it in Oracle efficiently?
My Input-
'2013-01-01'
My Output
Output-20130101
select to_number(replace('2013-01-01', '-')) from dual;
You can convert the string to date and convert back using the format that you want to:
select to_char(to_date('2013-01-01', 'YYYY-MM-DD'), 'YYYYMMDD');
More information about datetime format:
http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm
to_char(your_date,'YYYYMMDD')
Just the basics of using to_char with a date here.
You can also "exploit" the format mask:
select to_number('2013-01-01', '9999G99G99', 'nls_numeric_characters=,-')
from dual
Here is a sqlfiddle demo
To convert date string to number:
Always convert it to char first then to number:
Select TO_NUMBER(TO_CHAR(to_date('2013-01-01', 'YYYY-MM-DD'),'YYYYMMDD')) from dual
if your column datatype is DATE then use your column name in place of sysdate in the below:
Select TO_NUMBER(TO_CHAR(sysdate,'YYYYMMDD')) from dual

Resources