How to copy column names from Teradata - teradata

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;

Related

Convert TimeStamp to Teradata TimeStamp

I have a table which contains DateTime in the following format
'MM/DD/YYYY HH:MI'
now I am trying to convert this time into Teradata Timestamp but getting an error.
What I have tried is:
select cast('4/13/2022 0:00' AS TIMESTAMP(0) Format 'mm/dd/yyyyBhh:mi')
but it return invalid timestamp error.
Any method for this?
Best regards.
You've got single digit month and hour values in there. I don't think there's any way to make Teradata play nice with those, other than adding the leading 0s.
I think this should work but you'll want to test it thoroughly.
select cast (regexp_replace('4/13/2022 0:00', '\b([0-9])\b', '0\1') AS TIMESTAMP(0) Format 'mm/dd/yyyyBhh:mi')
after different solutions, this worked.
CASE WHEN CHAR_LENGTH( STRTOK(ACT_TIME, '/', 1)) = 1 THEN TO_TIMESTAMP( '0' || ACT_TIME, 'MM/DD/YYYY HH24:MI') END ACT_TIME_TIMESTAMP
,CASE WHEN CHAR_LENGTH( STRTOK(ACT_TIME, '/', 1)) = 1 THEN CAST(TO_TIMESTAMP( '0' || ACT_TIME, 'MM/DD/YYYY HH24:MI') AS DATE) END ACT_TIME_DATE
,CASE WHEN CHAR_LENGTH( STRTOK(ACT_TIME, '/', 1)) = 1 THEN CAST(TO_TIMESTAMP( '0' || ACT_TIME, 'MM/DD/YYYY HH24:MI') AS TIME) END ACT_TIME_DATE

SQL: Sum based on specified dates

Thanks again for the help everyone. I went with the script below...
SELECT beginning, end,
(SELECT SUM(sale) FROM sales_log WHERE date BETWEEN beginning AND `end` ) AS sales
FROM performance
and I added a salesperson column to both the performance table and sales_log but it winds up crashing DB Browser. What is the issue here? New code below:
SELECT beginning, end, salesperson
(SELECT SUM(sale) FROM sales_log WHERE (date BETWEEN beginning AND end) AND sales_log.salesperson = performance.salesperson ) AS sales
FROM performance
I believe that the following may do what you wish or be the basis for what you wish.
WITH sales_log_cte AS
(
SELECT substr(date,(length(date) -3),4)||'-'||
CASE WHEN length(replace(substr(date,instr(date,'/')+1,2),'/','')) < 2 THEN '0' ELSE '' END
||replace(substr(date,instr(date,'/')+1,2),'/','')||'-'||
CASE WHEN length(substr(date,1,instr(date,'/') -1)) < 2 THEN '0' ELSE '' END||substr(date,1,instr(date,'/') -1) AS date,
CAST(sale AS REAL) AS sale
FROM sales_log
),
performance_cte AS
(
SELECT substr(beginning,(length(beginning) -3),4)||'-'||
CASE WHEN length(replace(substr(beginning,instr(beginning,'/')+1,2),'/','')) < 2 THEN '0' ELSE '' END
||replace(substr(beginning,instr(beginning,'/')+1,2),'/','')||'-'||
CASE WHEN length(substr(beginning,1,instr(beginning,'/') -1)) < 2 THEN '0' ELSE '' END||substr(beginning,1,instr(beginning,'/') -1)
AS beginning,
substr(`end`,(length(`end`) -3),4)||'-'||
CASE WHEN length(replace(substr(`end`,instr(`end`,'/')+1,2),'/','')) < 2 THEN '0' ELSE '' END
||replace(substr(`end`,instr(`end`,'/')+1,2),'/','')||'-'||
CASE WHEN length(substr(`end`,1,instr(`end`,'/') -1)) < 2 THEN '0' ELSE '' END||substr(`end`,1,instr(`end`,'/') -1)
AS `end`
FROM performance
)
SELECT beginning, `end` , (SELECT SUM(sale) FROM sales_log_cte WHERE date BETWEEN beginning AND `end` ) AS sales
FROM performance_cte
;
From your data this results in :-
As can be seen the bulk of the code is converting the dates into a format (i.e. YYYY-MM-DD) that is usable/recognisable by SQLite for the BETWEEN clause.
Date And Time Functions
I don't believe that you want a join between performance (preformance_cte after reformatting the dates) and sales_log (sales_log_cte) as this will be a cartesian product and then sum will sum all the results within the range.
The use of end as a column name is also awkward as it is a KEYWORD requiring it to be enclosed (` grave accents used in the above).
The above works by using 2 CTE's (Common Table Expresssions), which are temporary tables who'd life time is for the query in which they are used.
The first sales_log_cte is simply the sales_log table but with the date reformatted. The second, likewise, is simply the performace table with the dates reformatted.
If the tables already has suitable date formatting then all of the above could simply be :-
SELECT beginning, `end` , (SELECT SUM(sale) FROM sales_log WHERE date BETWEEN beginning AND `end` ) AS sales FROM performance;

Sqlite sum time

I have a table with a column that contained time duration of events.
It it formatted as 'h:mm:ss'
I found the function strftime - but according to the manual, it requires the format 'hh:mm:ss'
can someone tell me how i can sum up the duration without recreating the sql table?
Is this what you want ?
with t as (
select '4:02:01' as v
union all
select '9:30:12'
union all
select '2:14:00'
),
diff as (
select sum(strftime('%s', '0'||v) - strftime('%s', '00:00:00')) as v
from t
)
select (v/3600) || ' hours, ' || (v%3600/60) ||' minutes, '
|| (v%60) || ' seconds.'
from diff
https://www.db-fiddle.com/f/2SNbFYQv2zYiCE4gw5bhzi/0
You can use time() and strftime():
select
time(sum(
strftime('%s', case length(timecolumn) when 7 then '0' else '' end || timecolumn)
- strftime('%s','00:00:00')),
'unixepoch') totaltime
from tablename
The result time sum will be in format hh:mm:ss.

how to extract only month from date which is in sqlite database?

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.

Column exists but still get this error ORA-00904: invalid identifier

Anyone can help me with this issue
declare
lv2_sql VARCHAR2(32767);
cursor c_scv is
select financial_code, object_id, daytime from stream_category_version;
begin
for r_scv in c_scv LOOP
IF r_scv.financial_code = 'PURCHASE' THEN
lv2_sql := 'UPDATE stream_category_version ' || CHR(10) ||
'set REVN_PURCHASES_IND = ''Y'', last_updated_by = nvl(last_updated_by, created_by) ' || CHR(10) ||
'WHERE object_id = r_scv.object_id AND daytime = r_scv.daytime';
ecdp_dynsql.execute_statement(lv2_sql);
ELSIF r_scv.financial_code = 'SALE' THEN
lv2_sql := 'UPDATE stream_category_version ' || CHR(10) ||
'set REVN_SALES_IND = ''Y'', last_updated_by = nvl(last_updated_by, created_by) ' || CHR(10) ||
'WHERE object_id = r_scv.object_id AND daytime = r_scv.daytime';
ecdp_dynsql.execute_statement(lv2_sql);
END IF;
END LOOP;
end;
I have code as shown above, but i got error saying 'ORA-00904: R_SCV.DAYTIME: invalid identifier'. I have checked the table definition for 'stream_category_version' and found the column DAYTIME as shown below
SQL> desc stream_category_version
Name Type Nullable Default Comments
------------------ -------------- -------- ------- --------
OBJECT_ID VARCHAR2(32)
DAYTIME DATE
END_DATE DATE Y
NAME VARCHAR2(240) Y
FINANCIAL_CODE VARCHAR2(32) Y
SORT_ORDER NUMBER Y
COMMENTS VARCHAR2(2000) Y
Then i am confused with the error. Anyone can help me ?
Thanks in advance.
Shortly speaking - Oracle is case sensitive...
... probably during table creation column was typed UPPERCASE in quotation marks like that:
"DAYTIME"
and in your sql i see this column in lowercase
so you should verify your column name and best change it to version without quotation marks.
Other option is to call this column like that:
= r_scv.DAYTIME

Resources