Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert char to numeric

Status
Not open for further replies.

rgb30b

Technical User
May 9, 2003
19
US
I need to convert a char(17) to numeric for a MSSQL view. In VB it goes like this

If PartType <> &quot;&quot; Then
mantissa = CInt(Mid(SideMillType, 5, 2))
num = CInt(Mid(SideMillType, 7, 2))
den = CInt(Mid(SideMillType, 9, 2))
Width = CDbl(mantissa + (num / den))
End If
WidthFromPartType = Width '42.625
'SideMillType record: B166420508N

Ive tried a few different things, but not having much luck. How do I convert B166420508N to be 42.625?
Thankx in advance...
 
Use the corresponding functions in T-SQL. See Books Online.
Try these pieces in Query Analyzer.
Code:
DECLARE @part_type CHAR(11)
DECLARE @mantissa NUMERIC(10,5)
DECLARE @num NUMERIC(10,5)
DECLARE @den NUMERIC(10,5)
DECLARE @result NUMERIC(10,5)

SET @part_type = 'B166420508N'
SET @mantissa = CAST( SUBSTRING(@part_type, 5,2) AS NUMERIC )
SET @num = CAST( SUBSTRING(@part_type, 7,2) AS NUMERIC )
SET @den = CAST( SUBSTRING(@part_type, 9,2) AS NUMERIC )
SET @result = @mantissa + ( @num / @den )

SELECT @mantissa, @num, @den,@result

Put it all together.
Code:
SELECT 
      CASE
        WHEN PartType <> &quot;&quot; THEN
           CAST( SUBSTRING(PartType, 5,2) AS NUMERIC(10,5) ) +
           ( CAST( SUBSTRING(PartType, 7,2) AS NUMERIC(10,5) ) /
             CAST( SUBSTRING(PartType, 9,2) AS NUMERIC(10,5) )
           )
        ELSE 0
      END AS &quot;Width&quot;
FROM myPartsTable
WHERE ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top