• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

how to convert?

 
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
 
author & internet detective
Posts: 40211
816
Eclipse IDE VI Editor Java
  • 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?
 
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";
 
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.
 
No, tomorrow we rule the world! With this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic