I am using Teradata, and have following code:
SELECT
...
CAST(t1.atr AS VARCHAR(30))
...
FROM (
SELECT
...
COALESCE(sum(t1.atr),0) AS atr,
...
) AS t1
The problem is, when the value of the 'atr' is either 0 or 0.xy (or starts with 0), the output is '.00' and '.xy' respectively.
I would like to have the following output: '0.00' and '0.xy' respectively.
Btw, I am doing export in csv format file in this step.
Did you try to specify a format for your CAST?
CAST(t1.atr AS VARCHAR(30) FORMAT 'ZZZZZZZZZ9.99')
The answer is:
CAST( CAST(t1.atr AS DECIMAL(13,4) FORMAT 'Z(9)9.99') AS VARCHAR(30))
Related
Attempting to cast a string (formatted as YYYY-MM-DD) as ISO date. It's already in the right format but it is erroring out.
SELECT
*
FROM TABLE1
WHERE CHAR(VARCHAR((SUBSTRING(PARAMETER_VALUE,8,4)||
CASE SUBSTRING(PARAMETER_VALUE,4,3)
WHEN 'Jan' THEN '-01-'
WHEN 'Feb' THEN '-02-'
WHEN 'Mar' THEN '-03-'
WHEN 'Apr' THEN '-04-'
WHEN 'May' THEN '-05-'
WHEN 'Jun' THEN '-06-'
WHEN 'Jul' THEN '-07-'
WHEN 'Aug' THEN '-08-'
WHEN 'Sep' THEN '-09-'
WHEN 'Oct' THEN '-10-'
WHEN 'Nov' THEN '-11-'
WHEN 'Dec' THEN '-12-' END||
SUBSTRING(PARAMETER_VALUE,1,2))
),ISO) > CURRENT DATE;
I receive the following error: The statement was not processed because the data type, length or value of the argument for the parameter in position "1" of routine "SYSIBM.CHAR" is incorrect. Parameter name: "".. SQLCODE=-171, SQLSTATE=42815, DRIVER=4.19.56
I am using IBM Data Studio to run this.
PARAMETER_VALUE looks like it's DDMonYYYY if you wanted to use TO_DATE similar to:
SELECT
*
FROM TABLE1
WHERE TO_DATE(PARAMETER_VALUE, 'DDMonYYYY') > CURRENT DATE;
If that's not the correct format of that column then you can put together the correct string from here: https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.5.0/com.ibm.db2.luw.sql.ref.doc/doc/r0007107.html
I'm using the following code to convert a string datetime variable to datetime, but the converted string is missing SSS part.
Code used:
cast(FROM_UNIXTIME(UNIX_TIMESTAMP(oldtime, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"),"yyyy-MM-dd HH:mm:ss.SSS") as timestamp) as newtime
The outcome:
2019-03-08T18:28:36.901Z is converted to 08MAR2019:18:28:36.000000
Some other oldtimes in string:
2020-03-09T16:05:06:827Z
2020-03-09T16:03:19:354Z
2020-03-11T16:03:57:280Z
2020-03-10T16:02:57:642Z
2020-03-10T16:04:07:455Z
2020-03-10T16:04:09:737Z
2020-03-10T16:03:57:280Z
2020-03-10T16:02:46:816Z
The SSS part '901' is missing in the converted time. Would like help on keeping the SSS part since I need to sort the records by their exact time.
Thank you!
from_unixtime is always until minutes(yyyy-MM-dd HH:mm:ss) to get millisecs we need to do some workarounds.
we will extract the millisecs from the old_time using regexp_extract then concat that to from_unixtime result and finally cast to timestamp.
Example:
select old_time,
timestamp(concat_ws(".", --concat_ws with . and cast
FROM_UNIXTIME(UNIX_TIMESTAMP(old_time, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"),"yyyy-MM-dd HH:mm:ss"), -- from_unixtime and unix_timestamp to convert without millisecs
regexp_extract(string(old_time),".+\\.(.*)(?i)z",1))) as newtime from --regexp_extract to extract last 3 digits before z then concat
(select string("2020-03-11T21:14:41.335Z")old_time)e
+------------------------+-----------------------+
|old_time |newtime |
+------------------------+-----------------------+
|2020-03-11T21:14:41.335Z|2020-03-11 21:14:41.335|
+------------------------+-----------------------+
UPDATE:
Your sample data have : before milliseconds, Try with below query:
select old_time,
timestamp(concat_ws(".", --concat_ws with . and cast
FROM_UNIXTIME(UNIX_TIMESTAMP(old_time, "yyyy-MM-dd'T'HH:mm:ss:SSS'Z'"),"yyyy-MM-dd HH:mm:ss"), -- from_unixtime and unix_timestamp to convert without millisecs
regexp_extract(string(old_time),".+\\:(.*)(?i)z",1))) as newtime from --regexp_extract to extract last 3 digits before z then concat
(select string("2020-03-11T21:14:41:335Z")old_time)e
Simply replace 'T' with space ' ' remove 'Z' and replace last ':' with dot, like this :
select regexp_replace('2020-03-09T16:05:06:827Z','(.*?)T(.*?):([^:]*?)Z$','$1 $2\\.$3');
Result:
2020-03-09 16:05:06.827
Read also this answer if you need to convert to different format, preserving milliseconds: https://stackoverflow.com/a/59645846/2700344
I'm willing to filter a table based on a column which has the data in format 'YYYY-MM-DD HH:MI:SS.s' as described in the question. Since this is not default timestamp format in teradata,so I tried to typecast.
So far I have tried following things :
where column_name > ${VAL1}(Timestamp(1),Format'YYYY-MM-DDbHH:MI:SS.s(1)') and column_name < ${VAL2}(Timestamp(1),Format'YYYY-MM-DDbHH:MI:SS.s(1)')
where column_name > ${VAL1}(Timestamp(0),Format'YYYY-MM-DDbHH:MI:SS.s(1)') and column_name < ${VAL2}(Timestamp(0),Format'YYYY-MM-DDbHH:MI:SS.s(1)')
where column_name > ${Val1}(Timestamp(1),Format'YYYY-MM-DDbHH:MI:SS.s(1)bt') and column_name < ${Val2}(Timestamp(1),Format'YYYY-MM-DDbHH:MI:SS.s(1)bt')
But everytime the error is same "Syntax error,expected something like
an 'OR' keyword or ')' between an integer and the integer '13'.
I believe the problem is in passing the format to teradata. Any help appreciated.
P.S: VAL1 and VAL2 are bash variables.
You pass the variables without quotes, e.g.
where column_name > 2019-03-04 12:34:56.7(Timestamp(1) ...
the parser treats 2019-03-04 as a calculation and stops at 13.
Try
where column_name > '${VAL1}'(Timestamp(1) ...
And because this format is Teradata's default instead of casting to a timestamp you better use a literal:
where column_name > TIMESTAMP '${VAL1}' and column_name < TIMESTAMP '${VAL2}'
Does anyone know how to prepend leading zeros to a column in sqlite?
I am getting columns like 1, 2 and for our data import they need to be 3 character codes with leading zeros.
I've tried this
update LockBox
set CustomField2 = case
when CustomField2 = '1' then '001'
end
WHERE CustomField2 = '1';
but does not give me the correct result. I'm probably doing this wrong. I'm more of a sql server guy not a sqlite person
I would do this in SQL Server
SELECT RIGHT('000'+CAST(field AS VARCHAR(3)),3)
How can I accomplish this in SQLite. I want to update the table then export to CSV to import into our business application.
Use substr:
update LockBox
set CustomField2 = substr('000' || CustomField2, -3, 3);
provided that all values in CustomField2 are no longer than 3 digits.
Define the column as text?
$ sqlite3 types
sqlite> create table T( i integer, c text );
sqlite> insert into T values ( '001', '001' );
sqlite> select * from T;
i c
---------- ----------
1 001
I have a column defined as Date format yyyy-mm-dd while creating.
I want to insert data from other table which has that column as varchar(50)
While selecting and inserting into the table I got this error
INSERT Failed. 2665: Invalid date.
Can someone help me in casting this?
INSERT INTO TEMP_TABLES.FACT
(
CUSTOMER_ACCOUNT_ID,
LOB_START_DATE,
)
SEL
CUSTOMER_ACCOUNT_ID,
I.start_date as LOB_START_DATE,
FROM #LOGIN I
left join JOURNEY_TABLE.DOTCOM_DIM d1
on I.PAGES = d1.PAGE_DESC
This is the example of date stored in varchar(50) field : 2014-04-03
Thanks in advance
In case it would be helpful, here's a query that should allow you to identify the rows with invalid dates:
select
*
from
#login t1
left outer join sys_calendar.calendar t2
on t1.start_date = cast (cast(t2.calendar_date as date format 'YYYY-MM-DD') as char(10))
where t2.calendar_date is null
Any rows that return from this query will have invalid dates.
I think all you need is the format statement
examples here
SEL
CUSTOMER_ACCOUNT_ID,
cast(((I.start_date (date, format 'yyyy-mm-dd'))(char(10))) as LOB_START_DATE,
In more recent versions of Teradata there is a TRYCAST() function. This function will attempt to cast the data and return NULL if the conversion fails, instead of failing the statement.
INSERT INTO TEMP_TABLES.FACT
(
CUSTOMER_ACCOUNT_ID,
LOB_START_DATE,
)
SELECT Customer_Account_ID
, TRYCAST(I.Start_Date AS DATE) AS LOB_START_DATE
FROM #LOGIN I
LEFT JOIN JOURNEY_TABLE.DOTCOM_DIM d1
ON I.Pages = d1.Page_Desc;