I tried to insert values into SURVEY.YSG table ( used in the insert query )
INSERT INTO SURVEY.YSG(YSG_ID,YSG_MSN_DTM,TAX_ID,TAX_NM,TYPE_CD,USER_ID,
FRST_NM,LAST_NM,SHL_TXT,NPR_DT, PHONE_NBR,FAX_NBR,
EMAIL_ADRS_TXT,YSG_IMD,MDFD_ID,YSG_VRFCTN_CD,
YSG_VRFCTN_DTM,LOG_KEY,PLS_DTM,LOAD_KEY,UPDTD_LOAD_KEY )
VALUES ( '103','2011-08-11 13:34:36.000000','656002075',
'STG HEALTH SYSTEM','null','OPLINKS4','UNK','UNK','UNK',
'12/31/8888','UNK','UNK','UNK','X','UNK','Y','8888-12-31 00:00:00.000000',
0,'2011-07-20 12:00:00.000000',0,0)
Error report:
SQL Error: ORA-01843: not a valid month
01843. 00000 - "not a valid month"
*Cause:
*Action:
Below are the 4 columns created with date and timestamp in SURVEY.YSG table.
YSG_MSN_DTM - TIMESTAMP(6)
NPR_DT - DATE
YSG_VRFCTN_DTM - TIMESTAMP(6)
PLS_DTM - TIMESTAMP(6)
tried with TO_DATE() function:
TO_DATE('2011-08-11 13:34:36','YYYY-MM-DD HH24:MI:SS')
Got output like: 11-AUG-11 01.34.36.000000000 PM.
Expected Output:
column: YSG_MSN_DTM -> 2011-08-11 13:34:36.000000
and for column NPR_DT ->12/31/8888 in another column.
is there any other way to achieve this in Oracle?
Any of your help is appreciated.
Thanks
Try this:
INSERT INTO
SURVEY.YSG ( YSG_ID,
YSG_MSN_DTM,
TAX_ID,
TAX_NM,
TYPE_CD,
USER_ID,
FRST_NM,
LAST_NM,
SHL_TXT,
NPR_DT,
PHONE_NBR,
FAX_NBR,
EMAIL_ADRS_TXT,
YSG_IMD,
MDFD_ID,
YSG_VRFCTN_CD,
YSG_VRFCTN_DTM,
LOG_KEY,
PLS_DTM,
LOAD_KEY,
UPDTD_LOAD_KEY )
VALUES
( '103',
TO_TIMESTAMP ( '2011-08-11 13:34:36.000000',
'YYYY-MM-DD HH24:MI:SS.FF' ),
'656002075',
'STG HEALTH SYSTEM',
'null',
'OPLINKS4',
'UNK',
'UNK',
'UNK',
TO_DATE ( '12/31/8888',
'MM/DD/YYYY' ),
'UNK',
'UNK',
'UNK',
'X',
'UNK',
'Y',
TO_TIMESTAMP ( '8888-12-31 00:00:00.000000',
'YYYY-MM-DD HH24:MI:SS.FF' ),
0,
TO_TIMESTAMP ( '2011-07-20 12:00:00.000000',
'YYYY-MM-DD HH24:MI:SS.FF' ),
0,
0 );
Related
I have a table 4 columns - Code, Status, EffectiveDate (EFF_DT), EndDate (END_DT). All the columns are Varchar2 type. EFF_DT and END_DT has ISO format date (YYYY-MM-DD) with NULL values for few rows. Need to get the rows which has END_DT greater than today's date.
While executing the below query, it returns all the Not NULL rows for END_DT. Do not compare the END_DT at all.
select code, status, EFF_DT, END_DT
from (
select CODE, EFF_DT, Status,to_date("END_DT" ,'YYYY-MM-DD' ) as END_DT
from xxx.ZZZ
) TAB
where to_date(TAB.END_DT ,'DD-MM-YY' ) > to_date(CAST(CURRENT_TIMESTAMP as Date), 'DD-MM-YY')
ORDER BY 1 ASC
But the below query compares the END_DT and returns the result properly -
SELECT "TAB"."CODE" , "TAB"."STATUS" AS "STATUS" , "TAB"."EFF_DT" , "TAB"."END_DT"
FROM xxx.ZZZ "TAB"
WHERE ( (to_date("TAB"."END_DT",'YYYY-MM-DD') > to_date(CAST(CURRENT_TIMESTAMP as Date), 'YY-MM-DD')) )
ORDER BY 1 ASC
What is going wrong with the 1st query?
I see difference in return value of END_DT.
For the 1st query, the data is coming like -
while as for the 2nd query, the data is coming like
I want to export the column names into excel sheet by running the query in Teradata. I used ctrl+c but it didnt work. Thanks in advance.
Change your settings in Result Set View Preferences\Copy Options\
check "Copy Include Column Headers"
The "Result Set View Preferences" is the first button that looks like pliers on the result set view window.
To get column names, open you answerset, and save the result set.
File > save as >
I've found the answer show table tablename
I frequently want to report out the colums and the dbc.columns is the best way to do this. They all come out right padded so a trim makes them paste into excel nicely. I also added a case statement that will translate the datatype for you.
sel
trim(databasename)
, trim(tablename)
, trim(columnname)
, max(case
when columntype = 'D' then 'decimal(' || decimaltotaldigits || ', ' || decimalfractionaldigits || ')'
when columntype = 'CV' then 'varchar(' || columnlength || ')'
when columntype = 'CF' then 'char(' || columnlength || ')'
when columntype like 'I%' then 'integer'
else 'unknown' end) as colDDL
from dbc.columns where tablename in (<sometableList>)
and databasename in (<someDBList>)
order by column_id
A Full List of DBC.columns data tpe mapping example:
create volatile table vt_woe_col_list
as (
select
trim(columnname) as column_name
, case when ColumnType in ('CF','CV') then 'Character'
when ColumnType in ('D','F','I1','I2','I') then 'Numeric'
when ColumnType in ('DA') then 'Date'
when ColumnType in ('SZ','TS') then 'TimeStamp'
else 'Skip' end as process_type
, case trim(columntype)
when 'AT' then 'TIME'
when 'BF' then 'BYTE'
when 'BO' then 'BLOB'
when 'BV' then 'VARBYTE'
when 'CF' then 'CHAR'
when 'CO' then 'CLOB'
when 'CV' then 'VARCHAR'
when 'D' then 'DECIMAL'
when 'DA' then 'DATE'
when 'DH' then 'INTERVAL DAY TO HOUR'
when 'DM' then 'INTERVAL DAY TO MINUTE'
when 'DS' then 'INTERVAL DAY TO SECOND'
when 'DY' then 'INTERVAL DAY'
when 'F' then 'FLOAT'
when 'GF' then 'GRAPHIC'
when 'GV' then 'VARGRAPHIC'
when 'HM' then 'INTERVAL HOUR TO MINUTE'
when 'HR' then 'INTERVAL HOUR'
when 'HS' then 'INTERVAL HOUR TO SECOND'
when 'I1' then 'BYTEINT'
when 'I2' then 'SMALLINT'
when 'I' then 'INTEGER'
when 'MI' then 'INTERVAL MINUTE'
when 'MO' then 'INTERVAL MONTH'
when 'MS' then 'INTERVAL MINUTE TO SECOND'
when 'SC' then 'INTERVAL SECOND'
when 'SZ' then 'TIMESTAMP WITH TIME ZONE'
when 'TS' then 'TIMESTAMP'
when 'TZ' then 'TIME WITH TIME ZONE'
when 'YM' then 'INTERVAL YEARTO MONTH'
when 'YR' then 'INTERVAL YEAR'
when 'UT' then 'UDT Type'
end as column_type_desc
, a.*
from dbc.columns A
where trim(tablename )='t_woe_data_samp'
and trim(databasename)= 'DUCSMAD'
) with data
primary index(column_name)
on commit preserve rows;
My requirement is need to convert the input date values which is in yyyy-MM-dd (user input value) format to GMT and we have our own timezone table which stores all the timezones values. So we need to convert input value(yyyy-MM-dd) to GMT as we are storing the values in GMT only. I have tried with FROM_TZ, CAST functions but getting timezone region not found issue.
Can anyone please suggest the changes accordingly.
Here attaching the some sample code (this is user input value).
start_time >= to_date(to_char((from_tz(to_timestamp('2017-09-01', 'yyyy-MM-dd HH:MI:SS AM') , tz.offSet) at time zone 'GMT'),'MM/dd/yyyy HH:MI AM'),'MM/dd/yyyy HH:MI AM')
Thanks,
Sant
I did this in my case :
-- returns a date value of a date converted in UTC and formated
cst_timestamp_fmt constant varchar2(20) := 'DD-MON-YY HH24:MI:SS';
function fn_Date_To_UTC_Formated(p_date date) return date is
begin
return to_date(to_char(
to_timestamp(
sys_extract_utc(
to_timestamp(
to_char(p_date,
cst_timestamp_fmt
),
cst_timestamp_fmt
)
)
),
'YYYY-MM-DD"T"HH24:MI:SS"Z"'
),
'YYYY-MM-DD"T"HH24:MI:SS"Z"'
);
end fn_Date_To_UTC_Formated;
and to test it:
declare
cst_timestamp_fmt constant varchar2(20) := 'DD-MON-YY HH24:MI:SS';
function fn_Date_To_UTC_Formated(p_date date) return date is
begin
return to_date(to_char(
to_timestamp(
sys_extract_utc(
to_timestamp(
to_char(p_date,
cst_timestamp_fmt
),
cst_timestamp_fmt
)
)
),
'YYYY-MM-DD"T"HH24:MI:SS"Z"'),'YYYY-MM-DD"T"HH24:MI:SS"Z"'
);
end fn_Date_To_UTC_Formated;
begin
dbms_output.put_line(' sysdate : '||to_char(sysdate, cst_timestamp_fmt));
dbms_output.put_line(' result : '||to_char(fn_Date_To_UTC_Formated(sysdate),cst_timestamp_fmt));
end;
result is
sysdate : 20-SEP-17 15:20:09
result : 20-SEP-17 13:20:09
hope that can help.
When I try to insert datetime value 12:58 AM into the oracle table it gets inserted as 00:58. How can I insert datetime value as 12 in my oracle db? I've set my Oracle time format as 24 hr time. Any suggestions would help.
Insert statement :
INSERT INTO TABLE
(
DATE_CREATED,
PLANNED_START,
PLANNED_COMPLETION
)
VALUES
(
sysdate,
TO_CHAR(p_planned_Start_Date, 'DD-MM-YYYY HH24:MI:SS'),
TO_CHAR(end_date_, 'DD-MM-YYYY HH24:MI:SS')
);
For the 24-hour time, you need to use HH24 instead of HH.
For the 12-hour time, the AM/PM indicator is written as A.M. (if you want periods in the result) or AM (if you don't). For example:
INSERT INTO TEST (LD_DATE) Values (TO_DATE('08/30/2016', 'MM/DD/YYYY '));
And select it as below:
SELECT LD_DATE,
TO_CHAR(LD_DATE, 'DD-MM-YYYY HH24:MI:SS') "Date 24Hr",
TO_CHAR(LD_DATE, 'DD-MM-YYYY HH:MI:SS AM') "Date 12Hr"
FROM test
;
I want to fetch few data that occur in particular month. For example, I need to choose all the names of employees who joined in July(irrespective of date). What is the query to choose particular month field alone from date field from database ? How do I compare the month field in database(stored in date of format mm/dd/yyyy) and the user given value of month. I'm using sqlite3 database and date field is set to text.
Thanks in advance.
SQLite only has a small set of date and time functions. You can use them like this:
sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE t1 (f1 string);
INSERT INTO "t1" VALUES('03/31/1970');
COMMIT;
sqlite> select substr(f1, 7) || '-' || substr(f1, 0, 3) || '-' || substr(f1, 4, 2) from t1;
1970-03-31
sqlite> select strftime("%m", substr(f1, 7) || '-' || substr(f1, 0, 3) || '-' || substr(f1, 4, 2)) from t1;
03
I Resolving this using this code in SQLITE.
If You have dat like this format : "2021-12-23 12:33:01"
then convert it in strftime() format function.
ex.-> Select strftime('%d/%m/%Y',EntryDate) as Date from table_name;
then output comes in
"2021-12-23 12:33:01" to "23-12-2021"
and then fire this query to get MONTH NAME from MONTH NUMBER in SQLITE
-> using case we can fetch it.
Select strftime('%d/%m/%Y',checkInDate) as Date,
case strftime('%m', checkInDate) when '01' then 'JAN'
when '02' then 'FEB' when '03' then 'MAR' when '04' then 'APR' when '05' then 'MAY' when '06' then 'JUN'
when '07' then 'JUL' when '08' then 'AUG' when '09' then 'SEP' when '10' then 'OCT'
when '11' then 'NOV' when '12' then 'DEC' else '' end as Month from AttendanceTable
☻♥ Done Keep Code.