File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes how to convert? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "how to convert?" Watch "how to convert?" New topic

how to convert?

Edward Durai
Ranch Hand

Joined: Oct 09, 2004
Posts: 223

I am using mssql. how can i convert varchar to int type.

for eg.

id (int) txt(varchar)
1 10
2 10a
3 10b

i want to convert varchar to int. HOw? pls explain with query.


Thank You<br />Edward
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33132

So you would want to covert 10 --> 1, 10a --> 2, etc?

I don't understand the relationship here. Is it just to give each varchar a unique value (you could use a sequence for this) or something else?

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518

By "convert" do you mean "get the integer associated with the string"?

That would be something like:

select my_integer from my_table where my_string = "10b";
Dimit Chadha

Joined: Jul 05, 2006
Posts: 2


I think you just want to convert the varchar columns data to int data.

For this first retrieve the data as varchar and then convert to int as

String varchar=rs.getString("abcd");
int aa=Integer.parseInt(varchar);
this will throw the parse exception if the varchar contains any characters.

Otherway around , friend change the database column to int as actually you want int value . Why you r using varchar.
I agree. Here's the link:
subject: how to convert?
It's not a secret anymore!