Solved

trying to convert a str into a number in sql 2012

Posted on 2014-09-08
4
198 Views
Last Modified: 2014-10-27
Select * from AUDIT_HEADER
where type  like ('SC%') and INV_REF_NUMERIC is NULL
go

update AUDIT_HEADER
set INV_REF_NUMERIC = (select CASE WHEN ISNUMERIC('Inv_ref') = 1 THEN CAST('Inv_ref' AS INT) ELSE NULL END)
go

how do I get any number only that exists in INV_REF (varchar (30) and put it into INV_REF_NUMERIC (int)

this doesnt seem to work?
0
Comment
Question by:Chris Michalczuk
  • 2
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40309553
this should do:
update AUDIT_HEADER
set INV_REF_NUMERIC = CAST(Inv_ref AS INT) 
WHERE ISNUMERIC(Inv_ref) = 1

Open in new window

0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40310181
When you put quotes around the column name, it becomes a literal and no longer a column name.

For example, if you did this:
SELECT 'Inv_ref' FROM AUDIT_HEADER
the output would be "inv_ref" over and over, and not the value in that column.

Therefore, you want to do something like this:

update AUDIT_HEADER
set INV_REF_NUMERIC = CAST(Inv_ref as int)
WHERE inv_ref NOT LIKE '%[^0-9]%' --only digits 0 thru 9 (integer value); add '-' if the value can be neg.
0
 

Author Comment

by:Chris Michalczuk
ID: 40318545
update SLXAddressesMasterCleaned
  set zip2 =  CAST(Postalcode as int)
WHERE Postalcode NOT LIKE '%[^0-9]%' --only digits 0 thru 9 (integer value);

Postalcode     ZIP2
16280             16280
C1022AAR       returned nothing  I wanted it to return 1022

basically I want to return any numbers that exist in postalcode into zip2 only
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 40318589
this is also possible, check this previous solved question to get a simple helper function to do that:
http://www.experts-exchange.com/Programming/Microsoft_Development/Q_27682377.html
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

828 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