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');
Related
I would like to make data in this table more optimise. I have now like this:
And I tried
SELECT *
FROM T_TEST
PIVOT (
MAX(cnt) FOR ASSIGNMENT_ROLE IN ('2nd case','1st case','3rd case')
);
I got this result which has nulls and doesn't work for me
Instead I would like something like this:
I guess you have a table with at least some date column and the assignment_role. For example:
create table role_whatever(
log_date date,
assignment_role varchar2(30),
whatever varchar2(42));
Then you can get the desired report with:
select *
from (
select assignment_role,trunc(log_date) dt
from role_whatever
) pivot (
count(*) for assignment_role in (
'1st case' as FIRST_CASE,
'2nd case' as SECOND_CASE,
'3rd case' as THIRD_CASE))
order by dt;
Note, since the log_date column may contain a time part, I used trunc() on it.
Test data for this table was generated with:
insert into role_whatever
select date '2022-07-15' + dbms_random.value(1,5) log_date,
decode(round(dbms_random.value(1,3)),1,'1st',2,'2nd',3,'3rd') || ' case' assignment_role,
dbms_random.string('A',10) whatever
from dual
connect by level < 10000;
If you have a table with the aggregated result as shown in your first image, then this query gives the same result:
select *
from (
select assignment_role,dt,cnt
from role_aggregated
) pivot (
sum(cnt) for assignment_role in (
'1st case' as FIRST_CASE,
'2nd case' as SECOND_CASE,
'3rd case' as THIRD_CASE))
order by dt;
The table above was created with:
create table role_aggregated as
select count(*) cnt,assignment_role,trunc(log_date) dt
from role_whatever
group by assignment_role,trunc(log_date);
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 TIMESTAMP datatype and I need to compare month value to select all table values. Example the created is the field and wanted to get all the rows which is created between November & December of any year. Tried with below query and it don't work.
select * from table_name where TO_CHAR(created_time, 'mon') in ('nov','dec')
Use EXTRACT:
SELECT *
FROM table_name
WHERE EXTRACT( MONTH from created_time ) IN ( 11, 12 )
Or, you can use TO_CHAR( created_time, 'MM' ) to get the numeric month value (and not worry about language settings as you would have to with the MON format model):
SELECT *
FROM table_name
WHERE TO_CHAR( created_time, 'MM' ) IN ( '11', '12' )
db<>fiddle
Well, there are several ways to achieve this.
1.Using the nlsparam of to_char function
The 'nlsparam' argument specifies the language in which month and day names and abbreviations are returned.
Example
SQL> create table t ( c1 timestamp ) ;
Table created.
SQL> alter session set nls_timestamp_format = 'dd.mm.yyyy hh24:mi:ss.rr' ;
Session altered.
SQL> insert into t values ( systimestamp - 30 ) ;
1 row created.
SQL> insert into t values ( systimestamp ) ;
1 row created.
SQL> select * from t ;
C1
---------------------------------------------------------------------------
30.07.2020 09:29:35.20
29.08.2020 09:29:42.20
SQL> select value from nls_database_parameters where parameter='NLS_LANGUAGE' ;
VALUE
--------------------------------------------------------------------------------
AMERICAN
SQL> select c1 , to_char(c1, 'mon' , 'NLS_DATE_LANGUAGE = american') as mon from t ;
C1
---------------------------------------------------------------------------
MON
------------
30.07.2020 09:29:35
jul
29.08.2020 09:29:42
aug
2.However, to avoid depending in the language, you can use extract and compare the number of the month which is the same in any language. In this case you need to convert the timestamp to a date, but before you need to set the nls_timestamp_format to the specific format.
SQL> alter session set nls_timestamp_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select c1 , extract(month from to_date(c1,'dd.mm.yyyy hh24:mi:ss')) from t ;
C1
---------------------------------------------------------------------------
EXTRACT(MONTHFROMTO_DATE(C1,'DD.MM.YYYYHH24:MI:SS'))
----------------------------------------------------
30.07.2020 09:29:35
7
29.08.2020 09:29:42
8
I have a column with TIMESTAMP type in my Oracle Database. The default column value is SYSTIMESTAMP.
I want to SELECT milliseconds FROM the TIMESTAMP column. I use the query below without success:
SELECT TO_CHAR (MY_TIMESTAMP, 'dd-mm-yyyy hh24:mi:ss.FF') AS MY_TIMESTAMP
FROM MY_TABLE
-- Result: 20-12-2015 15:23:28.
As you see the result does not have any milliseconds and it is empty. If I change the query with .FF4 then it results .0000.
How can I SELECT the column with milliseconds?
The precision for timestamp columns can be set up by
TIMESTAMP [(fractional_seconds_precision)].
In your case for 4 it's:
create table my_table
(id number,
my_TIMESTAMP timestamp(4) default SYSTIMESTAMP);
You can check your current precision by:
select column_name, data_scale from user_tab_columns
where table_name = 'MY_TABLE' and column_name = 'MY_TIMESTAMP';
Here is sample in SQL Fiddle
The display can be change by:
alter session set NLS_TIMESTAMP_FORMAT = 'DD-MON-RRRR HH:MI:SS.FF9';
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
);