I have created an external table, everything looks good except for one of the date format, it looks like it defaults to MM/DD/YYYY but I want it as YYYYMMDD as it is in the file. The column is DATE_ID. Anyone have any clues?
Thanks!
CREATE TABLE R2SCHD.AAFES_RDF_XTRNL_CAL_HIER
(
DATE_ID DATE,
DATE_DESC DATE,
WEEK_ID VARCHAR2(25 BYTE),
WEEK_DESC DATE,
MNTH_ID VARCHAR2(25 BYTE),
MNTH_DESC VARCHAR2(25 BYTE),
QRTR_ID VARCHAR2(25 BYTE),
QRTR_DESC VARCHAR2(25 BYTE),
HALF_ID VARCHAR2(25 BYTE),
HALF_DESC VARCHAR2(25 BYTE),
YEAR_ID VARCHAR2(25 BYTE),
YEAR_DESC VARCHAR2(25 BYTE),
DOW_ID VARCHAR2(25 BYTE),
DOW_DESC VARCHAR2(25 BYTE)
)
ORGANIZATION EXTERNAL
( TYPE ORACLE_LOADER
DEFAULT DIRECTORY R2SCHD_STATIC_DATA
ACCESS PARAMETERS
( RECORDS DELIMITED BY NEWLINE
NOBADFILE
NOLOGFILE
NODISCARDFILE
FIELDS TERMINATED BY ','
MISSING FIELD VALUES ARE NULL
(
DATE_ID CHAR(8) DATE_FORMAT DATE MASK "YYYYMMDD",
DATE_DESC CHAR(10) DATE_FORMAT DATE MASK "MM/DD/YYYY",
WEEK_ID,
WEEK_DESC CHAR(10) DATE_FORMAT DATE MASK "MM/DD/YYYY",
MNTH_ID, MNTH_DESC, QRTR_ID, QRTR_DESC, HALF_ID,
HALF_DESC, YEAR_ID, YEAR_DESC, DOW_ID, DOW_DESC
)
)
LOCATION (R2SCHD_STATIC_DATA:'rdft_cal_external.dat')
)
REJECT LIMIT UNLIMITED
NOPARALLEL
NOMONITORING;
You created the date_id as DATE data type, which is correct. Dates do not have a "format" - what you see on your screen, with slashes, is dictated by your NLS_DATE_FORMAT setting. What are you using - SQL*Plus? SQL Developer? Toad?
For example in SQL Developer: Tools - Preferences - Database - NLS and you will see (and be able to change) your date format. That is what controls what is shown on your screen - not the format you used for your external table (it won't help you to set it as 'YYYYMMDD' there).
Related
I am trying to implement the audit mechanism for our application so that all messages, errors, request/response etc. will be logged centrally in one table 'PROJECT_AUDIT'.
Below stored proc will be used to insert the data into 'PROJECT_AUDIT' table.
Here, I want to convert 'p_timestamp' value received as input to this stored proc into GMT time zone.
Can you please help.
Stored Procedure:
CREATE OR REPLACE PROCEDURE PROJECT_AUDIT_INSERT_RECORD(
p_message_Type IN PROJECT_AUDIT.message_type%TYPE,
p_component_Name IN PROJECT_AUDIT.component_name%TYPE,
p_username IN PROJECT_AUDIT.USERNAME%TYPE,
p_timestamp IN PROJECT_AUDIT.timestamp%TYPE,
p_request_Number IN PROJECT_AUDIT.request_number%TYPE,
p_module_Name IN PROJECT_AUDIT.module_name%TYPE,
p_process_name IN PROJECT_AUDIT.process_name%TYPE,
p_version IN PROJECT_AUDIT.version%TYPE,
p_task IN PROJECT_AUDIT.task%TYPE,
p_error_Code PROJECT_AUDIT.error_code%TYPE,
p_error_Message PROJECT_AUDIT.error_message%TYPE,
p_message PROJECT_AUDIT.message%TYPE
)
IS
BEGIN
INSERT INTO PROJECT_AUDIT ("MESSAGE_TYPE", "COMPONENT_NAME", "USERNAME", "TIMESTAMP", "REQUEST_NUMBER", "MODULE_NAME", "PROCESS_NAME", "VERSION", "TASK", "ERROR_CODE", "ERROR_MESSAGE", "MESSAGE")
VALUES (p_message_Type, p_component_Name, p_username, p_timestamp, p_request_Number, p_module_Name, p_process_name, p_version, p_task, p_error_Code, p_error_Message, p_message);
COMMIT;
END;
/
Table:
CREATE TABLE PROJECT_AUDIT (
ID NUMBER GENERATED ALWAYS AS IDENTITY,
MESSAGE_TYPE VARCHAR2(64 CHAR),
COMPONENT_NAME VARCHAR2(64 CHAR),
USERNAME VARCHAR2(32 CHAR),
TIMESTAMP TIMESTAMP WITH TIME ZONE NOT NULL,
REQUEST_NUMBER VARCHAR2(64 CHAR),
MODULE_NAME VARCHAR2(256 CHAR),
PROCESS_NAME VARCHAR2(256 CHAR),
VERSION VARCHAR2(64 CHAR),
TASK VARCHAR2(128 CHAR),
ERROR_CODE VARCHAR2(256 CHAR),
ERROR_MESSAGE VARCHAR2(4000 CHAR),
MESSAGE VARCHAR2(4000 CHAR)
);
Just use at time zone to convert it.
p_timestamp at time zone 'UTC'
will return a new timestamp with time zone which has been converted to UTC (aka GMT). See this fiddle for an example.
We have a table where we are trying to create a RANGE partition on CHAR data type for each month as mentioned below. But we are getting errors.The ACC_DATE value will be like '202010'. Can this be done in ORACLE? because the same can be done in DB2. But we have to implement it in ORACLE.
Error we are getting:
ORA-14751: Invalid data type for partitioning column of an interval partitioned table
CREATE TABLE T_ACCOUNT_PARTITION
(
V_ACCOUNT_NUM VARCHAR2(100 CHAR),
V_ACCOUNT_NAME VARCHAR2(200 CHAR),
ACC_DATE CHAR(6)
)
PARTITION BY RANGE (ACC_DATE) INTERVAL (NUMTOYMINTERVAL(1, 'MONTH'))
(PARTITION P_MAY2021 VALUES LESS THAN (202105));
As already mentioned you should never store date or time values as strings, use always proper DATE or TIMESTAMP data type.
As a workaround you can use VIRTUAL column like this:
CREATE TABLE T_ACCOUNT_PARTITION
(
V_ACCOUNT_NUM VARCHAR2(100 CHAR),
V_ACCOUNT_NAME VARCHAR2(200 CHAR),
ACC_DATE CHAR(6),
PARTITION_KEY TIMESTAMP(0) GENERATED ALWAYS AS (TO_TIMESTAMP(ACC_DATE, 'YYYYMM')),
)
PARTITION BY RANGE (PARTITION_KEY) INTERVAL (NUMTOYMINTERVAL(1, 'MONTH'))
(PARTITION P_MAY2021 VALUES LESS THAN (TIMESTAMP '2021-05-01 00:00:00'));
How can i get xml data from a table,
CREATE TABLE PCRD_3DS_SOAP_LOGS
(
SERVICE_NAME VARCHAR2(50 BYTE) NOT NULL,
REQUEST_ID VARCHAR2(50 BYTE) NOT NULL,
ERROR_CODE CHAR(5 BYTE),
REQUEST SYS.XMLTYPE,
RESPONSE SYS.XMLTYPE,
USER_CREATE VARCHAR2(15 BYTE),
DATE_CREATE DATE,
USER_MODIF VARCHAR2(15 BYTE),
DATE_MODIF DATE
)
I get this error when I execute the select query:
select * from PCRD_3DS_SOAP_LOGS
Access violation at address 659A0C4B in module 'OraClient12.Dll'. Read of address 00000008
SELECT * FROM PCRD_3DS_SOAP_LOGS;
Should work, however, if Toad cannot handle the XMLTYPE data type then you can explicitly convert them to a string using XMLTYPE's getStringVal or getClobVal methods:
SELECT service_name,
request_id,
error_code,
p.request.getClobVal() AS request,
p.response.getClobVal() AS response,
user_create,
date_create,
user_modif,
date_modif
FROM PCRD_3DS_SOAP_LOGS p;
(You do need to include the table name or alias before the column name.)
db<>fiddle here
My shift_date column of my shift table has all the dates of the shift requests recieved by the agency.It contains other columns which shows whether the shift was filled or cancelled. I want to use this to create a time dimension and link to the fact table to find out how many shifts were filled each month,cancelled in a quarter,etc. i have created the time table as below. I am doing it in SQL developer and to understand dimensional modelling which I am having trouble getting my head around.
my shift table:
shift_date |shift_status|request_id|
-----------------------------------------
09-01-2011|Filled |8899
21-01-2011 |Cancelled |6677
and so on.
I created a time dimension table as below:
CREATE TABLE "DIM_TIME"
( "TIME_KEY" NUMBER(10,0),
"FULL_DATE" DATE,
"DAY_NAME" VARCHAR2(9 BYTE),
"DAY_OF_WEEK" NUMBER(5,0),
"DAY_NUMBER_IN_MONTH" NUMBER(2,0),
"DAY_NAME_ABBREVATED" VARCHAR2(5 BYTE),
"WEEKDAY_FLAG" VARCHAR2(2 BYTE),
"WEEK_OF_THE_YEAR" NUMBER(5,0),
"WEEK_BEGIN_DATE" DATE,
"MONTH_NUMBER" NUMBER(3,0),
"MONTH_NAME" VARCHAR2(20 BYTE),
"MONTH_ABBREVATED" VARCHAR2(5 BYTE),
"QUARTER" NUMBER(5,0),
"YEAR" NUMBER(5,0),
"LAST_DAY_IN_MONTH_FLAG" VARCHAR2(2 BYTE)
);
how can i extract the dates from the shift_date column to populate this time_dimension table? I am not expecting the whole code but if anyone can point me in the right direction it will be helpful as i am still learning.Thank you
This will create the rows in the DIM_TIME table:
create sequence s_time_key START WITH 1;
insert into dim_time (
time_key,
full_date,
day_name,
day_of_week,
day_number_in_month,
day_name_abbrevated,
weekday_flag,
week_of_the_year,
week_begin_date,
month_number,
month_name,
month_abbrevated,
quarter,
year,
last_day_in_month_flag
)
select
s_time_key.nextval,
d,
to_char(d,'Day'), --Monday
to_char(d,'D'), --1-7, monday=2 in some countries, 1 in others (NLS)
to_char(d,'DD'), --1-31
to_char(d,'Dy'), --Su, Mo, ...
decode(to_char(d,'Dy'),'Sa','N','Su','N','Y'),
to_char(d,'IW'), --week num ISO standard
--to_char(d,'WW'), --week num other
d+1-to_char(d,'D'), --first day in week, depending on NLS
to_number(to_char(d,'MM')),
to_char(d,'Month'),
to_char(d,'MON'),
to_char(d,'YYYYQ'),
to_char(d,'YYYY'),
decode(to_char(d+1,'DD'),'01','Y','N')
from (select distinct shift_date d from shift);
In Oracle 12c, instead of a sequence to get the time_key column, you could use create table dim_time ( time_key number(10) generated by default on null as identity, ... to autoincrement the key.
The select above will only populate table DIM_TIME with the dates that actually exists in table SHIFT. But often one will want all dates in a certain period, for instance all the days Jan 1st 2014 trough 2018 like this: replace the last line in the sql above with this:
.
from (
select d from (
select to_date('1970','YYYY')+level d from dual connect by level<=366*100
)
where to_char(d,'YYYY')
between 2014 and 2018
);
It's important to learn the to_char and to_date functions to handle dates in Oracle. https://www.techonthenet.com/oracle/functions/to_char.php
I'm trying to export an existing Oracle SQL database schema so that I can use it to build up an Apache Derby database.
Whenever I use the "Database Export" functionality of SQL Developer 4.0.1.14, it generates the sql files, but they are in a format that Apache Derby cannot work with.
Does anybody know of any way to export the schema so that that format is compatible with Apache Derby?
Examples:
Apache Derby wants something like this:
CREATE TABLE "SURVEY"."LOAD_BALANCE"
( "LOAD_BALANCE_ID" NUMBER,
"SURVEY_ID" NUMBER,
"ROUTING_SERVICES" VARCHAR(255),
"ALGORITHM" VARCHAR(255),
"WEIGHT" VARCHAR(255),
"STICKINESS" VARCHAR(255),
"HEALTH_MONITOR" VARCHAR(255),
"SSL_USED" VARCHAR(255)
)
But SQL Developer outputs it like this:
CREATE TABLE "SURVEY"."LOAD_BALANCE"
( "LOAD_BALANCE_ID" NUMBER,
"SURVEY_ID" NUMBER,
"ROUTING_SERVICES" VARCHAR2(255 BYTE),
"ALGORITHM" VARCHAR2(255 BYTE),
"WEIGHT" VARCHAR2(255 BYTE),
"STICKINESS" VARCHAR2(255 BYTE),
"HEALTH_MONITOR" VARCHAR2(255 BYTE),
"SSL_USED" VARCHAR2(255 BYTE)
)
You might try DdlUtils: http://db.apache.org/ddlutils/
I know it supports Derby, and the project website says it supports Oracle, so ...