Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 varchar to money data type???

Author  Topic 

simflex
Constraint Violating Yak Guru

327 Posts

Posted - 2009-12-15 : 23:20:08
Greetings all,

I have a few name called TransAmount. I migrated the table containing this fieldname and host of others.

This TransAmount was defined as nvarchar(255) but I want to convert it to Money data type.

So far, there are over 300 records.

Some of the values are entered as follows:

34.00, -45.00, 75.00-, 60

Notice that in the above examples, I have periods (.), and dashes (-).

I have been manually removing the dashes and periods but I am sure there is a better, faster, more efficient way of messaging this till I can change the data type to Money.

Is there a script or a way to do this all at once?

For instance, is there a script that can remove any value that has a period, say 250.00 and convert that value to just 250

Also, is there a way to remove dashes from a value?

I hope I am clear in what I am asking for.
Your assistance is greatly appreciated.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-15 : 23:34:31
You shouldn't need to do any data cleaning except for the one with the negative sign at the end. The other ones you just need to use CONVERT.

Check this:

DECLARE @v1 nvarchar(255)

SET @v1 = '-45.00'

SELECT CONVERT(money, @v1)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

simflex
Constraint Violating Yak Guru

327 Posts

Posted - 2009-12-16 : 00:01:16
hi Tara,

I do have the dashes on both sides of the value though.

some show 139- and some show -23

Currently, I am using select cast(fieldname as money) from...

I am just trying to clean up the values.

Thanks a lot for your prompt response.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-16 : 00:05:43
What I'm saying is that you'll need to clean up the bad data first. Anything that can't be converted via CONVERT needs to be cleaned up first. 139- is an example that needs to be cleaned up first. Search for those use the RIGHT function and shift that negative sign to the beginning. Then run your CONVERT(money, ...).

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-12-16 : 01:57:03
To identify the bad data, use

select col from your_table
where col like '%[0-9]-%'

If you find

select cast(col as money) from your_table
where col not like '%[0-9]-%'


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

simflex
Constraint Violating Yak Guru

327 Posts

Posted - 2009-12-16 : 08:59:01
We are on the same page here Tara as far as cleaning data is concerned.

My issue is that I would prefer not to do them manually because there are several of them.

I was wondering if there is a script out there that can help me clean them in one fell swoop.

Thanks for your help.

madhivanan: Thanks for your help as well.

I can certainly use the first part of your code to identify data that needs scrubbing.

I just need to find the script that will change them in the db, rather then casting or converting which I can still use as extra protection.

Thanks to both of you for your assistance.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-12-16 : 09:08:32

Try

Begin Transaction

Update your_table
set col=substring(col,1,len(col)-1)
where col like '%[0-9]-%'


--See if this gives the data you want

select col from your_table

--If so, commit it

Commit Transaction

--If not, rollback it

Rollback transaction

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -