April 9, 2012 at 6:54 am
I'm trying to get the following output from the SQL Statement listed below, however I forgot how to handle the Quote '.
UPDATE ctl.Accounts_Payable SET insert_date = '2012-04-04'
I tried several variations but I did not get it right:
DECLARE @InsertDate VARCHAR(12)
SET @InsertDate = '2012-04-04'
SELECT 'UPDATE ' + s.Name + '.' +
t.name
+ ' SET insert_date = ' + @InsertDate
I get the following from the code listed above:
UPDATE ctl.Accounts_Payable SET insert_date = 2012-04-04
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
April 9, 2012 at 7:01 am
Try :
+ ' SET insert_date = ''' + @InsertDate + ''''
April 9, 2012 at 7:32 am
As an alternative...
+ ' SET insert_date = ' + QUOTENAME(@InsertDate,'''')
--Jeff Moden
Change is inevitable... Change for the better is not.
April 9, 2012 at 8:25 am
Jeff Moden (4/9/2012)
As an alternative...
+ ' SET insert_date = ' + QUOTENAME(@InsertDate,'''')
Be warned though that the parameter and return type of quotename() is sysname, i.e. any value you put in will -if it is longer than 128 characters- be truncated at 128 characters. It will do so silently, i.e. no truiincation warning or whatever will show. In this case, where the input is at most 12 characters long, the use of quotename() is perfectly valid.
April 9, 2012 at 8:29 am
Let me correct myself: in sql 2005 and earlier the value would be truncated, from 2008 the function will return null if the input is over 128 characters loing.
April 9, 2012 at 4:31 pm
True enough. I'm really hoping that a date/time wouldn't exceed 128 characters. 🙂
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply