I have stored 'getdate()' as text in one of my column in a table. (It's a complicated scenario ,how I saved it) Now when I select the field what I am getting is getdate() instead of the date . How can I convert this to date time.

I am adding a sample code to understand this

declare @a varchar(20)='getdate()'

select convert(datetime,@a)

This results in conversion error. How can I convert this to date?

  • You can't convert it to a date, you never saved the date in the first place. The data simply isn't there. You can calculate a new date as the answers show. Commented Jan 23, 2023 at 21:38

2 Answers 2


You can use a case statement.

declare @a varchar(20)='getdate()';
select case when @a = 'getdate()' then getdate() end;
  • Thanks @mikael 😀 Commented Jan 23, 2023 at 12:01

You can use dynamic SQL for this, however it is very prone to SQL Injection.

DECLARE @a VARCHAR(20) = 'getdate()';

EXEC ('SELECT convert(datetime,' + @a + ')');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.