• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to convert?

 
Edward Durai
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.

Thanks
edward
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34870
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edward,
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?
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edward,

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
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
:p

Hi

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic