convert date to varchar | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

convert date to varchar

I need help to convert date to varchar. SELECT @sql = ‘INSERT INTO ‘ + @ServerNamn + ” + @ServerTabell2 + ‘ (Arsnr, Id, SkapadDatum, Anvandare)
VALUES (‘ + convert(varchar, @tmpNoll) + ‘, ‘ + convert(varchar, @rad) + ‘, ‘ + left(convert(varchar, getdate(),120),10) + ‘, ”SY”)’
EXEC(@sql) The column that it´s going into is datetime. And the result gets "1905-05-24" in the table. I want todays date!
The errror is on "left(convert(varchar, getdate(),120),10)" in the string. Hope thats enough to get som help….please
Hi ya, make sure that you have quotes around the date, otherwise sql will see 2004-12-23 as being minus signs and results in 1969 days from 1900-1-1 which probably is about 1905 so make it
SELECT @sql = ‘INSERT INTO ‘ + @ServerNamn + ” + @ServerTabell2 + ‘ (Arsnr, Id, SkapadDatum, Anvandare)
VALUES (‘ + convert(varchar, @tmpNoll) + ‘, ‘ + convert(varchar, @rad) + ‘, ”’ + left(convert(varchar, getdate(),120),10) + ”’, ”SY”)’
EXEC(@sql)
on a more general note, dynamic sql like this is not good for security or performance… so tends to be best avoided in production applications Cheers
Twan

Thank you…it work fine now <img src=’/community/emoticons/emotion-1.gif’ alt=’:)‘ />
]]>