title: Can I make SQL Server FORMAT deterministic?
body: <p>I want to make a UDF which returns an integer form of <code>YYYYMM</code> so that I can easily partition some things on month. I am trying to assign this function to the value of a <code>PERSISTED</code> computed column.</p>
  
  <p>I currently have the following, which works fine:</p>
  
  <pre><code>CREATE FUNCTION dbo.GetYearMonth(@pDate DATETIME2)
  RETURNS INT
  WITH SCHEMABINDING
  AS
  BEGIN
      DECLARE @fYear VARCHAR(4) = RIGHT('0000' + CAST(YEAR(@pDate) AS VARCHAR),4)
      DECLARE @fMonth VARCHAR(2) = RIGHT('00' + CAST(MONTH(@pDate) AS VARCHAR),2)
  
      RETURN CAST(@fYear + @fMonth AS INT)
  END
  </code></pre>
  
  <p>But I think it's cleaner to use <code>FORMAT</code> instead. I tried this:</p>
  
  <pre><code>CREATE FUNCTION dbo.GetYearMonth(@pDate DATETIME2)
  RETURNS INT
  WITH SCHEMABINDING
  AS
  BEGIN
      DECLARE @fYear VARCHAR(4) = FORMAT(@pDate,'yyyy', 'en-us')
      DECLARE @fMonth VARCHAR(2) = FORMAT(@pDate,'MM', 'en-us')
  
      RETURN CAST(@fYear + @fMonth AS INT)
  END
  </code></pre>
  
  <p>But this function is nondeterministic.
  Is there a way to make <code>FORMAT</code> deterministic? Or is there a better way to do this, making the UDF deterministic?</p>