I have database, where i have inserted timestamps(6) in this format :
18-AUG-14 02.49.27.000000000 PM .
I want to extract it into this: 2014-08
Its called ISO 8601
You'll need to use the to_char function to extract the year-month from timestamp.
select to_char(timestamp, 'yyyy-mm') from your_table
I've done this in this way -
select extract(year from timestmp) || '-' || extract(month from timestmp) from texmp1;
Hope this helps.
Here is the table structure:
create table texmp1
(
timestmp timestamp
);
Related
Select * from Table where date >='20210911' + 30days
the date is in numeric format and what to pull the records for a specific date +30days of specific date
Could you please help
Uh. Never store dates into any other datatype column but DATE. Now you first have to "convert" it, then do the arithmetic.
select *
from some_table
where to_date(date_column, 'yyyymmdd') > date '2021-09-11' + 30
--------
apply format mask that matches data in that column
Hope (should I say pray?) that all values represent valid dates. Nobody prevents you to store e.g. 20228579 into it, and - applying to_date to it - results in
SQL> select to_date('20228579', 'yyyymmdd') from dual;
select to_date('20228579', 'yyyymmdd') from dual
*
ERROR at line 1:
ORA-01843: not a valid month
SQL>
Once again, bad, BAD idea!
how to apply between clause ( where date_column between date '2021-09-11' and date '2021-09-11'+30
If you have an index on the column that you want to use then convert the value to a date then add 30 days to it and convert it back to a number:
SELECT *
FROM Table_Name
WHERE date_number BETWEEN 20210911
AND TO_NUMBER(
TO_CHAR(
TO_DATE(20210911, 'YYYYMMDD')
+ INTERVAL '30' DAY,
'YYYYMMDD'
)
)
If you don't have an index and want a simpler query then:
SELECT *
FROM Table_Name
WHERE TO_DATE(date_number, 'YYYYMMDD') BETWEEN DATE '2021-09-11'
AND DATE '2021-09-11' + INTERVAL '30' DAY
The best solution would be to convert your numeric column to a DATE column:
ALTER TABLE table_name ADD date_column DATE;
UPDATE table_name SET date_column = TO_DATE(date_number, 'YYYYMMDD');
ALTER TABLE table_name DROP COLUMN date_number;
Then:
SELECT *
FROM Table_Name
WHERE date_column BETWEEN DATE '2021-09-11'
AND DATE '2021-09-11' + INTERVAL '30' DAY
db<>fiddle here
I have a table with one column giving me dates in the form 24-JUL-17 and another column a string telling me the hour in the form hh:mm:ss.several_decimals but they are in 24 hours and not AM/PM, could someone point me on how I can add a column in oracle that combines them both into a timestamp?
CAST the date to a timestamp data type and use TO_DSINTERVAL to convert the time column to a DAY TO SECOND INTERVAL and then add one to the other:
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE table_name (
date_column DATE,
time_column VARCHAR2(15)
);
INSERT INTO table_name ( date_column, time_column )
VALUES ( DATE '2017-06-24', '12:34:56.789012' );
Query 1:
SELECT CAST( date_column AS TIMESTAMP )
+ TO_DSINTERVAL( '+0 ' || time_column ) AS datetime
FROM table_name
Results:
| DATETIME |
|----------------------------|
| 2017-06-24 12:34:56.789012 |
Here is an example with a fabricated table.
WITH test_data AS (
SELECT
SYSDATE date_val,
'14:23:15.123' AS time_val
FROM
dual
) SELECT
to_timestamp(TO_CHAR(date_val,'MM/DD/YYYY')
|| ' '
|| time_val,'MM/DD/YYYY HH24:MI:SS.FF3')
FROM
test_data
Just concatenate both input strings with || and convert it with TO_TIMESTAMP. The format is a bit tricky, took me a while to get it right.
CREATE TABLE mytable (mydate VARCHAR2(9), mytime VARCHAR2(20), mytimestamp TIMESTAMP);
INSERT INTO mytable (mydate, mytime) VALUES ('24-JUL-17', '22:56:47.1915010');
Test it
SELECT TO_TIMESTAMP(mydate||mytime, 'DD-MON-YYHH24:MI:SS.FF9') FROM mytable;
2017-07-24 22:56:47,191501000
Or, in a table:
UPDATE mytable
SET mytimestamp = TO_TIMESTAMP(mydate||mytime, 'DD-MON-YYHH24:MI:SS.FF9');
This is how my Date column looks like
RPT_DT (Data Type is Number)
20180131
20180130
20180129
I wanna extract month out of these dates(either Month or mm), and I tried below
select extract(month from to_date(Rpt_dt))
from
(
select distinct to_char(to_date(RPT_DT,'yyyymmdd'),'mm/dd/yyyy') Rpt_dt
from TABLE_NAME
)
I am getting the error "Not a valid month"
if there is not any particular reason to have a double conversion I would suggest you to handle the problem with this simple query:
select substr(to_char(RPT_DT),5,2)from THE_TABLE
this query should be more performant since it make only one conversion. in your sample you transform:
a number to a date
then a date to a char
the char again in date
finally you extract the month
let me know if it help
r.
try this,
SELECT EXTRACT(MONTH FROM TO_DATE(rpt_dt, 'YYYYMMDD'))
FROM TABLE_NAME;
and I believe you need to modify your query as you did not put the format 'MM/DD/YYYY',
select extract(month from to_date(Rpt_dt, 'MM/DD/YYYY'))
from
(
select distinct to_char(to_date(RPT_DT,'yyyymmdd'),'mm/dd/yyyy') Rpt_dt
from TABLE_NAME
)
This back-and-forth conversion is useless. Try this
select
extract(month from to_date(RPT_DT,'yyyymmdd'))
from TABLE_NAME;
In Oracle I have a table with a DB_FLD_4 defined as VARCHAR2(20 BYTE) but actually holds DATE information with looks like this:
select DB_FLD_4 from DM_SUPDES_DISTRIB order by DB_FLD_4 desc;
4/9/2017
4/7/2017
4/6/2017
Kind of m/d/yyyy format.
I would like to create SP that would delete every day rows from the table older than 1 month.
My sysdate looks like this:
select sysdate from dual;
SYSDATE
24-APR-17 04.41:00
Please help to manipulate formats to make this possible.
Many thanks !!!
Use to_date() on your column, then trunc the sysdate to "round" to the day
delete
from MyTable
where to_date(MyColumn, 'MM/DD/YYYY') < add_months(trunc(sysdate), -1)
I have SQL query as follow
MONTH(ADD_MONTHS(TRAN_DATE,1))
and then in oracle extract function for month I have
EXTRACT(MONTH FROM TRAN_DATE)
So now how do I add month in Extract function in oracle query like SQL query
Assuming that you mean SQL-Server when talking about SQL, is this what you want?
EXTRACT( MONTH FROM ADD_MONTHS( tran_date, 1 ) )
The following might work for you:
select EXTRACT( MONTH FROM ADD_MONTHS( sysdate, 1 ) ) from dual