Solved

trying to convert a str into a number in sql 2012

Posted on 2014-09-08
4
195 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:chrismichalczuk
  • 2
4 Comments
 
LVL 142

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:ScottPletcher
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:chrismichalczuk
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 142

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now