Showing posts with label money. Show all posts
Showing posts with label money. Show all posts

Sunday, March 25, 2012

converting a text field to number

I have a table with over a million rows and one of the fields contains
amounts of money in text format.
What is the most efficient way of converting this field to a number
format that I can sum on?

Regards,
CiarnHi Ciaran,

Before converting the type of the col, make sure that there are no
invalid values in that column. You can pull them out by

SELECT colName FROM tabName WHERE IsNumeric(colName) = 0

Alter your column type by

ALTER TABLE tableName ALTER COLUMN colName NUMERIC

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!|||Nazeer Oasis (nazeerpp@.indiatimes.com) writes:
> Before converting the type of the col, make sure that there are no
> invalid values in that column. You can pull them out by
> SELECT colName FROM tabName WHERE IsNumeric(colName) = 0
> Alter your column type by
> ALTER TABLE tableName ALTER COLUMN colName NUMERIC

Unfortunately, this may still fail, since IsNumeric will approve of
values than converts to float or money, but not to numeric. Also, I
say that it's extremely bad practice to say numeric without specifying
scale and precision. You get some defaults, but these may not be what
you expect.

As for the original query, the easy way is:

SELECT SUM(convert(int, textcol)) FROM tbl
or SELECT SUM(convert(money, textcol)) FROM tbl

But this will of course fail if there are strings that does not convert.

If all data is integer, that is the text is undelimited and there are
no decimals, then it's pretty easy to test:

textcol NOT LIKE '%[0-9]%'

If the text can delimiters and decimals, it can become quite hairy
to filter.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Erland Sommarskog (esquel@.sommarskog.se) writes:
> If all data is integer, that is the text is undelimited and there are
> no decimals, then it's pretty easy to test:
> textcol NOT LIKE '%[0-9]%'

This is wrong. I forgot a ^:

textcol NOT LIKE '%[^0-9]%'

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp

Tuesday, March 20, 2012

Convert varchar to money/int

I have a table with varchar fields & I need to covert the fields to money
fields. My problem is if the varchar data is of format (581.63) how can I
change that to -581.63 before then doing the convert ie using CAST(July AS
money). Using there CAST function fails because of the brackets.You might try using a CASE expression:
CASE WHEN LEFT(July, 1) = '(' AND RIGHT(July, 1) = ')'
THEN CAST(SUBSTRING(July, 2, LEN(July) - 2) AS money) * -1
ELSE CAST(July AS money) END AS July
Hope this helps.
Dan Guzman
SQL Server MVP
"ChrisB" <ChrisB@.discussions.microsoft.com> wrote in message
news:389EEB22-E89F-413B-823B-5F045C9F3B04@.microsoft.com...
>I have a table with varchar fields & I need to covert the fields to money
> fields. My problem is if the varchar data is of format (581.63) how can I
> change that to -581.63 before then doing the convert ie using CAST(July AS
> money). Using there CAST function fails because of the brackets.

Convert VARCHAR to money value

Ok i know this is simple but i just don't know the syntax. What I have
is a bunch of values and i want to be able to display them as money
values with a $ in front.
eg.
16000000
1000
160000
TO THIS
$16,000,000.00
$1000.00
$16,000.000
I'm using MS SQL 2000
Cheers
I guess it's not straight forward by just using convert function to change
datatype to money.
You need to write a function which will append $ and will separate thousands
with comma.
Woodies_46@.hotmail.com wrote:
>Ok i know this is simple but i just don't know the syntax. What I have
>is a bunch of values and i want to be able to display them as money
>values with a $ in front.
>eg.
>16000000
>1000
>160000
>TO THIS
>$16,000,000.00
>$1000.00
>$16,000.000
>I'm using MS SQL 2000
>Cheers
Message posted via droptable.com
http://www.droptable.com/Uwe/Forums...ivity/200603/1

Monday, March 19, 2012

Convert VARCHAR to money value

