What is the best way of inserting a datetime value using a dynamic sql string, whilst at the same time being able to handle the possibility of the value being null?
The current statement inserts into a table from a select statement built using a string. The datetime value is stored in a parameter and the parameter is used in the select.
Like so:
SET @execsql = 'INSERT INTO ( start_date )
SELECT ( ''' + CAST(start_date as VARCHAR) + ''' + ')'
EXECUTE(@execsql)
As you’re running Sybase something like
Should do the trick
Here’s the SQL Server way for reference
Obviously, in this case you wouldn’t need dynamic SQL, but I assume this is part of a larger example.
As always with dynamic SQL questions, http://www.sommarskog.se/dynamic_sql.html is recommended reading.