title: SQL Server 2008 passing datatype as parameter for function
body: <p>Since SQL Server 2008 does not have a Try_convert function I wanted to implement it for use in my Project. (no simply updating to a more recent version is not an option)</p>
  
  <p>Problem is: How can i pass a datatype as a parameter to my Function? </p>
  
  <p>Or </p>
  
  <p>More generic: What entry type does a variable need to be to be able to hold a datatype?</p>
  
  <p>Basically I want to build a Function like this:</p>
  
  <pre><code>CREATE FUNCTION my_Try_Convert
  (
      -- Add the parameters for the function here
      @value varchar(max),@entrytype type,
      @format int = NULL  
  )
  RETURNS typeof(@entrytype)
  AS
  BEGIN
      if @entrytype = typeof(datetime)
          if (select  ISDATE(@value))=1
              return convert(@entrytype,@value,@format)
      if @entrytype = typeof(int)     
          if (select  isnumeric(@value))=1
              return convert(@entrytype,@value,Null)
  END
  GO
  </code></pre>
  
  <p>What do I need to pass for @entrytypes type and for the typeOf psyodo-function?</p>