Ok i know this is simple but i just don't know the syntax. What I have
is a bunch of values and i want to be able to display them as money
values with a $ in front.
eg.
16000000
1000
160000
TO THIS
$16,000,000.00
$1000.00
$16,000.000
I'm using MS SQL 2000
CheersI guess it's not straight forward by just using convert function to change
datatype to money.
You need to write a function which will append $ and will separate thousands
with comma.
Woodies_46@.hotmail.com wrote:
>Ok i know this is simple but i just don't know the syntax. What I have
>is a bunch of values and i want to be able to display them as money
>values with a $ in front.
>eg.
>16000000
>1000
>160000
>TO THIS
>$16,000,000.00
>$1000.00
>$16,000.000
>I'm using MS SQL 2000
>Cheers
Message posted via droptable.com
http://www.droptable.com/Uwe/Forum...tivity/200603/1|||On 7 Mar 2006 20:32:18 -0800, Woodies_46@.hotmail.com wrote:

> Ok i know this is simple but i just don't know the syntax. What I have
> is a bunch of values and i want to be able to display them as money
> values with a $ in front.
> eg.
> 16000000
> 1000
> 160000
> TO THIS
> $16,000,000.00
> $1000.00
> $16,000.000
>
> I'm using MS SQL 2000
> Cheers
Ideally, this formatting would be done in your front-end application (or
reporting tool), rather than being formatted by SQL.

Convert VARCHAR into Money Value

Ok i know this is simple but i just don't know the syntax. What I have
is a bunch of values and i want to be able to display them as money
values with a $ in front.

eg.

16000000
1000
160000

TO THIS

$16,000,000.00
$1000.00
$16,000.000

I'm using MS SQL 2000

Cheers(Woodies_46@.hotmail.com) writes:
> Ok i know this is simple but i just don't know the syntax. What I have
> is a bunch of values and i want to be able to display them as money
> values with a $ in front.
> 16000000
> 1000
> 160000
> TO THIS
> $16,000,000.00
> $1000.00
> $16,000.000
>
> I'm using MS SQL 2000

Normally, formatting should be done client-side. Not the least because
the format is dependent on regional settings. The above is not how I
want my decimal commas and thousands separator.

Nevertheless, you can do this:

declare @.x money
select @.x = 9009090909
select '$' + convert(varchar, @.x, 1)

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx

convert txt to money data type

Hi,

I'm using the data type "money" in my SQL database and want to convert what's in txtPrice_textBox to the "money" format. I'm currently using the following code:

' objectCym.price = Convert.ToInt16(txtPrice_textBox.Text) '

Will this work? Is there any reason I should stay away from the "Money" data type?

Thanks,

David

I think you can use Money I only advice not using money when you are have precision problems which happens. I think you should change Int16 to decimal and run your code. Try the link below and look at the shopping Cart code for sample code. Hope this helps.

http://asp.net/CommerceStarterKit/docs/docs.htm

|||you can do a convert.todecimal or convert.todouble. AFAIK there is no reason you should stay away from Money types.|||

Thanks for the help,

dp

Saturday, February 25, 2012

convert numbers in SQL

how can i format numbers in SQL
Eg. i have number such as 1250.000002341 in a table [money].[Paid]
how can i fornat that number to 125,00?With the number stored in "money.paid", useCAST(money.paid AS DECIMAL(10,2))|||formatting is usually a function of your user interface. So its rare to come accross a requirement like this in SQL. Where are you using this information - in a report, in a screen, in a text file?

Convert NULL to zero

