title: Type of column " " conflicts with the type of other columns specified in the UNPIVOT list
body: <p>I have written this query to dynamically find the max or min of numeric value from the entire tables. My query goes like this:</p>
  
  <pre><code>declare @col varchar(2000)
  
   set @col = STUFF( (select ',' +' [' + c.name + ']'  FROM  
      sys.columns c
    INNER JOIN 
      sys.types t ON c.user_type_id = t.user_type_id
   WHERE
     c.object_id = OBJECT_ID('JRR_20170301_Stg')
     and c.system_type_id = 108 for XML path('')),1,1, '')
  
     select @col -- This returns the column name and good.
  
   --However, the problem starts from here below
    declare @sql varchar(max)
  
   set @sql='SELECT  max(col) 
   FROM JRR_20170301_Stg unPIVOT (col for ListofColumns in ('+@col+')) as unpivat'
  
     exec (@sql)
  </code></pre>
  
  <p>Here is my table structure:</p>
  
  <pre><code>    CREATE TABLE [dbo].[JRR_20170301_Stg](
  [col1] [numeric](7, 0) NULL,
  [col2] [numeric](7, 0) NULL,
  [col3] [varchar](30) NULL,
  [TIMESTAMP1] [varchar](8) NULL,
  [BRANCH] [varchar](2) NULL,
  [COE] [varchar](8) NULL,
  [SCHEME] [varchar](3) NULL,
  [NO] [varchar](7) NULL,
  [PLAN] [varchar](5) NULL,
  [ACODE] [varchar](8) NULL,
  [ACTNAME] [varchar](30) NULL,
  [SRNAME] [varchar](30) NULL,
  [OTHERNAME] [varchar](30) NULL,
  [ACTWTCH] [varchar](1) NULL,
  [EMPLC] [varchar](5) NULL,
  [EMPLNO] [varchar](13) NULL,
  [LADATE] [numeric](7, 0) NULL,
  [EARN] [numeric](7, 2) NULL,
  [REGPA] [numeric](7, 2) NULL,
  [OTAMT] [numeric](7, 2) NULL,
  [EMPSTAT] [varchar](1) NULL,
  [RESVE5] [varchar](100) NULL
  ) ON [PRIMARY]
  </code></pre>
  
  <p>Is there any way to correct this error and get the desired result? Can I use cross apply or something?</p>