title: Conversion failed when converting date and/or time from character string CASE + CAST
body: <p>I'm a beginner to programming and I have this problem with a <code>CAST</code>.</p>
  
  <p>Here is my query:</p>
  
  <pre><code>DECLARE @adtdays INT
  
  SELECT 
      @adtdays = CASE 
                    WHEN DATEPART(MONTH, GETDATE()) &gt; DATEPART(MONTH, CONVERT(DATETIME, Day1)) 
                       THEN DATEPART(DAY, (DATEADD(DAY, -1, CAST(CAST(DATEPART(MONTH, GETDATE()) AS VARCHAR) + '/1/' + CAST(DATEPART(year, Day1) AS VARCHAR) AS DATE)))) 
                       ELSE 0 
                 END 
  FROM 
      ProgProdAlpidi 
  WHERE 
      ProductCode  = 'PRODUCT CODE' 
  </code></pre>
  
  <p>I get the following error </p>
  
  <blockquote>
    <p>Conversion failed when converting date and/or time from character string.</p>
  </blockquote>
  
  <p>Could someone please help me? </p>
  
  <p>Ok sorry im new,,, the desired result should be  :</p>
  
  <p>If the current month is greater than the month of the "Day1 field" then
    Remove one day to current month, 1, current year.
    That will bring me the last day of the previous month...</p>
  
  <p>that will be something like '4/30/2017'</p>
  
  <p>first row is columnHeader second is first the row of my table... </p>
  
  <pre><code>ProductCode     MY  Vehicle Project Dest    Line   Day1     Day2    Day3
  PRODUCT CODE    MY  VEHICLE PROJECT DEST.   LIN    42826    42827   42828
  </code></pre>
  
  <p>but seems the conversion im trying to do is wrong...</p>