One table I am working with has several money columns. Some are null. I need
to add the columns, but the result is NULL since some of the columns are
NULL.
How can I write the query to convert to zeroes?
Thanks.SELECT COLAESCE(col1,0) + COALESCE(col2,0) + ...
FROM Sometable
--
David Portas
--
Please reply only to the newsgroup
--
"Paul" <nospam@.please.com> wrote in message
news:%23nSQbLK1DHA.2336@.TK2MSFTNGP09.phx.gbl...
> One table I am working with has several money columns. Some are null. I
need
> to add the columns, but the result is NULL since some of the columns are
> NULL.
> How can I write the query to convert to zeroes?
> Thanks.
>|||Thank you. I'll try that.
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:AL-dnQNnTNpeqWai4p2dnA@.giganews.com...
> SELECT COLAESCE(col1,0) + COALESCE(col2,0) + ...
> FROM Sometable
> --
> David Portas
> --
> Please reply only to the newsgroup|||You can also do it with ISNULL:
SELECT SUM(ISNULL(col1,0)), SUM(ISNULL(col2,0)) + ...
FROM TestTable
--
Rohtash Kapoor
http://www.sqlmantra.com
"Paul" <nospam@.please.com> wrote in message
news:OtDfAjK1DHA.2308@.TK2MSFTNGP11.phx.gbl...
> Thank you. I'll try that.
> "David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
> news:AL-dnQNnTNpeqWai4p2dnA@.giganews.com...
> > SELECT COLAESCE(col1,0) + COALESCE(col2,0) + ...
> > FROM Sometable
> >
> > --
> > David Portas
> > --
> > Please reply only to the newsgroup
>|||Since the SUM aggregate ignores NULLs anyway you will save some processor
cycles by putting ISNULL (or COALESCE) outside the SUM. This deals with the
specific case where all values in the column are NULL.
SELECT ISNULL(SUM(col1),0), ISNULL(SUM(col2),0) + ...
FROM TestTable
COALESCE is an ANSI Standard SQL function which is why I usually prefer to
use it in preference to the proprietary ISNULL function.
--
David Portas
--
Please reply only to the newsgroup
--|||You are very right.
Instead of this:
SELECT SUM(ISNULL(col1,0)), SUM(ISNULL(col2,0)) + ...
FROM TestTable
We should write:
SELECT ISNULL(SUM(col1),0), ISNULL(SUM(col2),0) + ...
FROM TestTable
However, I prefer to use ISNULL than COALESCE because I can type it quickly
(...just kidding..)
--
Rohtash Kapoor
http://www.sqlmantra.com
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:9O-dnaPUQsGe32aiRVn-vA@.giganews.com...
> Since the SUM aggregate ignores NULLs anyway you will save some processor
> cycles by putting ISNULL (or COALESCE) outside the SUM. This deals with
the
> specific case where all values in the column are NULL.
> SELECT ISNULL(SUM(col1),0), ISNULL(SUM(col2),0) + ...
> FROM TestTable
> COALESCE is an ANSI Standard SQL function which is why I usually prefer to
> use it in preference to the proprietary ISNULL function.
> --
> David Portas
> --
> Please reply only to the newsgroup
> --
>|||David Portas (REMOVE_BEFORE_REPLYING_dportas@.acm.org) writes:
> Since the SUM aggregate ignores NULLs anyway you will save some
> processor cycles by putting ISNULL (or COALESCE) outside the SUM. This
> deals with the specific case where all values in the column are NULL.
> SELECT ISNULL(SUM(col1),0), ISNULL(SUM(col2),0) + ...
> FROM TestTable
> COALESCE is an ANSI Standard SQL function which is why I usually prefer to
> use it in preference to the proprietary ISNULL function.
On the other hand, if you do this and run with ANSI_WARNINGS enabled, you
will get the message "Warnings: null eliminated from aggregate".
(ANSI_WARNINGS is enabled for default, unless you are using DB-Library.)
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp

Friday, February 24, 2012

Convert Money to Varchar(50)

I created a table using sql, and I accidentally made one of the fields a money datatype and I meant to make it a Varchar(50). Is there an easy way to change the datatype with an sql update/convert statement. I want to make the change permanent. As I need it to reflect on a couple of different databases with this same table name. Here is the update statement I have been trying.

update ibapoorderline set salestype=convert(varchar(50),salestype)

There is currently no data stored in the column salestype. They are all null. I just want to change the datatype so I can begin adding data.

Thanks in advance.Drop the column and re-add it.

create table f00 (f00id int, f00monex money)
go
--whoops!
alter table f00 drop f00monex
go
alter table f00 add f00varchar varchar(50) NULL
go

Convert will only convert the data for output, it won't change the datatype of the column.|||Thank you so much. What I was looking for was the drop line. That helps a lot. I couldn't remember how to get rid of a column and then add it back in with a different datatype.|||No problem. Note that when you drop/add it moves the column to the bottom of the column list. Not sure if you care about that...

Look up ALTER TABLE in Books Online for more syntax.

Convert Money to Char

