Trying Convert and CAST is not changing "10.00000" to "10"

51 Views Asked by At

I have a table in which all columns are set as varchar. This is required because the data in the file contains multiple data types in the columns.

(PS I tried to load the data as a table, but I was not successful)

Table The first 6 lines are loaded from a file. The format of the file is required. I use the values in column 0 to build a dynamic SQL statement that I pass to a data flow task. The query below is the dynamic query built from rows 1-6 of the table. It is not the problem query.

SELECT  REQ_NUMBER
        ,LINE_NBR
        ,LTrim(Rtrim(ITEM)) AS ITEM
        ,[DESCRIPTION]
        ,LTrim(Rtrim(MANUF_NBR)) AS MANUF_NBR
        ,ENTERED_UOM
        ,Convert(varchar(5),Cast(QUANTITY AS Integer)) as newqty
        ,Convert(varchar(5),Cast(KILL_QUANTITY AS Integer)) as newkillqty
        ,Convert(varchar(5),Cast((QUANTITY - KILL_QUANTITY) AS Integer)) as newpickqty
        FROM [LSLMDB].[ls_apps].[REQLINE] 
        WHERE REQ_NUMBER LIKE 2286260  
        AND LINE_NBR <> 3 
        AND LINE_NBR <> 2 
        AND LINE_NBR <> 1 
        AND ITEM_TYPE like 'I' 
        and FILL_OR_KILL like 'K'

When I execute the sql above against the source database it returns the expected 10. But the data flow appends it to the table above, which shows 10.00000. When I select the rows from the table in the destination database using SSMS it shows 10.00000. So far nothing I have tried will change to 10.00000 to 10 when I extract the table to a flat file. I have tried the suggested ways from stack overflow posts. The problem query is:

SELECT 
      [Column 0]
      ,[Column 1]
      ,[Column 2]
      ,[Column 3]
      ,[Column 4]
      ,[Column 5] --making this result in 10,not 10.00000 in the flat file
      ,[Column 6]  --making this result in 10,not 10.00000 in the flat file
      ,[Column 6]  --making this result in 0,not 0.00000 in the flat file
      ,[Column 7]
      ,[Column 8]
      ,[Column 9]
      ,[Column 10]
      ,[Column 11]
      ,[Column 12]
      ,[Column 13]
      ,[Column 14]
      ,[Column 15]
      ,[Column 16]
      ,[Column 17]
  FROM [Voxware].[dbo].[VoxPackListIn]
--CONVERT(varchar(5),CAST([Column 6] AS Integer)) as [Column 6],
--CONVERT(VARCHAR(5),TRUNC(CAST([Column 5] AS NUMERIC)))
--CONVERT(varchar(5),CAST(ABS(QUANTITY) AS Integer)) AS QUANTITY
--CONVERT(VARCHAR(5),(CAST(ROUND([Column 5],0) AS NUMERIC)))
--Cast(QUANTITY AS Integer)

There are other iterations I'm sure, but I have been doing this for 2 days and didn't save what didn't work. The file always contains 10.00000.

1

There are 1 best solutions below

0
MaggieW On

The solution provided by Josh works. I use a case statement to determine if the conversion should be done.

SELECT 
      [Column 0]
      ,[Column 1]
      ,[Column 2]
      ,[Column 3]
      ,[Column 4]
      ,CASE
            when seqnum > 3 then CONVERT(VARCHAR(5),CONVERT(INTEGER,CONVERT(FLOAT,[Column 5]))) --making this result in 10,not 10.00000 in the flat file
            Else [column 5]
       End as newcol5
      ,CASE
            when seqnum > 3 then CONVERT(VARCHAR(5),CONVERT(INTEGER,CONVERT(FLOAT,[Column 6]))) --making this result in 10,not 10.00000 in the flat file
            Else [column 6]
       End as newcol6
      ,CASE
            when seqnum > 3 then CONVERT(VARCHAR(5),CONVERT(INTEGER,CONVERT(FLOAT,[Column 7]))) --making this result in 10,not 10.00000 in the flat file
            Else [column 7]
       End as newcol7
      
      ,[Column 7]
      ,[Column 8]
      ,[Column 9]
      ,[Column 10]
      ,[Column 11]
      ,[Column 12]
      ,[Column 13]
      ,[Column 14]
      ,[Column 15]
      ,[Column 16]
      ,[Column 17]
  FROM [Voxware].[dbo].[VoxPackListIn]