title: How to solve "Conversion failed when converting date and/or time from character string"
body: <p>I am creating a Table value function in which I am using a <code>DATEENTER</code> type field <code>Datetime</code>, but when I execute my function it generates the following error message:</p>
  <pre><code>Conversion failed when converting date and/or time from character string.
  </code></pre>
  <p>The function I am using in SQL Server is the following:</p>
  <pre><code>CREATE FUNCTION [dbo].[Function_TotalTravel](
      @YearTravel SMALLINT = 0,
      @MonthTravel SMALLINT = 0
  )
  RETURNS @Tabla TABLE (
      Travel VARCHAR(20),
      StatusTravel VARCHAR(20),
      DATEENTER DATETIME,
      IdStatusTravel INT
  )
  AS
  BEGIN
  
  INSERT INTO @Tabla (Travel,StatusTravel,DATEENTER,IdStatusTravel)
  SELECT  V.Travel,
          V.DATEENTER,
          
              CASE    WHEN V.TravelIncomplete = 1 THEN 'Travel Incomplete'
                      ELSE CASE 
                      WHEN V.StatusTravel = 2 THEN 'Transit'
                      WHEN V.StatusTravel = 3 THEN 'Closed'
                      WHEN V.StatusTravel = 4 THEN 'Cancel'
                      ELSE CAST(V.StatusTravel AS VARCHAR)
                      END END as 'StatusTravel',
              CASE    WHEN V.TravelIncomplete = 1 THEN 99
              ELSE    V.StatusTravel END IdStatusTravel
              
      FROM    TblTravel V WITH(NOLOCK)
      WHERE   (DATEPART(YearTravel, V.DATEENTER) = @YearTravel OR @YearTravel = 0)
      AND     (DATEPART(MONTH, V.DATEENTER) = @MonthTravel OR @MonthTravel = 0)
      AND  ((V.StatusTravel NOT IN (0,1) AND V.TravelIncomplete = 0) OR V.TravelIncomplete = 1)
  
      RETURN
  END
  </code></pre>
  <p>Try to perform a <code>CONVERT</code> on the select of the function as follows:</p>
  <pre><code>convert(varchar,DATEENTER),
  </code></pre>
  <p>But this one returns the same error message, any other solution that you can suggest.</p>