Solved

trying to convert a str into a number in sql 2012

Posted on 2014-09-08
4
200 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
[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
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

624 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