I have a varchar column with some data like: 0000000000,0000000123,0000000010,...
I want to cast this column into an integer, but I get the error it is not a number. So I thought I have to remove the left 0 of the varchar, but how do I do that?
I'm not sure what your problem is. Both these work when I try them:
select cast('0000000123' as int), to_number('0000000123')
from dual;
Here is a rextester illustrating that this works in Oracle.
use to_number function
select to_number('000001') from dual it will return 1
so your case
select to_number(column) from your_table
Related
SQL newbie here, but I can't find the solution to something that looks easy:
The following query does not seem to have a valid syntax (ORA-00904: invalid identifier), but its logic should be clear. How can I achieve this in a query that needs to be speedy?
SELECT * FROM table WHERE LEFT(column,4)="abcd"
For this purpose, you should use like rather than left(). First, Oracle doesn't support left() (you need substr() instead). Second, like can make use of indexes because the wildcard is not at the beginning of the string:
SELECT *
FROM table
WHERE column like 'abcd%';
Oracle and some other products have substr.
SELECT * FROM tablename WHERE substr(columnname, 1, 4) = 'abcd'
I.e. single quotes for string literals!
ANSI SQL has substring:
SELECT * FROM tablename WHERE substring(columnname from 1 for 4) = 'abcd'
And others have left:
SELECT * FROM tablename WHERE LEFT(columnname,4) = 'abcd'
I have an issue whith this query which doesn't work :
select count(*)
from MYTABLE where
MYFIELD in (select trim(cast(CLOBFIELD as varchar(20000))) from TABLE2) ;
=>0 rows
The subquery return right results :
select trim(cast(CLOBFIELD as varchar(20000))) from TABLE2 ;
=>1202,1203,1205,1206,1207,1208,1209,1210,1211,1212,1213,1214,1215,1216,1217,1218,1226
This query which doesn't have the subquery returns right reusults.
select count(*)
from MYTABLE where
MYFIELD in (1202,1203,1205,1206,1207,1208,1209,1210,1211,1212,1213,1214,1215,1216,1217,1218,1226) ;
The column CLOBFIELD is a CLOB field VS the column is a char(4) field.
In my opinion, this a cast issue in the subquery because of the cast on the clob field. I don't know what's wrong, I am not very familar with DB2, does anybody can help me ?
As comments above say, it isn't right to mess with casting large datatypes like this, but here is some code to try:
select count(*)
from MYTABLE
where LOCATE_IN_STRING((select ','||trim(cast(CLOBFIELD as varchar(20000)))||',' from TABLE2), ','||trim(MYFIELD)||',' ) > 0 ;
Can have problems if your values contain commas though.
Perhaps you could investigate applying a text search index? http://pic.dhe.ibm.com/infocenter/db2luw/v9r7/index.jsp?topic=%2Fcom.ibm.db2.luw.admin.ts.doc%2Fdoc%2Ft_creatingafulltextindex.html
However, if you need to access the values in the CLOB, perhaps they shouldn't be in a CLOB?
SELECT test_column FROM test_table ORDER BY test_column gives me this:
1
12
123
2
3
Why not:
1
2
3
12
123
How can I sort strings like numbers?
Try
SELECT test_column
FROM test_table
ORDER BY cast(test_column as int)
But you should look into changing the column types to the correct ones.
This worked for me:
ORDER BY cast(test_column as SIGNED)
here, cast function convert value from string to integer(SIGNED) then it applied ORDER BY. https://dev.mysql.com/doc/refman/8.0/en/cast-functions.html
The sort is working. It's a lexicographic sort (alphabetical). It appears that that column has a text (char, varchar, ...) type, so the ordering you'll get is textual and not numeric.
If you want a numerical sort, use a numeric column type (e.g. int). (Or cast the column appropriately.)
Check if the type of the column is varchar or something similar. It looks like it is being ordered by string value, not by numeric value. If the column only contains numbers it should better be of type int.
I think , we need to do the cast as part of the select statement , because if we use distinct then casting in the order by statement does not work:
SELECT cast(test_column as int) as test_column
FROM test_table
ORDER BY test_column
and
SELECT DISTINCT cast(test_column as int) as test_column
FROM test_table
ORDER BY test_column
This may be simple, but I am no SQL whiz so I am getting lost. I understand that sql takes your query and executes it in a certain order, which I believe is why this query does not work:
select * from purchaseorders
where IsNumeric(purchase_order_number) = 1
and cast(purchase_order_number as int) >= 7
MOST of the purchar_order_number fields are numeric, but we introduce alphanumeric ones recently. The data I am trying to get is to see if '7' is greater than the highest numeric purchase_order_number.
The Numeric() function filters out the alphanumeric fields fine, but doing the subsequent cast comparison throws this error:
Conversion failed when converting the nvarchar value '124-4356AB' to data type int.
I am not asking what the error means, that is obvious. I am asking if there is a way to accomplish what I want in a single query, preferably in the where clause due to ORM constraints.
does this work for you?
select * from purchaseorders
where (case when IsNumeric(purchase_order_number) = 1
then cast(purchase_order_number as int)
else 0 end) >= 7
You can do a select with a subselect
select * from (
select * from purchaseorders
where IsNumeric(purchase_order_number) = 1) as correct_orders
where cast(purchase_order_number as int) >= 7
try this:
select * from purchaseorders
where try_cast(purchase_order_number as int) >= 7
have to check which column has numeric values only.
Currently, in a table every field is setted with nvarchar(max) Like tableName (field1 nvarchar(max),field2 nvarchar(max),field3 nvarchar(3)) and tableName has 25lac Rows.
But on manually Check Field2 Contain the numeric Values Only... How to Check With t-sql that in the Complete Column (Field2) has numeric Value or not/null value with Longest Length in the Column!
There is a VARCHAR2 type column in my database table for store invoice_number. and there is a query to get max invoice_number as below.
select max(invoice_number) from invoice;
but there are no data in invoice table, above query return value as null. but in this case i need to replace this value to 0 instead of null. how could i do this ?
Using NVL:
SELECT NVL(MAX(invoice_number), '0')
Using the ANSI COALESCE:
SELECT COALESCE(MAX(invoice_number), '0')
Using DECODE:
SELECT DECODE(MAX(invoice_number), NULL, '0')
Using the ANSI CASE:
SELECT CASE
WHEN MAX(invoice_number) IS NULL THEN '0'
ELSE MAX(invoice_number)
END
Verdict
All work, I'd probably use NVL because COALESCE is ANSI but not known to necessarily work as fast as native alternatives.
You could use COALESCE
For instance:
select COALESCE(max(invoice_number),'0') from invoice;
select coalesce(max(invoice_number),0)
can also use Decode.
select decode(max(invoice_number),null, 0)