Hql how to cast number to varchar2
Hello.
How to cast number to varchar2(to string)? I'm using hql toplink implementation and (don't know why) functions like cast(... as ...), to_char(...) and str(...) don't work. Please help me.
Kuba :).
Edited by: 854998 on 2011-04-27 03:12
Hello Chris.
First of all I'd like to thank you for answer. :)
1)I can't use native query.
2)What do you mean?
3)I can't upgrade.
I have something like this(it's only simple example, I have more complex query):
getManager().createQuery("SELECT obj FROM " + EntityClass.class.getSimpleName() + " obj "
+ " WHERE '14' = obj.number").getResultList();
r.number is Number in Oracle and Long in entity class.
I want to cast obj.number to String.
Here: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-expressions expressions like cast is valid.
So, where's the problem? My toplink version is too old?
Thanks a lot
Kuba.
Similar Messages
-
Conversion/Type Cast of sys.XMLType to NUMBER and VARCHAR2
How do we typecast or convert a variable of the type sys.XMLType to NUMBER and VARCHAR2?
For e.g. I have a variable :-
v_STATE sys.XMLType
I want to convert it to either of the type NUMBER or VARCHAR2.How do we typecast or convert a variable of the type sys.XMLType to NUMBER and VARCHAR2?
Your question is too vague to give a relevant answer.
Typecasting doesn't make sense in this situation, XMLType is an opaque datatype, it's not like converting VARCHAR2 to NUMBER (or vice versa).
Do you mean serializing the XML document, or extracting atomic node values, or something else ?
Depending on your requirement, you may want to look for XMLSerialize() or XMLCast() functions.
And on a side note, don't always expect people to search for your previous threads to find some useful information, such as a db version... -
How to find number of files in a folder using pl/sql
please someone guide as to how to find number of files in a folder using pl/sql
RegardsThe Java option works well.
-- results table that will contain a file list result
create global temporary table directory_list
directory varchar2(1000),
filename varchar2(1000)
on commit preserve rows
-- allowing public access to this temp table
grant select, update, insert, delete on directory_list to public;
create or replace public synonym directory_list for directory_list;
-- creating the java proc that does the file listing
create or replace and compile java source named "ListFiles" as
import java.io.*;
import java.sql.*;
public class ListFiles
public static void getList(String directory, String filter)
throws SQLException
File path = new File( directory );
final String ExpressionFilter = filter;
FilenameFilter fileFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
if(name.equalsIgnoreCase(ExpressionFilter))
return true;
if(name.matches("." + ExpressionFilter))
return true;
return false;
String[] list = path.list(fileFilter);
String element;
for(int i = 0; i < list.length; i++)
element = list;
#sql {
insert
into directory_list
( directory, filename )
values
( :directory, :element )
-- creating the PL/SQL wrapper for the java proc
create or replace procedure ListFiles( cDirectory in varchar2, cFilter in varchar2 )
as language java
name 'ListFiles.getList( java.lang.String, java.lang.String )';
-- punching a hole in the Java VM that allows access to the server's file
-- systems from inside the Oracle JVM (these also allows executing command
-- line and external programs)
-- NOTE: this hole MUST be secured using proper Oracle security (e.g. AUTHID
-- DEFINER PL/SQL code that is trusted)
declare
SCHEMA varchar2(30) := USER;
begin
dbms_java.grant_permission(
SCHEMA,
'SYS:java.io.FilePermission',
'<<ALL FILES>>',
'execute, read, write, delete'
dbms_java.grant_permission(
SCHEMA,
'SYS:java.lang.RuntimePermission',
'writeFileDescriptor',
dbms_java.grant_permission(
SCHEMA,
'SYS:java.lang.RuntimePermission',
'readFileDescriptor',
commit;
end;
To use:
SQL> exec ListFiles('/tmp', '*.log' );
PL/SQL procedure successfully completed.
SQL> select * from directory_list;
DIRECTORY FILENAME
/tmp X11_newfonts.log
/tmp ipv6agt.crashlog
/tmp dtappint.log
/tmp Core.sd-log
/tmp core_intg.sd-log
/tmp da.sd-log
/tmp dhcpclient.log
/tmp oracle8.sd-log
/tmp cc.sd-log
/tmp oms.log
/tmp OmniBack.sd-log
/tmp DPISInstall.sd-log
12 rows selected.
SQL> -
How to convert clob to varchar2 in an update trigger
Hi,
I need to convert a field (clob) to varchar2 in a update trigger, how can I do that?
regards.The maximum amount of data you can store in CLOB column is 4GB.
You have table TABLE1 having col1 CLOB datatype.You have one more table
Table_audit_1 without having CLOB datatype column and want to move CLOB
data into VARCHAR2 column which has maximum length of 4000 and want to
audit col1 CLOB into VARCHAR2.It seems to me you dont care about the data
beyond 4000 bytes.
In 9i SUBSTR, INSTR can be performed without reference to the DBMS_LOB
package.
SQL> DROP TABLE mytable
2 /
Table dropped.
SQL> DROP TABLE mytable1
2 /
Table dropped.
SQL> CREATE TABLE mytable
2 (a NUMBER,b CLOB)
3 /
Table created.
SQL> CREATE TABLE mytable1
2 (c NUMBER,d VARCHAR2(4000))
3 /
Table created.
SQL> DECLARE
2 loc CLOB;
3 BEGIN
4 FOR i IN 1..5000
5 LOOP
6 loc:=loc||i;
7 END LOOP;
8 INSERT INTO mytable (a,b) VALUES (1,loc);
9 INSERT INTO mytable1 (c,d) VALUES (1,SUBSTR(loc,1,4000));
10 COMMIT;
11 END;
12 /
PL/SQL procedure successfully completed.
SQL> SET LONG 5000
SQL> CREATE OR REPLACE TRIGGER mytrigger BEFORE UPDATE ON mytable FOR EACH ROW
2 BEGIN
3 UPDATE mytable1
4 SET d=SUBSTR(:NEW.b,1,4000)
5 WHERE c=:NEW.a;
6 END;
7 /
Trigger created.
SQL> DECLARE
2 loc CLOB;
3 BEGIN
4 FOR i IN 1..1000
5 LOOP
6 loc:=loc||'A '||i;
7 END LOOP;
8 UPDATE mytable
9 SET b=loc
10 WHERE a=1;
11 COMMIT;
12 END;
13 /
PL/SQL procedure successfully completed.
SQL> SELECT LENGTH(b) FROM mytable
2 /
LENGTH(B)
4893
SQL> SELECT LENGTH(d) FROM mytable1
2 /
LENGTH(D)
4000
SQL> Khurram -
How to cast RECORD of nested tables into OBJECT of nested tables
Right, we have an existing massive pl/sql package where some of the processing is taking too long so they want to try multithreading it.
The data in this package is stored in an array of records which contains nested tables, which themselves contain nested tables.
So, we want to split this table into 10, and submit them to 10 dbms_jobs to run concurrently, write the modified arrays to the database so they can be picked up again by the original process.
I'm stuck on converting the associative array of data (containing tables of records) into objects which can be stored in the DB.
My database objects:
CREATE OR REPLACE
TYPE ktest_claims_rt IS OBJECT
col1 varchar2(10)
,col2 varchar2(10));
CREATE OR REPLACE
TYPE ktest_claims_tt IS TABLE OF ktest_claims_rt;
CREATE OR REPLACE
TYPE ktest_driver_rt IS OBJECT
col1 varchar2(10)
,col2 varchar2(10)
,claims_nt ktest_claims_tt);
CREATE OR REPLACE
TYPE ktest_driver_tt IS TABLE OF ktest_driver_rt;
CREATE OR REPLACE
TYPE ktest_policy_rt IS OBJECT
col1 varchar2(10)
,col2 varchar2(10)
,driver_nt ktest_driver_tt);
CREATE OR REPLACE
TYPE ktest_policy_tt IS TABLE OF ktest_policy_rt;
CREATE TABLE ktest_job_table
(job_no NUMBER
,tab_type VARCHAR2(3)
,policy_nt ktest_policy_tt
NESTED TABLE policy_nt STORE AS policy_nested_tab
(NESTED TABLE driver_nt STORE AS driver_nested_tab
(NESTED TABLE claims_nt STORE AS claims_nested_tab))
/ And my local package versions:
TYPE claims_rt IS RECORD
col1 varchar2(10)
,col2 varchar2(10));
TYPE claims_tt IS TABLE OF claims_rt INDEX BY PLS_INTEGER;
TYPE driver_rt IS RECORD
col1 varchar2(10)
,col2 varchar2(10)
,claims_nt claims_tt);
TYPE driver_tt IS TABLE OF driver_rt INDEX BY VARCHAR2(20);
TYPE policy_rt IS RECORD
policy_no policy.policy_no%TYPE
,driver_tab driver_tt
,other_col VARCHAR2(20));
TYPE policy_tt IS TABLE OF policy_rt
INDEX BY pls_integer;
main_table policy_tt;What I can't get through my pea sized brain is how to turn "main_table" into an array based on ktest_policy_tt.
I got as far as:
FUNCTION convert (p_table IN policy_tt) RETURN ktest_policy_tt
IS
db_vers ktest_policy_tt := ktest_policy_tt();
db_rec ktest_policy_rt;
BEGIN
FOR i IN p_table.FIRST..p_table.LAST
LOOP
db_rec := ktest_policy_rt(p_table(i).policy_no
,p_table(i).other_col
,ktest_driver_tt(p_table(i).driver_tab(i).col1
,p_table(i).driver_tab(i).col2
,ktest_claims_tt(p_table(i).driver_tab(i).claims_nt(i).col1
,p_table(i).driver_tab(i).claims_nt(i).col1
db_vers(i) := db_rec;
END LOOP;
END;but, apart from the fact that it only coverts the first row of each table, it doesn't compile:
LINE/COL ERROR
139/10 PL/SQL: Statement ignored
143/52 PLS-00306: wrong number or types of arguments in call to
'KTEST_CLAIMS_TT'
143/52 PLS-00306: wrong number or types of arguments in call to
'KTEST_CLAIMS_TT'I'd appreciate any help as this is getting urgent.
Thanks!I would recommend writing your function in a more stepwise, explicit fashion rather than trying to write the conversion as basically one big constructor.
Firstly, you will require nested loops in your pl/sql code for the different levels of nested tables. This is not a choice, you need to do this.
Within each level of looping, explicitly create the object of the desired type before adding it to the table / record as need be.
cheers,
Anthony -
Hello
i must stock my data to a nvarchar2 data (unicode) and i must also make full text search on this column, oracle does not support full text search on unicode data, so i have decided to stock my unicode data to a blob clomun, after i can create a full text index and search data.
my problem is how to cast nvarchar2 to blob in oracle. i'm using a vb program with adodb to write and read data from the db and i must write the data (unicode text) in the blob column using the right cast which allows me to use directly adodb to read information (i have tried this with sql server with a varbinary(max) column and it works : i write a nvarchar data to the verbinary(max) column using the cast function, after, adodb can read directly the information without any explicit cast) is this possible on oracle
thank you for your helpWhat is your database character set? If it's AL32UTF8 you shouldn't really need to use NVARCHAR2 at all - you can store Unicode data in a VARCHAR2 or CLOB column.
BLOB doesn't seem like the right datatype to use - if it's text you want to index then it should be in a VARCHAR2 or a CLOB column. If you put it in a BLOB then you'll have to explicity tell Oracle not to treat it as a binary document, and you will have to handle all character set issues manually.
I'm afraid I know nothing about VB or ADODB. -
How to manipulate number data embedded in number type?
Hi all,
I have a SERVER profile table with column IP_ADD char(15) with allowed value range 224.0.0.0 to 239.255.255.255.
I want to create a constraint for this column imposing this range. Can you help me the logic/syntax please?
Is CHECK IP_ADD between '224.0.0.0' and '239.255.255.255' accecptable? is it the same logic with
CHECK IP_ADD between 224000 and 239255255255 when the column is treated as numeric?
Also....How do I create a query like,
Select min(ip_add) from range(224.0.0.0 to 239.255.255.255) where ip_add not in (select ip_add from server_profile);
I want put this as the default value for the IP_ADD field in the DATA_ENTRY web page.
Thanks a lot.Hi Bill & Nik
I got these:
create or replace
function inttoip(ip_address integer) return varchar2
deterministic
is
begin
return to_char(mod(trunc(ip_address/256/256/256),256))
||'.'||to_char(mod(trunc(ip_address/256/256),256))
||'.'||to_char(mod(trunc(ip_address/256),256))
||'.'||to_char(mod(ip_address,256));
end;
(Comments about making function deterministic and using to_char taken on board - thanks).
In Oracle 11G you could make the formatted IP address a virtual column on the host table:
alter table host
add formatted_ip_address varchar2(15)
generated always as
( to_char(mod(trunc(ip_address/256/256/256),256))
||'.'||to_char(mod(trunc(ip_address/256/256),256))
||'.'||to_char(mod(trunc(ip_address/256),256))
||'.'||to_char(mod(ip_address,256))
) virtual;
This column could then be indexed for queries if required.
Your query becomes:
select hostname, formatted_ip_address from host;2nd
===
CREATE OR REPLACE
FUNCTION inttoip(ip_address IN INTEGER) RETURN VARCHAR2 IS
v8 VARCHAR2(8);
BEGIN
-- 1. convert the integer into hexadecimal representation
v8 := TO_CHAR(ip_address, 'FMXXXXXXXX');
-- 2. convert each XX portion back into decimal
RETURN to_number(substr(v8,1,2),'XX')
|| '.' || to_number(substr(v8,3,2),'XX')
|| '.' || to_number(substr(v8,5,2),'XX')
|| '.' || to_number(substr(v8,7,2),'XX');
END inttoip;
CREATE OR REPLACE
FUNCTION iptoint(ip_string IN VARCHAR2) RETURN INTEGER IS
d1 INTEGER;
d2 INTEGER;
d3 INTEGER;
q1 VARCHAR2(3);
q2 VARCHAR2(3);
q3 VARCHAR2(3);
q4 VARCHAR2(3);
v8 VARCHAR2(8);
BEGIN
-- 1. parse the input, e.g. '203.30.237.2'
d1 := INSTR(ip_string,'.'); -- first dot
d2 := INSTR(ip_string,'.',1,2); -- second dot
d3 := INSTR(ip_string,'.',1,3); -- third dot
q1 := SUBSTR(ip_string, 1, d1 - 1); -- e.g. '203'
q2 := SUBSTR(ip_string, d1 + 1, d2 - d1 - 1); -- e.g. '30'
q3 := SUBSTR(ip_string, d2 + 1, d3 - d2 - 1); -- e.g. '237'
q4 := SUBSTR(ip_string, d3 + 1); -- e.g. '2'
-- 2. convert to a hexadecimal string
v8 := LPAD(TO_CHAR(TO_NUMBER(q1),'FMXX'),2,'0')
|| LPAD(TO_CHAR(TO_NUMBER(q2),'FMXX'),2,'0')
|| LPAD(TO_CHAR(TO_NUMBER(q3),'FMXX'),2,'0')
|| LPAD(TO_CHAR(TO_NUMBER(q4),'FMXX'),2,'0');
-- 3. convert to a decimal number
RETURN TO_NUMBER(v8, 'FMXXXXXXXX');
END iptoint;3rd
===
CREATE OR REPLACE function inet_ntoa (ip integer) return varchar2
is
ip1 integer;
ip2 integer;
ip3 integer;
ip4 integer;
ipi integer := ip;
begin
ip1 := floor(ipi/power(2,24));
ipi := ipi - (ip1*power(2,24));
ip2 := floor(ipi/power(2,16));
ipi := ipi - (ip2*power(2,16));
ip3 := floor(ipi/power(2,8));
ipi := ipi - (ip3*power(2,8));
ip4 := ipi;
return ip1||'.'||ip2||'.'||ip3||'.'||ip4;
end;
CREATE OR REPLACE FUNCTION get_token (the_list VARCHAR2,the_index NUMBER, delim VARCHAR2 := '.') RETURN VARCHAR2
IS
start_pos INTEGER;
end_pos INTEGER;
BEGIN
IF the_index = 1 THEN
start_pos := 1;
ELSE
start_pos := INSTR (the_list, delim, 1, the_index - 1);
IF start_pos = 0 THEN
RETURN NULL;
ELSE
start_pos := start_pos + LENGTH (delim);
END IF;
END IF;
end_pos := INSTR (the_list, delim, start_pos, 1);
IF end_pos = 0 THEN
RETURN SUBSTR (the_list, start_pos);
ELSE
RETURN SUBSTR (the_list, start_pos, end_pos - start_pos);
END IF;
END get_token;
CREATE OR REPLACE function inet_aton (ip varchar2) return integer
is
invalid_ip_adres exception;
pragma exception_init(invalid_ip_adres,-6502);
ipi integer;
begin
ipi := get_token(ip,4)
+(get_token(ip,3)*power(2,8))
+(get_token(ip,2)*power(2,16))
+(get_token(ip,1)*power(2,24));
return ipi;
exception
when invalid_ip_adres
then
return null;
end;
/Are these programs serve the same purpose? Which one do you like most?
Thanks a lot
Edited by: KinsaKaUy? on 22-Sep-2011 19:46 -
Is it better to use NUMBER or VARCHAR2 performance wise?
Hi,
maybe it's a silly question,
but if i want to save a numeric code in my table is it better to use NUMBER or VARCHAR2, the code will never be use in any calculation.
I'm asking this because i have an idea that is to use NUMBER for a column type only when that value will be used in mathmatic calculations of some sort, if it's not going to be used in calculation i use VARCHAR2.
But other than my choice I've been wondering what is the best choice considering performance.
Can you help?
Best regardsSentinel wrote:
Unless you need to preserve leading zeros, the number data type is the most appropriate data type to use for numeric data.I have to disagree I'm afraid.
"Leading zeros" is a display issue and should not determine how you store your data on the database. If you need to display leading zeros, then that should be done at the front-end. You can't determine future requirements against the data (businesses change their minds) so you will give yourself problems of updating your data if you find the business decides that another area should display it with e.g. another 2 leading zeros or they don't want leading zeros etc. Never store numbers as varchar for display purposes.
It's quite simple, if you are dealing with numbers, store them as numbers, if you are dealing with varchars store them as varchars, if you are dealing with dates and times, store them as date etc.
As for performance, there is no noticable difference between the datatypes... it's all Binary data when you get down to machine code level.
If you store numbers as varchar you could end up with other issues, apart from possible problems in calculations...
Comparisons against numbers stored as varchar are notoriously problematic...
SQL> select 'TRUE' from dual where '150' > '1000';
'TRU
TRUE
SQL> select 'TRUE' from dual where '150' < '1000';
no rows selected
SQL>... and then the developer wonders why the data isn't being returned. ;) -
How to Plot number and string in one row (data logger counter) ?
hi all i made data log quantity using Digital Counter via modbus to monitoring quantity and reject that has and Name Operator, Machine and Part Number.
i have problem about plot the number & string in one row, as shown on the picture below :
how to move that string on one row ? i attach my vi.
Thanks~
Attachments:
MODBUS LIB Counter.vi 39 KBDuplicate and answered - http://forums.ni.com/t5/LabVIEW/How-to-Plot-number-and-string-in-one-row-data-logger-counter-via/m-p...
-
CRM Web UI :- How to Control number of session of Web UI for a user
Hello Experts,
We do have very specific requirement which SAP doesn`t recommend in terms of multi-sessioning for a user.So we are little worried upon the challeges.Could you please share your ideas/Experience on following?
1.How to Control number of Internet explorer session for the web UI (may be 2 or 3).?
Though SAP doesn`t recommned more than one but our client requires 2 or more than that but we need to fix it either
2 or 3.
So we are looking for specific setting for this.
2.What are the challenges we need to face if we implement option 1?
3.Specially in interaction record management
a. Is there chance of loosing the data if agent has opened more than two session.
b. In which session interaction record data will get stored,the one on which agent picked up the call or in the latest
one?
Please let me know if I am not clear enough on any point.
Thanks in advance,
Shailendra TolambiyaHi Shailendra,
The following wiki page might be useful in this respect:
http://wiki.sdn.sap.com/wiki/x/gxdKDQ
Best Regards,
Shiromani -
How to count number of sales orders generated in a month in SAP SD
Hi SD Gurus,
I have a very strange query from client. I have to count the number of sales order created in a month for a z report. For example 30 in Jan, 25 in Feb etc. Could anyone suggest me How to count number of sales orders generated in a month in SAP SD.
Regards
Vinod KumarHi,
Goto the T.Code "SE16" or "SE16n" or "SE11".
Enter the table name as VBAK
Enter the created on date as the starting date of the period and to date as the end date.
Enter.
Click on "Number of Entries".It will tell you the number of entries created in a particular period.
If you want a report,goto the T.Code "VA05n".
Regards,
Krishna. -
I have a Windows copy of Photoshop Elements 11, but the serial number has been lost. I didn't buy the product and I am unable to find the box. As far as I know, it's never been used so has not been registered. Does anyone know how the serial number can be recovered under these circumstances?
If you never bought the product then you cannot get the serial number.
-
How to get number of rows return in SELECT query
i'm very new in java, i have a question:
- How to get number of rows return in SELECT query?
(i use SQL Server 2000 Driver for JDBC and everything are done, i only want to know problems above)
Thanks.make the result set scroll insensitve, do rs.last(), get the row num, and call rs.beforeFirst(), then you can process the result set like you currently do.
String sql = "select * from testing";
PreparedStatement ps =
con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = ps.executeQuery();
rs.last();
System.out.println("Row count = " + rs.getRow());
rs.beforeFirst();~Tim
NOTE: Ugly, but does the trick. -
How to restrict number of rows display using ig:gridView
Hi
All
How to restrict number of rows display using <ig:gridView datasource="{mybean.mylist}">
i am getting 10 rows using data_row . i wanna show only first 5 rows .
pageSize ="5" will be ok . but it displays remaining rows in next page. but i want to display only first 5 rows . is there any attribute to restrict number of rows.
thanks
rambhapuriI have no idea which component you're talking about. If want to discuss here about a non-Sun JSF component, then please mention about the name, version and build in detail. You can also consider posting this question at the website/forum/mailinglist of the component manfacturer rather than here. At least I can tell you that the Sun JSF h:dataTable has the 'rows' attribute for that. I can also suggest you to just take a look in the TLD documentation of the component in question. There should be all possible attributes listed with a detailed explanation about the behaviour.
-
How to calculate number of rows for perticular characterstic in SAP BI Bex
Hi experts,
Please let me know how to calculate ' number of rows ' for perticular characterstic in Bex query.
Thanks & Regards,
Babu..Hello,
You can try this
Create a CKF and assign the vale 1 to it. Open the query and select Character where you want to display ' number of rows ', go to properties windows, select 'display', in the results row drop down box, select 'always display'.
Thanks.
With regards,
Anand Kumar
Maybe you are looking for
-
Slow report caused by 85K record dataset
I'm developing a report that cross validates fields in the Item table, based on criteria-FILTERS. I have 30 such FILTERS. Item Table is all about Item SKU properties and has Fields like: group, category, colour, price, cost, brand,class, etc Filters
-
What I am trying to do is, get the current row and compare with the previous from the resultset. To achieve it I am using the previous(), absolute() and next() methods from ResultSet. Now to be able to use these methods I believe I need to define con
-
It's any way to get my website out of Muse? I need to send the desig to my client before develop and, as I'm using it for the first time, I've found that I can't export it in anyway!!! How it is possible? No printing neither????Does anybody know how
-
TS3274 ipad will not charge when USB cord connected to PC or socket.
When i plug the USB cord to either electrical socket or computer. ipad will not charge. Says NOT CHARGING. Please HELP.
-
TS1413 computer stuck after mountain lion osx install
computer is stuck after trying to download mountain lion osx?