How Do I Write Sql Statemen That Can Cast String To Double

A Member? Please Login  
type your username and password to login
Date: October 08, 2008, 12:55 AM
247334 members and 146309 Topics
Latest Member: aderhema
Nairaland [Nigerian Forum] Home Help Search Who is currently online? Login Register
Nairaland Forum  |  Technology  |  Programming  |  How Do I Write Sql Statemen That Can Cast String To Double
Pages: (1) Go Down Send this topic Notify of replies
Author Topic: How Do I Write Sql Statemen That Can Cast String To Double  (Read 241 views)
luckyCO
How Do I Write Sql Statemen That Can Cast String To Double
« on: June 21, 2007, 04:51 AM »

I need SQL statement that can cast string (A column in a table) to double or int. I used this select  Cast(Amount_Paid as int)  from contractpayment but wasn't working. Amount paid is in string data type for a reason,  Plz I need ur help
Fdeveloper (m)
Re: How Do I Write Sql Statemen That Can Cast String To Double
« #1 on: June 21, 2007, 10:08 AM »

I assume you're using MS-SQL in which case you could try rounding the column first before the cast as maybe the column is stored in a decimal format

select cast(round(amount_paid, 0) as int) as "AmountPaid"
  from contractpayment


Alternatively, if your intention was to Cast as double then you should use the Double qualifier in the Cast() function [i.e. Cast(<Column Name> as Double)]


I don't know if it's relevant but Oracle has a built function for converting varchar2 to a number as follows:

to_number('1210.73', '9999.99') would return the number 1210.73
to_number('546', '999') would return the number 546
to_number('23', '99') would return the number 23


So your example would be written as follows:

select to_number(amount_paid '9,999,999') as "AmountPaid"
  from contractpayment

luckyCO
Re: How Do I Write Sql Statemen That Can Cast String To Double
« #2 on: June 22, 2007, 05:23 AM »

Am using MySQL 5.1. I have used all these functions but none worked out for. I don't know what is the problem. Is still complain that I have not used the right MySQL syntax for cast.
Fdeveloper (m)
Re: How Do I Write Sql Statemen That Can Cast String To Double
« #3 on: June 22, 2007, 02:11 PM »

It could be that you are not using one of the correct type identifiers in your Cast() function. From what I can see in the documentation, "int" is not one of the defined types

I got this from the MySQL Developer site http://dev.mysql.com/doc/refman/5.1/en/cast-functions.html:

Quote
CAST(expr AS type), CONVERT(expr,type), CONVERT(expr USING transcoding_name)

The CAST() and CONVERT() functions take a value of one type and produce a value of another type.

The type can be one of the following values:
  BINARY[(N)]
  CHAR[(N)]
  DATE
  DATETIME
  DECIMAL
  SIGNED [INTEGER]
  TIME
  UNSIGNED [INTEGER]

I guess you should also ensure that the contents of the column can be safely converted in that it doesn't contain any non numeric characters such as currency indicators, semi colons etc. and also ensure that the column in question is not null.

What is the output if you just do a straightforward select without a Cast() or Convert()?
luckyCO
Re: How Do I Write Sql Statemen That Can Cast String To Double
« #4 on: June 22, 2007, 06:43 PM »

Thanks so much Fdeveloper I was casting to data types not supported. SO I used decimal and it worked.
 Thanks Alexis  Programmers Needed - Vb.net, Php, Javascript  How To Connect An Html Form 2 Databases  Page 2
Pages: (1) Go Up Send Topic to Friend by E-mail Reply 
Google
 
Web www.nairaland.com
Sections: TV/Movies (2) Music/Radio (2) Celebrities Job Talk Jobs/Vacancies (2) Career Talk Romance Books Politics Sports Fashion Travel
Health Schooling Religion General(2) Business Webmaster Programming Computers Phones Cars & Trucks

Links: Page1 Page2 Page3 Page4 Page5 Page6 Page7 Page8 Page9 Page10

Nairaland is owned by Oluwaseun Osewa
Nairaland Forum | Powered by SMF 1.0.12.
© 2001-2005, Lewis Media. All Rights Reserved.