How to visualize CLOB content - Oracle - sql

I want to select and visualize the content of a column with CLOB datatype (>100 bytes).
select UTL_RAW.CAST_TO_VARCHAR2(DBMS_LOB.SUBSTR(ds_cirurgia,1,4000))
from AVISO_CIRURGIA
where cd_paciente = 123456789;
But I get this error:
[SELECT - 0 row(s), 0.000 secs] [Error Code: 997, SQL State: 42000] ORA-00997: illegal use of LONG datatype
I used UTL_RAW.CAST_TO_VARCHAR2(DBMS_LOB.SUBSTR()) in another column and it works.
What is wrong in this case?

You can just leave it with the call to DBMS_LOB.SUBSTR: it returns a varchar2:
select DBMS_LOB.SUBSTR(ds_cirurgia,1,4000)
from AVISO_CIRURGIA
where cd_paciente = 123456789
However, keep in mind that Oracle SQL is only capable of having a varchar2 with 4000 bytes, not 4000 characters. Your call can fail if the string contains Unicode characters.

Your column type is LONG not CLOB. Simple look up it in USER_TAB_COLUMNS.
Here are some workaround how to resolve it. I'd recoment to change the type to CLOB with CTAS.
create table t1 as
select ...
to_lob(c) c /* convert to CLOB */
from t;
After that you can simple cast to VARCHAR such as
cast (substr(col_name,1,4000) as varchar2(4000))
UPDATE
of course you may also use DBMS_LOB.SUBSTR
DBMS_LOB.SUBSTR(col_name,4000,1)
but please note the signature of this function: 2nd parameter is length, 3rd offset (not vice versa as in substr).

Related

Arithmatic overflow error converting varchar to data type numeric in sql server

I am trying to insert data from one table to another table, but we are facing issue it gives us error of
Numeric value out of range: 8115 [Microsoft][ODBC Driver 11 for SQL
Server][SQL Server ]Arithmetic overflow error converting varchar to
data type numeric. (SQLExecDirect[8115] at /builddir
/build/BUILD/php-5.6.30/ext/pdo_odbc/odbc_driver.c:247)
We are using below query to insert data
`INSERT INTO template_150 ([ClientName],[LOB],[PharmacyTotalClaimAmt])
SELECT PharmacyID,ProductIDNDC
, REPLACE(REPLACE(REPLACE(REPLACE(CONVERT(decimal(10,2),CONVERT(varchar(10),CONVERT(varchar(10),LEFT
(SUBSTRING(REPLACE(TRIM(IngredCost),'\',''), PATINDEX('%[0-9.-]%',REPLACE(TRIM(IngredCost),'\','')),
8000),PATINDEX('%[^0-9.-]%', SUBSTRING(REPLACE(TRIM(IngredCost),'\',''), PATINDEX('%[0-9.-]%',REPLACE
(TRIM(IngredCost),'\','')), 8000) + 'X') -1)) )
),'"',''),'$',''),',',''),'-','') AS [TRIM(IngredCost)]
FROM file_5979bd211a3a9`
I found some solution to use lenth WHERE LEN(IngredCost
)<=13 function in where condition, but if we will use this function then we will not able to insert all the records for the table file_5979bd211a3a9, we want to save all the records of table file_5979bd211a3a9, can anyone please give us solutio, how can we resolve this error ?
You're converting a 10 character number to a DECIMAL(10,2). That means up to 8 digits in the whole portion and 2 in the fractional. If there are more than 8 numbers in the whole portion of the number, you can't convert that to a DECIMAL(10,2).
For example:
select convert(decimal(10,2),'1000000000')
Try DECIMAL(12,2) or use a VARCHAR(8).

Unable to remove carriage returns in a LONG data type column using REPLACE function

I am working on a LONG column in Oracle SQL Developer and this column contains carriage returns that need to be removed. The error I'm getting after using :
REPLACE ( col_name , CHR(13) , '' ) is :
ORA-00932: inconsistent datatypes: expected CHAR got LONG
00932. 00000 - "inconsistent datatypes: expected %s got %s"
Is there a workaround for this ?
Answers or suggestions will be much appreciated!
You will not be able to do almost anything with LONG data type columns. You should convert them to CLOB. You just found a reason why that is.
https://docs.oracle.com/cd/B28359_01/appdev.111/b28393/adlob_long_lob.htm
There is a to_lob() function to convert LONG to CLOB, but that can only be used in the select portion of an insert statement (that is, it can only be used to convert a LONG column to a CLOB column). After the conversion, you should have no problems using text functions on the resulting CLOB. You may also want to look at CLOB-specific functions in the DBMS_LOB package:
http://docs.oracle.com/cd/E11882_01/appdev.112/e40758/d_lob.htm#ARPLS600

Conversion failed when converting the nvarchar value 'AAAR78509883' to data type int

I have a nvarchar column in one of my tables that I have imported from Access. I am trying to change to an int. To move to a new table.
The original query:
insert into members_exams_answer
select
ua.members_exams_id, ua.exams_questions_id,
ua.members_exams_answers_value, ua.members_exams_answers_timestamp
from
members_exams as me
full join
UserAnswers1 as ua on me.members_exams_username = ua.members_exams_id
full join
exams_questions as eq on eq.exams_questions_id = ua.exams_questions_id
This throws an error:
Conversion failed when converting the nvarchar value 'AAAR78509883' to data type int.
I have tired:
select convert (int, UserAnswers1.members_exams_id)
from UserAnswers1
and
select cast(members_exams_id as integer) int_members_exams_id
from UserAnswers1
and
select cast (members_exams_id as int)
from UserAnswers1
All result in the same error
Conversion failed when converting the nvarchar value 'AAAR78509883' to data type int.
Clearly you are trying to convert data that is alphanumeric to an int and that cannot be done.
Looking at your data why are you insisting on converting it to an int when it cannot be an int? Why not just process it as an nvarchar?
Your problem could be systemic where all data has a leading alpha characters that you need to strip out (and hopefully the same number of alpha characters)
In that case use a substring to strip off the alphas (this assumes the name number of alphabetic characters in each record). Or use a varchar or nvarchar field instead of an int. If the number of leading characters varies or if they can be leading or trailing or some other combination, it will much more complex to fix than we can probably describe on the Internet.
The other possibility is that you simply have some bad data. In which case identify the records which are not numeric and fix them or null the value out if they cannot be fixed. This happens frequently when you have stored the data in an incorrect datatype.

sql convert error on view tables

SELECT logicalTime, traceValue, unitType, entName
FROM vwSimProjAgentTrace
WHERE valueType = 10
AND agentName ='AtisMesafesi'
AND ( entName = 'Hawk-1')
AND simName IN ('TipSenaryo1_0')
AND logicalTime IN (
SELECT logicalTime
FROM vwSimProjAgentTrace
WHERE valueType = 10 AND agentName ='AtisIrtifasi'
AND ( entName = 'Hawk-1')
AND simName IN ('TipSenaryo1_0')
AND CONVERT(FLOAT , traceValue) > 123
) ORDER BY simName, logicalTime
This is my sql command and table is a view table...
each time i put "convert(float...) part " i get
Msg 8114, Level 16, State 5, Line 1
Error converting data type nvarchar to float.
this error...
One (or more) of the rows has data in the traceValue field that cannot be converted to a float.
Make sure you've used the right combination of dots and commas to signal floating point values, as well as making sure you don't have pure invalid data (text for instance) in that field.
You can try this SQL to find the invalid rows, but there might be cases it won't handle:
SELECT * FROM vwSimProjAgentTrace WHERE NOT ISNUMERIC(traceValue)
You can find the documentation of ISNUMERIC here.
If you look in BoL (books online) at the convert command, you see that a nvarchar conversion to float is an implicit conversion. This means that only "float"-able values can be converted into a float. So, every numeric value (that is within the float range) can be converted. A non-numeric value can not be converted, which is quite logical.
Probably you have some non numeric values in your column. You might see them when you run your query without the convert. Look for something like comma vs dot. In a test scenario a comma instead of a dot gave me some problems.
For an example of isnumeric, look at this sqlfiddle

Oracle VIEW - More than 4000 bytes in Column

Iam using this part of an SQL Satement to fetch Information from an N:N Relationship.
The Goal is to have an view with an column like: "STRING1,STRING2,STRING3". This works fine but i have sometimes more than 4000 Bytes in the Column.
(SELECT
(RTRIM(XMLAGG(xmlelement(X, TABLE1.STRING||',') order by TABLE1.STRING).extract('//text()'),','))
FROM
STRING_HAS_TABLE1
JOIN TABLE1 STRING_HAS_TABLE1.STRING_ID = TABLE1.ID
WHERE
STRING_HAS_TABLE1.USER_ID = X.ID) AS STRINGS,
Oracle throws "Buffer overflow". I think the problem is the columntype inside the view: VARCHAR2(4000).
ERROR: ORA 19011 - Character string buffer to small
Any ideas to handle this without changing the whole application logic?
This is a problem converting implicitly between data types. You can get around it by treating it as a CLOB before trimming, by adding a getClobVal() call:
SELECT RTRIM(XMLAGG(xmlelement(X, TABLE1.STRING||',')
order by TABLE1.STRING).extract('//text()').getClobVal(),',')
FROM ...
The RTRIM documentation shows the types it accepts, and since XMLTYPE isn't listed that means it has to be doing an implicit conversion, apparently to VARCHAR2. (The same applies to the other TRIM functions).
But it does accept CLOB, so doing an explicit conversion to CLOB means RTRIM doesn't do an implicit conversion to a type that's too small.