sqlstored-proceduresvarcharuser-defined-typessql-convert

Convert varchar into numeric in SQL Server stored procedure


I'm writing a stored procedure for INSERT and I want to convert a varchar column into numeric so when I'm writing this code

CONVERT(NUMERIC(18,2), CAST([Outcost] AS FLOAT))

then it throws this error

Incorrect syntax near the keyword 'CONVERT'.

Here is my query

INSERT INTO dbo.UdtEquipmentsStaging
        ([Type], [ModelID], [AccessoryID], [Make], [Model], [Category], 
         [CategoryDescription], [Grouping], [Item], [RequiredModel], [SKU], [ProductNum],
         [Speed], [Description], [Cost], [Cost2], [Cost3], [Cost4], [Cost5], 
         CONVERT(NUMERIC(18,2), CAST([Outcost] AS FLOAT)), [MSRP], 
         [Discontinued], [PercentMSRP], [ServiceType], [ServiceTypeColor],
         [ConnectivityFee], [ConnectFee], [InstallFee], [Notes], [ManufactureNotes]
        , [Special1], [Special2], [Special3], [Special4], [Special5], [Special6], [Special7], [Special8], [Special9], [Special10], [Special11], [Special12], [Special13] 
        , [Special14], [Special15], [Special16], [Special17], [Special18], [Special19], [Special20], [Special21], [Special22], [Special23], [Special24], [Special25] 
        , [Special26], [Special27], [Special28], [Special29], [Special30], [Special31], [Special32], [Special33], [Special34], [Special35], [Special36], [Special37] 
        , [Special38], [Special39], [Special40])
    SELECT 
        [Type], [ModelID], [AccessoryID], [Make], [Model], [Category], 
        [CategoryDescription], [Grouping], [Item], [RequiredModel], [SKU], [ProductNum],
        [Speed], [Description], [Cost], [Cost2], [Cost3], [Cost4], [Cost5], 
        [Outcost], [MSRP], [Discontinued], [PercentMSRP], [ServiceType], [ServiceTypeColor],
        [ConnectivityFee], [ConnectFee], [InstallFee], [Notes], [ManufactureNotes],
        [Special1], [Special2], [Special3], [Special4], [Special5], [Special6], [Special7], [Special8], [Special9], [Special10], [Special11], [Special12], [Special13] 
        , [Special14], [Special15], [Special16], [Special17], [Special18], [Special19], [Special20], [Special21], [Special22], [Special23], [Special24], [Special25] 
        , [Special26], [Special27], [Special28], [Special29], [Special30], [Special31], [Special32], [Special33], [Special34], [Special35], [Special36], [Special37] 
        , [Special38], [Special39], [Special40]
    FROM 
        @Equipments

and this is exact error

Msg 102, Level 15, State 1, Procedure ImportEquipmentFile_UDT, Line 27
Incorrect syntax near 'Outcost'.


Solution

  • The column list in the insert contains only column names. The value goes in the select:

    INSERT INTO dbo.UdtEquipmentsStaging
            ( [Type], [ModelID], [AccessoryID], [Make], [Model], [Category], [CategoryDescription], [Grouping], [Item], [RequiredModel], [SKU], [ProductNum]
            , [Speed], [Description], [Cost], [Cost2], [Cost3], [Cost4], [Cost5], [Outcost], [MSRP], [Discontinued], [PercentMSRP], [ServiceType], [ServiceTypeColor]
            , [ConnectivityFee], [ConnectFee], [InstallFee], [Notes], [ManufactureNotes]
            , [Special1], [Special2], [Special3], [Special4], [Special5], [Special6], [Special7], [Special8], [Special9], [Special10], [Special11], [Special12], [Special13] 
            , [Special14], [Special15], [Special16], [Special17], [Special18], [Special19], [Special20], [Special21], [Special22], [Special23], [Special24], [Special25] 
            , [Special26], [Special27], [Special28], [Special29], [Special30], [Special31], [Special32], [Special33], [Special34], [Special35], [Special36], [Special37] 
            , [Special38], [Special39], [Special40])
     SELECT [Type], [ModelID], [AccessoryID], [Make], [Model], [Category], [CategoryDescription], [Grouping], [Item], [RequiredModel], [SKU], [ProductNum]
            , [Speed], [Description], [Cost], [Cost2], [Cost3], [Cost4], [Cost5], CONVERT(NUMERIC(18,2), CAST([Outcost] AS FLOAT)), [MSRP], [Discontinued], [PercentMSRP], [ServiceType], [ServiceTypeColor]
            , [ConnectivityFee], [ConnectFee], [InstallFee], [Notes], [ManufactureNotes]
            , [Special1], [Special2], [Special3], [Special4], [Special5], [Special6], [Special7], [Special8], [Special9], [Special10], [Special11], [Special12], [Special13] 
            , [Special14], [Special15], [Special16], [Special17], [Special18], [Special19], [Special20], [Special21], [Special22], [Special23], [Special24], [Special25] 
            , [Special26], [Special27], [Special28], [Special29], [Special30], [Special31], [Special32], [Special33], [Special34], [Special35], [Special36], [Special37] 
            , [Special38], [Special39], [Special40]
     FROM @Equipments