Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

converting from char(8) to smallmoney

Status
Not open for further replies.

cybercop23

IS-IT--Management
Aug 12, 2001
103
US
Hi all.
I have some data that I'm importing from an ODBC. The source data is money represented as 123456 (1234.56). I import it as char(8) but need to display it to the user as 1,234.56.

I tried convert, but it truncates the cents:
CONVERT(smallmoney, Amount/100) 123456 -> 1234.0000
CAST(Amount/100 as smallmoney) 123456 -> 1234.0000

Any suggestions?
 
Looks like integer math to me. try....

CONVERT(smallmoney, Amount/100.00) 123456 -> 1234.0000

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
George,
That what was in my example. When I divide, it loosed the cents. So 123456 should be 1,234.56, however cast and convert will result in 1234.0000. It looses the 56 cents.
 
Sorry, I forgot to remove your comments when I copy/pasted. Open a blanks Query Analyzer window and run this...

Code:
Select Convert(SmallMoney, 123456/100) [green]--  1234.0000[/green]
Select Convert(SmallMoney, 123456/100[red].0[/red])[green]--  1234.5600[/green]

The .0 causes sql server to do floating point math instead of integer math.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Also forgot to mention how to put the commas in every 3 characters (to the left of the decimal).

Select [blue]Convert(VarChar(8), [/blue]Convert(SmallMoney, 123456/100.0)[blue], 1)[/blue] [green]-- 1,234.56[/green]

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Thanks much George. Had to cast the Amount from char(8) to numeric, and it worked.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top