title: SQL server Function - Take column name as input parameter
body: <p>I need to write a SQL function to return column specific values, so I am passing the column name as a parameter to SQL-function to return its corresponding value. Here is the sample function</p>
  
  <pre><code>CREATE FUNCTION GETDATETIME(@columnName VARCHAR(100)) 
  RETURNS DATETIME
  AS 
  BEGIN
      RETURN (SELECT TOP 1.@columnName  FROM TEST_TABLE ) 
  END
  GO
  </code></pre>
  
  <p>The above function seems to be straight forward, but it not working as expected.
  And when I execute the function</p>
  
  <pre><code>SELECT dbo.GETDATETIME('DATETIMECOLUMNNAME')
  </code></pre>
  
  <p>I am getting this error:</p>
  
  <blockquote>
    <p>Conversion failed when converting date and/or time from character string.</p>
  </blockquote>
  
  <p>Can someone help me to identify the issue?</p>