Author |
Topic |
danyeung
Posting Yak Master
102 Posts |
Posted - 2007-02-01 : 14:18:08
|
How do I quote the string in a dynamic SQL. For example, set WhereClause = ' WHERE IssueTitle like %' + @IssueTitle + '%'How do I add quotes befor and after the %s?Thanks.DanYeung |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-01 : 14:29:12
|
[code]declare @WhereClause varchar(1000), @issuetitle varchar(15)select @issuetitle = 'mark'set @WhereClause = ' WHERE IssueTitle like ''%' + @IssueTitle + '%'''print @WhereClause[/code]Peter LarssonHelsingborg, Sweden |
 |
|
danyeung
Posting Yak Master
102 Posts |
Posted - 2007-02-01 : 16:53:21
|
Thanks Peter,It works. But I applied it to the date and it didn't work.For example: declare @WhereClause as varchar(1000), SubmittedDate as datetimeset SubmittedDate = '01/01/2007'set @WhereClause = ' WHERE SubmittedDate = ''' + cast(@SubmittedDate as char(10)) + ''''Please advise. Thanks.DanYeung |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-02-01 : 17:04:53
|
Don't use cast. use Convertdeclare @WhereClause as varchar(1000),@SubmittedDate as datetimeset @SubmittedDate = '01/01/2007'set @WhereClause = ' WHERE SubmittedDate = ''' + cast(@SubmittedDate as char(10)) convert(varchar(10), @SubmittedDate, 112) + '''' KH |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-02-01 : 17:06:18
|
are you using exec() or sp_executesql ? KH |
 |
|
danyeung
Posting Yak Master
102 Posts |
Posted - 2007-02-01 : 17:21:59
|
Thanks KH,Actually, cast works too if I increase the cast size to char(12). One number was cut off if char(10). Thanks.DanYeung |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-02-01 : 17:41:18
|
Why do you need dynamic SQL for this?Why can't you just do this:SELECT ...FROM YourTableWHERE SubmittedDate LIKE '%' + @SubmittedDate + '%' Tara Kizer |
 |
|
danyeung
Posting Yak Master
102 Posts |
Posted - 2007-02-01 : 19:04:39
|
The stored procedure has 14 pass-in parameters. The where clause considers only the paremeters that have data passing in. So it looks like this:if @SubmittedDate <> '' begin @WhereClause = @WhereClause + ' AND SubmittedDate = ''' + cast(@SubmittedDate as char(10)) + '''' endif @IssueTitle <> '' begin @WhereClause = @WhereClause + ' AND IssueTitle like ''%' + @IssueTitle + '''' end.... and 12 more.DanYeung |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-02-01 : 19:52:50
|
quote: Originally posted by danyeung Thanks KH,Actually, cast works too if I increase the cast size to char(12). One number was cut off if char(10). Thanks.DanYeung
yes but with cast() you can't specify the style and the resulting date string format is depending on the system settings. If you need to convert date into string for comparison with a datatime datatype (as in your case), you should format it as YYYYMMDD. That's why i use convert() with style 112.Another question I ask is if you are using exec() or sp_executesql. With sp_executesql, you can pass the @SubmittedDate in as a parameter and hence you do not need to convert to string to form the sql string.you sql string can be like thisset @WhereClause = ' WHERE SubmittedDate = @SubmittedDate'-- complete sql stringset @sql = . . . . + @WhereClauseexec sp_executesql @sql, N'@SubmittedDate datetime', @SubmittedDate KH |
 |
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
|
danyeung
Posting Yak Master
102 Posts |
Posted - 2007-02-02 : 16:35:23
|
quote: Originally posted by tkizer You should be using COALESCE then instead. http://www.sqlteam.com/item.asp?ItemID=2077COALESCE allows you to have a dynamic WHERE clause without using dynamic SQL.Tara Kizer
It works if I the parameters are string. But it works differently when the parameters are date, integer, and other types. I will figure it out. Thanks.DanYeung |
 |
|
danyeung
Posting Yak Master
102 Posts |
Posted - 2007-02-02 : 16:36:37
|
quote: Originally posted by khtan
quote: Originally posted by danyeung Thanks KH,Actually, cast works too if I increase the cast size to char(12). One number was cut off if char(10). Thanks.DanYeung
yes but with cast() you can't specify the style and the resulting date string format is depending on the system settings. If you need to convert date into string for comparison with a datatime datatype (as in your case), you should format it as YYYYMMDD. That's why i use convert() with style 112.Another question I ask is if you are using exec() or sp_executesql. With sp_executesql, you can pass the @SubmittedDate in as a parameter and hence you do not need to convert to string to form the sql string.you sql string can be like thisset @WhereClause = ' WHERE SubmittedDate = @SubmittedDate'-- complete sql stringset @sql = . . . . + @WhereClauseexec sp_executesql @sql, N'@SubmittedDate datetime', @SubmittedDate KH
You are right. Thanks.DanYeung |
 |
|
|