Conversion of decimal 5,0 into smallint in teradata sql assistant - teradata

I am trying to cast Decimal 5.0 into small integer.I am using following query but its not working.thanks in advance.
cast (a.mrktc_agen as SMALLINT) as mrktc_agen

"is not working" is not a very detailed error description :-)
There's probably a value outside of the valid range for a SMALLINT (-32768 to 32767) and you get a "Numeric overflow" error.

Related

Teradata: expected something like ',' between an integer and the word

I'm trying to create a table and I get the error. Could someone please let me know how to add a column which has an integer starting in its name. Find below the statement and error
Create table mutablecode
(
4th_Procedure_Code varchar(20)
);
Syntax error, expected something like ','
between an integer and the word 'th_Procedure_Code'
A valid object name in Teradata consists of a-z,A-Z,0-9,#,_,$ but must not start with a digit.
If you really need this column name you must double quote it (then almost any character is allowed):
"4th_Procedure_Code" varchar(20)
Remark: According to Standard SQL a double quoted name is case-sensitive, but in Teradata it's still case-insensitive.

insert statment make error

I am using oracle 12c with the username system. My problem is when I execute this insert statement that I took from oracle live sql site:
insert into emp
values(7788, 'SCOTT', 'ANALYST', 7566,to_date('13-JUL-87','dd-mm-rr') - 85,3000, null, 20);
it shows :
sql error ora-01858. 00000 - "a non-numeric character was found where a numeric was expected"
*Cause: The input data to be converted using a date format model was
incorrect. The input data did not contain a number where a number was
required by the format model.
*Action: Fix the input data or the date format model to make sure the
elements match in number and type. Then retry the operation.
what is this -85 after the to_date(..)
To handle dates, you would better use the ANSI format (date 'yyyy-mm-dd'):
insert into emp values(7788, 'SCOTT', 'ANALYST', 7566, date '1987-07-13'- 85,3000, null, 20);
If you need to use a to_date for some reason, you have to be sure that the format of your string exactly matches the format mask you use: if your month is written as 'JUL' you need 'MON' in the format mask and not 'mm'. 'mm' would match a month written as '07'.
Please notice that even with the right format mask, this way to write dates is dangerous, because it's based on the language of your DB.
The -85 means "subtract 85 days".

Datatype Mismatch in THEN/ELSE expression

I am trying to run below query snippet in a Teradata query
WHERE COALESCE(CAST (EXPC_DLVR_TS as date),'2020-12-31') >'2016-11-18'
I tried another but similar one
WHERE CAST(COALESCE(EXPC_DLVR_TS,'12/31/2020 17:00:00.000000-08:00') as date) >'2016-11-18'
For both the queries I am getting below error -
Datatype Mismatch in THEN/ELSE expression
You need to tell Teradata that '2020-12-31' is a date, otherwise it thinks it's a string. Just preface it by DATE. It's a good habit to always do that for dates.
where COALESCE(CAST (EXPC_DLVR_TS as date),date '2020-12-31') > date '2016-11-18'

How to check if decimal is valid value in Teradata

How to check if the given decimal is valid. I usually do a case statement like below to check if column is invalid or NULL then set it to 0 else take it as it is:
case when decimal_column is NULL or decimal_column NOT BETWEEN -999999999999 AND 999999999999 then 0 else decimal_column end
Can anyone please let me know if the above query looks correct
Thanks
In Teradata 14.10 or greater, the TO_NUMBER() function can be used.
SELECT TO_NUMBER({decimal column})
FROM {table};
If the conversion to number failed, TO_NUMBER() returns NULL. See the SQL Functions,Operators, Expressions and Predicates manual for more details.
You can also use trycast. Something like ..:
trycast(trim(col1)) as DECIMAL (XX,Y))
From TD-Doku: TRYCAST takes a string and tries to cast it to a data type specified after the AS keyword (similar to CAST). If the conversion fails, TRYCAST returns a NULL instead of failing.

Inserting a Datetime value into SQL Server CE in Webmatrix 3

I want to insert a datetime value into a database using SQL Server Compact Edition in Microsoft Webmatrix 3.
I tried the following query:
INSERT INTO Tutorials ([Tutorial], [StartDate])
VALUES ('3d', CONVERT(DATETIME, '07-23-08', 110));
And I got the following error message:
The conversion is not supported. [ Type to convert from (if known) = datetime, Type to convert to (if known) = float ]
Try with
INSERT INTO Tutorials ([Tutorial], [StartDate])
VALUES ('3d', CONVERT(DATETIME, '07-23-08', 10));
If you set the style value to 10 the input format must be mm-dd-yy, if you add 100 to the style value the expected format has a four digit year (110 --> mm-dd-yyyy).
For an exhaustive table of the style values look at CAST and CONVERT (SQL Server Compact).
By the way, you could take advantage of an implicit conversion using a date format like yyyymmdd or yyyy-mm-dd:
INSERT INTO Tutorials ([Tutorial], [StartDate])
VALUES ('3d', '20080723');

Resources