Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Data Conversion

Posted on 2000-03-17
5
Medium Priority
?
282 Views
Last Modified: 2008-03-17
I have a table having data stored in this format (varchar)
2000
699
1
00
3500
40000

I would like to convert it in this format (currency)
20.00
6.99
1.00
0.00
35.00
400.00
0
Comment
Question by:serigne
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 8

Expert Comment

by:chigrik
ID: 2630591
Try this:

USE pubs
GO

SET NOCOUNT ON
GO

if object_id('dbo.tbConverted') is not null drop table tbConverted
GO

CREATE TABLE tbConverted (
        ID int identity primary key,
        fldMoney varchar (20)
)
GO

insert into tbConverted VALUES ('2000')
insert into tbConverted VALUES ('699')
insert into tbConverted VALUES ('1')
insert into tbConverted VALUES ('00')
insert into tbConverted VALUES ('3500')
insert into tbConverted VALUES ('40000')
GO

select * from tbConverted
GO

ALTER TABLE tbConverted ADD fldMoney_new numeric(15,2)
GO

update tbConverted set fldMoney_new = convert(numeric(15,2), fldMoney) / 100
GO

ALTER TABLE tbConverted DROP COLUMN fldMoney
GO

sp_rename 'tbConverted.fldMoney_new', 'fldMoney'
GO

select * from tbConverted
GO

This is the results set:
"
ID          fldMoney            
----------- --------------------
1           2000
2           699
3           1
4           00
5           3500
6           40000

Caution: Changing any part of an object name could break scripts and stored procedures.
The column was renamed to 'fldMoney'.
ID          fldMoney          
----------- -----------------
1           20.00
2           6.99
3           .01
4           .00
5           35.00
6           400.00
"
0
 
LVL 4

Expert Comment

by:Gustavo Perez Buenrostro
ID: 2632624
serigne,
You can do what you want using this query:

update YourTable
   set YourColumn
      =case when isnumeric(YourColumn)=1
            then
            substring(convert(varchar,convert(decimal(15,2),YourColumn)/100,2)
                     ,1
                     ,charindex('.',convert(varchar,convert(decimal(15,2),YourColumn)/100,2))+2
                        )
            else YourColumn
       end
  from YourTable
0
 
LVL 1

Author Comment

by:serigne
ID: 2658465
gpbuenrostro

When I  run your update query I get this error"
Server: Msg 8152, Level 16, State 9, Line 2

String or binary data would be truncated.
The statement has been terminated
0
 
LVL 1

Author Comment

by:serigne
ID: 2658469
gpbuenrostro

When I  run your update query I get this error"
Server: Msg 8152, Level 16, State 9, Line 2

String or binary data would be truncated.
The statement has been terminated
0
 
LVL 4

Accepted Solution

by:
Gustavo Perez Buenrostro earned 400 total points
ID: 2662507
serigne,
Check query below and let me know if it works as you need, note you should be careful not to exceed column's string length.

PD: A column with data type varchar(5) was assumed.


update YourTable
   set YourColumn
      =case when isnumeric(YourColumn)=1 and datalength(YourColumn)<5
            then
            substring(convert(varchar,convert(decimal(15,2),YourColumn)/100,2)
                     ,1
                     ,charindex('.',convert(varchar,convert(decimal(15,2),YourColumn)/100,2))+2
                        )
            else YourColumn
       end
  from YourTable
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question