I have a sql query listed below, I would like to count the number of values
that the first 5 characters are '80438' from iNum field.
Please help me correct the sql query listed below.
Thank You,
SELECT count(iNum) from Call_Movement
where substring(cast(iNum as money) as char(20),1,5) like '80438'
iNum Money Format
iNum Data In Call_Movement Table
803482000146220.0000
803482000147143.0000
803482000153805.0000You are very close. You're mistake is mostly with the "like."
Select count(iNum)
from Call_Movement
where left(cast(iNum as varchar(30)),5) = '80348'
I do not know your data as well as you, but I can see a day when there
will be more than twenty characters available and this code failing.
Joe K. wrote:
> I have a sql query listed below, I would like to count the number of value
s
> that the first 5 characters are '80438' from iNum field.
> Please help me correct the sql query listed below.
> Thank You,
> SELECT count(iNum) from Call_Movement
> where substring(cast(iNum as money) as char(20),1,5) like '80438'
> iNum Money Format
> iNum Data In Call_Movement Table
> 803482000146220.0000
> 803482000147143.0000
> 803482000153805.0000|||Dear joe,
instead of cast and substring, u can use convert()
e.g.,
SELECT count(iNum) from Call_Movement
where convert(varchar(50), iNum,0) like '80438%'
Thanks & Regards
Ravi
"Joe K." wrote:

> I have a sql query listed below, I would like to count the number of value
s
> that the first 5 characters are '80438' from iNum field.
> Please help me correct the sql query listed below.
> Thank You,
> SELECT count(iNum) from Call_Movement
> where substring(cast(iNum as money) as char(20),1,5) like '80438'
> iNum Money Format
> iNum Data In Call_Movement Table
> 803482000146220.0000
> 803482000147143.0000
> 803482000153805.0000
>
>

convert money datatype

Hello!

I need to convert money datatype into string without decimal points.

For example, I have column pmt_amt (money datatype)=27.00.

Is there a way to convert this amount into format "2700" without decimals?

Thanks,

Lena

Can you do that in the presentation layer?

declare @.m money

set @.m = 27

select @.m, str(@.m * 10000, 15, 0)

set @.m = 27.0015

select @.m, str(@.m * 10000, 15, 0)

AMB

|||

Thank you!

I tried your conversion and here's my results:

value in table converted

47.02 470200 1202.78 12027800 411.91 4119100 20401.78 204017800 711.01 7110100 517.83 5178300 11756 117560000 773 7730000 3439.75 34397500 4062.66 40626600

How can I get rid of zeroes in the end of converted values?

E. G.: for 40626600 I need it t be 406266.

|||

I would 'refine' AMB's suggestion with the following:

If you want the results to have ONLY 2 numbers to the right of the decimal, then use 100 instead of 10000 in the str() function. That is because the money datatype has four (4) numbers to the right of the decimal, and EVEN though you often ignore them, they are there.

(It will still round up/down as needed.)

And if you have really large numbers, the '15' in the string function may need to be increased.

|||

Money data type has 4 decimal digits. If you want to consider just the first two, then multiply the value by 100 instead 10000.

declare @.m money

set @.m = 4062.66

select @.m, str(@.m * 100, 15, 0)

AMB

|||

Thank you for all your replies.

It helped a lot.

Convert money columns to update?

Hi Guys

I need your help again, I am try to update several columns and the data type is 'money'.

Below is the code I have used:

UPDATE CAT_Products
SET

UnitCost ='10.00',
UnitCost2 = '10.00',
UnitCost3 = '10.00',
UnitCost4 = '10.00',
UnitCost5 = '10.00',
UnitCost6 = '10.00'

WHERE ProductCode = '0008'

But it will not update, instead I get this error:

-----------------------------------------------------------

>[Error] Script lines: 1-9 --------
Disallowed implicit conversion from data type varchar to data type money, table 'dbo.CAT_Products', column 'UnitCost'. Use the CONVERT function to run this query.

More exceptions ... Disallowed implicit conversion from data type varchar to data type money, table '.dbo.CAT_Products', column 'UnitCost2'. Use the CONVERT function to run this query.

-----------------------------------------------------------

The error message indicates that I need to use the convert function. But the columns data type is set at 'money' not 'varcher' . So do I need to convert data type to 'varcher' in order to update and convert back to data type 'money' when update complete? Or do I need to indicate in the update statement that data type is already 'money'? I am not sure how I would either.

Thanks

try:

UPDATE CAT_Products
SET

UnitCost =convert(money,'10.00'),
UnitCost2 = convert(money,'10.00'),
UnitCost3 = convert(money,'10.00'),
UnitCost4 =convert(money,'10.00'),
UnitCost5 = convert(money,'10.00'),
UnitCost6 = convert(money,'10.00')

WHERE ProductCode = '0008'

|||

Hi jpazgier

Thank you for your reply and code.

I am pleased to say that your code worked first time.

A great help.

Cheers.

CONVERT Money Char(20)

I have a sql query listed below that I would like to only output values like
'6024', the Bonus values are converted to char(20) from Call_Movement table.
SELECT cast(cast(Bonus as money) as char(20)) like = '6024' from
Call_Movement
where DATEDIFF(mi, Start_Time, GETDATE()) <=60
Please help me complete this task.Joe K. wrote:
> I have a sql query listed below that I would like to only output values like
> '6024', the Bonus values are converted to char(20) from Call_Movement table.
>
> SELECT cast(cast(Bonus as money) as char(20)) like = '6024' from
> Call_Movement
> where DATEDIFF(mi, Start_Time, GETDATE()) <=60
> Please help me complete this task.
Please post with table structure and simple data and desired output.
if you are not using wildcard then no need to use like in the query
just use =.
Also you can not put = after like and you have to put like operator in
where clause.
Regards
Amish Shah

CONVERT Money Char(20)

I have a sql query listed below that I would like to only output values like
'6024', the Bonus values are converted to char(20) from Call_Movement table.
SELECT cast(cast(Bonus as money) as char(20)) like = '6024' from
Call_Movement
where DATEDIFF(mi, Start_Time, GETDATE()) <=60
Please help me complete this task.Joe K. wrote:

> I have a sql query listed below that I would like to only output values li
ke
> '6024', the Bonus values are converted to char(20) from Call_Movement tabl
e.
>
> SELECT cast(cast(Bonus as money) as char(20)) like = '6024' from
> Call_Movement
> where DATEDIFF(mi, Start_Time, GETDATE()) <=60
> Please help me complete this task.
Please post with table structure and simple data and desired output.
if you are not using wildcard then no need to use like in the query
just use =.
Also you can not put = after like and you have to put like operator in
where clause.
Regards
Amish Shah

Convert Money as String $XX.XX in output

How can I convert or format a Money defined field to look like $xx,xxx.xx?
My data is stored like 50000.99 but I'd like to see $50,000.99 in the query
results. I tried using CAST (SELET CAST (MyField As Money) As MyField FROM
MyTable...) but that didn't seem to change anything.
Thanks!Try this:
SELECT '$' + convert(char(9), MyField , 1)
FROM MyTable
It is not perfect because it will have space between $ and the digit if the
amount is small, and you need to make sure your money amont is less then
100.000. Try it and make more modification as you need.
Perayu
"ShaneFowlkes" <shanefowlkes@.h-o-t-m-a-i-l.com> wrote in message
news:%23qH6Nm6bGHA.1960@.TK2MSFTNGP05.phx.gbl...
> How can I convert or format a Money defined field to look like $xx,xxx.xx?
> My data is stored like 50000.99 but I'd like to see $50,000.99 in the
> query results. I tried using CAST (SELET CAST (MyField As Money) As
> MyField FROM MyTable...) but that didn't seem to change anything.
> Thanks!
>|||ShaneFowlkes,
See function "convert" in BOL.
Example:
select '$' + convert(varchar(25), cast(50000.99 as money), )
go
AMB
"ShaneFowlkes" wrote:

> How can I convert or format a Money defined field to look like $xx,xxx.xx?
> My data is stored like 50000.99 but I'd like to see $50,000.99 in the quer
y
> results. I tried using CAST (SELET CAST (MyField As Money) As MyField FRO
M
> MyTable...) but that didn't seem to change anything.
> Thanks!
>
>|||This one is better:
SELECT '$' + convert(varchar(15), MyField , 1)
FROM MyTable.
Perayu
"Perayu" <yu.he@.state.mn.us.Remove4Replay> wrote in message
news:eN62z16bGHA.5116@.TK2MSFTNGP02.phx.gbl...
> Try this:
> SELECT '$' + convert(char(9), MyField , 1)
> FROM MyTable
> It is not perfect because it will have space between $ and the digit if
> the amount is small, and you need to make sure your money amont is less
> then 100.000. Try it and make more modification as you need.
> Perayu
> "ShaneFowlkes" <shanefowlkes@.h-o-t-m-a-i-l.com> wrote in message
> news:%23qH6Nm6bGHA.1960@.TK2MSFTNGP05.phx.gbl...
>|||"ShaneFowlkes" <shanefowlkes@.h-o-t-m-a-i-l.com> wrote in message
news:%23qH6Nm6bGHA.1960@.TK2MSFTNGP05.phx.gbl...
> How can I convert or format a Money defined field to look like $xx,xxx.xx?
> My data is stored like 50000.99 but I'd like to see $50,000.99 in the
query
> results. I tried using CAST (SELET CAST (MyField As Money) As MyField
FROM
> MyTable...) but that didn't seem to change anything.
General note though is this is typically something you want to do in the
front end, not the back end.
But for quick dirty stuff, what others said should work.

> Thanks!
>

Convert Money as String $XX.XX in output

How can I convert or format a Money defined field to look like $xx,xxx.xx?
My data is stored like 50000.99 but I'd like to see $50,000.99 in the query
results. I tried using CAST (SELET CAST (MyField As Money) As MyField FROM
MyTable...) but that didn't seem to change anything.
Thanks!Try this:
SELECT '$' + convert(char(9), MyField , 1)
FROM MyTable
It is not perfect because it will have space between $ and the digit if the
amount is small, and you need to make sure your money amont is less then
100.000. Try it and make more modification as you need.
Perayu
"ShaneFowlkes" <shanefowlkes@.h-o-t-m-a-i-l.com> wrote in message
news:%23qH6Nm6bGHA.1960@.TK2MSFTNGP05.phx.gbl...
> How can I convert or format a Money defined field to look like $xx,xxx.xx?
> My data is stored like 50000.99 but I'd like to see $50,000.99 in the
> query results. I tried using CAST (SELET CAST (MyField As Money) As
> MyField FROM MyTable...) but that didn't seem to change anything.
> Thanks!
>|||ShaneFowlkes,
See function "convert" in BOL.
Example:
select '$' + convert(varchar(25), cast(50000.99 as money), )
go
AMB
"ShaneFowlkes" wrote:
> How can I convert or format a Money defined field to look like $xx,xxx.xx?
> My data is stored like 50000.99 but I'd like to see $50,000.99 in the query
> results. I tried using CAST (SELET CAST (MyField As Money) As MyField FROM
> MyTable...) but that didn't seem to change anything.
> Thanks!
>
>|||This one is better:
SELECT '$' + convert(varchar(15), MyField , 1)
FROM MyTable.
Perayu
"Perayu" <yu.he@.state.mn.us.Remove4Replay> wrote in message
news:eN62z16bGHA.5116@.TK2MSFTNGP02.phx.gbl...
> Try this:
> SELECT '$' + convert(char(9), MyField , 1)
> FROM MyTable
> It is not perfect because it will have space between $ and the digit if
> the amount is small, and you need to make sure your money amont is less
> then 100.000. Try it and make more modification as you need.
> Perayu
> "ShaneFowlkes" <shanefowlkes@.h-o-t-m-a-i-l.com> wrote in message
> news:%23qH6Nm6bGHA.1960@.TK2MSFTNGP05.phx.gbl...
>> How can I convert or format a Money defined field to look like
>> $xx,xxx.xx? My data is stored like 50000.99 but I'd like to see
>> $50,000.99 in the query results. I tried using CAST (SELET CAST (MyField
>> As Money) As MyField FROM MyTable...) but that didn't seem to change
>> anything.
>> Thanks!
>|||"ShaneFowlkes" <shanefowlkes@.h-o-t-m-a-i-l.com> wrote in message
news:%23qH6Nm6bGHA.1960@.TK2MSFTNGP05.phx.gbl...
> How can I convert or format a Money defined field to look like $xx,xxx.xx?
> My data is stored like 50000.99 but I'd like to see $50,000.99 in the
query
> results. I tried using CAST (SELET CAST (MyField As Money) As MyField
FROM
> MyTable...) but that didn't seem to change anything.
General note though is this is typically something you want to do in the
front end, not the back end.
But for quick dirty stuff, what others said should work.
> Thanks!
>

Sunday, February 19, 2012

convert int to money without decimal and cents

I am doing the following to change an int into money (I want the commas) but it is adding a decimal and 2 zeroes at the end:
convert(varchar,convert(money,m.[FirstTier]),1) as 'First Tier',

m.firsttier = '1583456'
after conversion = '1,583,456.00'

Is there any easy way to not have the '.00'?

Thanks.this trick should do:
parsename(convert(varchar,convert(money,m.[FirstTier]),1),2) as 'First Tier'|||That worked... thanks!
|||Note that PARSENAME returns unicode string so you need to be careful when using that expression in WHERE clause for example. (for example if the column against which you are comparing is indexed and it is varchar , then with this expression the column will be converted to unicode) On the other hand, if this is for display purposes then it is better done in the client-side. Doing it on the client-side, you can handle other regional settings also.

Tuesday, February 14, 2012

CONVERT function use

I'll go right to the point:

I have a textbox in wich the user can seize a "money" value.

But when i'm doing the INSERT command, it fails and says I cannot implicitely convert nvarchar data (mytextbox.text) to money data.

I don't know how to handle this...


objCmd = New SqlCommand("INSERT INTO tbl_appel_service_pieces " & _
"(fld_nom_piece, fld_quantite, fld_prix, fld_description, fld_num_appel) " & _
"VALUES (@.fld_nom_piece, @.fld_quantite, @.fld_prix, @.fld_description, @.fld_num_appel)", objConn)

objCmd.Parameters.Add("@.fld_nom_piece", champsNomPiece.Text)
objCmd.Parameters.Add("@.fld_quantite", champsQuantite.Text)
objCmd.Parameters.Add("@.fld_prix", champsPrix.Text)
objCmd.Parameters.Add("@.fld_description", champsDescription.Text)
objCmd.Parameters.Add("@.fld_num_appel", champsNumAppel.Text)

objConn.Open()
objCmd.ExecuteNonQuery()
objConn.Close()

afficherPieces()

How and where to do the conversion ? On the VALUES variable ?objCmd.Parameters.Add("@.fld_nom_piece", SqlDbType.Money)
objCmd.Parameters("@.fld_nom_piece").Value = System.Convert.ToDecimal(champsNomPiece.Text)|||Thanks ! It worked well.|||I have a similar problem in that I have a text box which holds a numeric value and when I try to insert the value into the numeric field in the SQL database I get the same error message.

I have tried the suggestion above but then I get another error message saying incorrect input.

Any idea's ??

Thanks

Convert from money to varchar?

I want to print in an error message a money value but have to convert it to a varchar first. I do not have any clue how to do this. Could someone help me out?

SELECT @.iError = @.@.error, @.iRowCount = @.@.rowcount
IF(@.iError <> 0 OR @.iRowCount <> 1)
BEGIN
CONVERT(@.dValue, @.cValue)
set @.cError = 'Error attempting to insert new record.\n\t\tProduct : ' + @.cProduct + '\n\t\tSub-account : ' + @.cSubAccount + '\n\t\tCost : $' + @.cValue
RAISERROR(@.cError, 16, 1)
END

@.cValue is a varchar(20)
@.dValue is money

Any help?

Mike BI tried the following

SELECT CAST(@.dValue, AS VARCHAR) AS "cValue"

but I get the error "Error 156 : Incorrect syntaxt near keyword 'AS'"

Mike B|||I figured it out. I used the CAST

set @.cError = ............ + '\n\t\tCost : $' + CAST(@.dValue AS VARCHAR)

Mike B|||Originally posted by MikeB_2k4
I want to print in an error message a money value but have to convert it to a varchar first. I do not have any clue how to do this. Could someone help me out?

SELECT @.iError = @.@.error, @.iRowCount = @.@.rowcount
IF(@.iError <> 0 OR @.iRowCount <> 1)
BEGIN

Hey...looks vaguely familiar...

And I think you need an AND instead...

Why not break it up in to 2 clauses though...

That way you'll know it 1 of 2 types of errors...the first a system problem and the second a user error...

It's more coding, but you'll never be in doubt|||Originally posted by Brett Kaiser
Hey...looks vaguely familiar...

hehe, you didn't copyright it I hope? :)

And I think you need an AND instead...

I think OR is required because I am expecting only 1 row to be inserted. Therefore, if more or less then one row?

Why not break it up in to 2 clauses though...

Good Suggestion.

Thanks
Mike B

P.S., I will try to remember to give credit where credit is due next time I copy some of your code! ;)|||Well if @.@.Error = 0 and @.@.ROWCOUNT = 2, then your statement would be true, no?|||Originally posted by Brett Kaiser
Well if @.@.Error = 0 and @.@.ROWCOUNT = 2, then your statement would be true, no?

No, I don't even no why I am checking rowcount. Maybe just to confuse myself. The stored procedure inserts a record and only a single record.

I should really only be checking the error value probably, but due to lack of experience, I still don't trust the insertion, deletion etc.. statements in a SQL statement.

I have a habit of checking to ensure the record count is what is expected, nothing more, nothing less. Unnecessary but.....

Thanks for your input.

Mike B

In case your interested, here is the Stored proc

CREATE PROCEDURE usp_updateProductCost

@.dValue money,
@.cProduct varchar(10),
@.cSubAccount varchar(10)

AS

declare @.cError varchar(255), @.iError int, @.iRowCount int

if(EXISTS(SELECT * FROM tbProductCosting WHERE tbProductCosting.Product = @.cProduct AND tbProductCosting.SubAccount = @.cSubAccount))
BEGIN
Update tbProductCosting SET tbProductCosting.Cost = @.dValue WHERE tbProductCosting.Product = @.cProduct AND tbProductCosting.SubAccount = @.cSubAccount
SELECT @.iError = @.@.error

if(@.iError <> 0)
BEGIN
set @.cError = 'Error update cost for product ' + @.cProduct
RAISERROR(@.cError, 16, 1)
END
END
else
BEGIN
INSERT INTO tbProductCosting (Product, SubAccount, Cost) VALUES (@.cProduct, @.cSubAccount, @.dValue)
BEGIN
SELECT @.iError = @.@.error, @.iRowCount = @.@.rowcount
IF(@.iError <> 0 OR @.iRowCount <> 1)
BEGIN
set @.cError = 'Error attempting to insert new record.\n\t\tProduct : ' + @.cProduct + '\n\t\tSub-account : ' + @.cSubAccount + '\n\t\tCost : $' + CAST(@.dValue AS VARCHAR)
RAISERROR(@.cError, 16, 1)
END
END
END
GO|||I do see one major problem now if I look at it. Even if the record count was greater then 1, I don't handle it. I should be using transaction management. hmmmm

Mike B

Sunday, February 12, 2012

Convert decimals and money to fixed length varchar format.

I am trying to convert decimals and money data types to a universal format and convert to varchar. The format is:

+00.000000

The format must be exactly this format.
The sign must always be present.
There must always be two numbers to the left of the decimal.
There must always be six to the right.
No matter the value, round to six decimals.

Examples:

1.123 : +01.123000
-4 : -04.000000
.98672385 : +00.986724

I have tried every combo of Convert, Cast, Right, etc and every time it doesn't seem to work.

Any ideas?

It is recommended to do the formating in the presentation layer. Try:

select

case when sign(c1) < 0 then '-' else '+' end +

right('00' + str(abs(c1), 8, 6), 9)

from

(

select 1.123 as c1

union all

select -4

union all

select .98672385

) as t

AMB

|||Try this

Code Snippet

declare @.num decimal(18,6)

SET @.num = .98672385

SELECT FmtNum = CASE WHEN @.num < 0 THEN '-' ELSE '+' END +
RIGHT('00'+CONVERT(varchar(9),CONVERT(decimal(9,6),ABS(@.num))),9)

|||You rock.

Thanks. It works great.