problem in pattern matching..http://forums.asp.net/t/1319861.aspx/1?problem+in+pattern+matching+Sun, 21 Sep 2008 18:05:04 -040013198612620279http://forums.asp.net/p/1319861/2620279.aspx/1?problem+in+pattern+matching+problem in pattern matching.. <p>&nbsp;I store jobs in table and display them as per conditions selected by user.</p> <p>my table has 4 columns.</p> <p><b>company&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; role &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; Location &nbsp;&nbsp; postdate</b></p> <p>abc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Fresher&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; Ny &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9/10/2008 12:00:00 AM</p> <p>xyz &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; Vp-operations , &nbsp; LA&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9/10/2008 12:03:00 AM</p> <p>&nbsp;</p> <p>Now,&nbsp; this is my select query</p> <p>SELECT Companyname, role, Location, date FROM callcenter where Location Like '%' &#43; @city &#43; '%'&nbsp; and DATEDIFF(Day, postdate, getdate( ) ) &lt;= @datediff and Role Like '%' &#43; @role &#43; '%'&nbsp;&nbsp;&nbsp; </p> <p>&nbsp;</p> <p><b>my problem is</b> , for searching jobs, i allow user to select multiple roles. <b>I get results when choose just single role. But if i select multiple roles, I don't get any results</b>. Is there something wrong with </p> <p><b>Role Like '%' &#43; @role &#43; '%'&nbsp;&nbsp;</b>&nbsp; ???</p> <p>&nbsp;I have defined @role varchar(100) and I am passing value to it as</p> <p>cmd.Parameters.AddWithValue(&quot;@role&quot;, moreabout)&nbsp;</p> <p>If i select 2 roles, Freshers and Vp-operations , variable moreabout holds &quot;Fresher,Vp-operations&quot; </p> <p>then where is the problem ? <b>( I am using pattern matching , so it should show results. It shows result</b></p> <p><b>if i select just Vp-operations or just Freshers )</b> <br> </p> 2008-09-14T13:18:55-04:002620286http://forums.asp.net/p/1319861/2620286.aspx/1?Re+problem+in+pattern+matching+Re: problem in pattern matching.. <p>&nbsp;SELECT Companyname, role, Location, date FROM callcenter where Location Like '%' &#43; @city &#43; '%'&nbsp; and DATEDIFF(Day, postdate, getdate( ) ) &lt;= @datediff and Role IN (Fresher,Vp-operations)</p> 2008-09-14T13:34:38-04:002620296http://forums.asp.net/p/1319861/2620296.aspx/1?Re+problem+in+pattern+matching+Re: problem in pattern matching.. <p></p> <blockquote><span class="icon-blockquote"></span> <h4>anonymouswrites</h4> <p></p> <p>&nbsp;SELECT Companyname, role, Location, date FROM callcenter where Location Like '%' &#43; @city &#43; '%'&nbsp; and DATEDIFF(Day, postdate, getdate( ) ) &lt;= @datediff and Role IN (Fresher,Vp-operations)</p> <p></p> </blockquote> &nbsp; <p></p> <p><b>But there are not only 2 roles</b> viz. Fresher and&nbsp; Vp-operations . There are more than 10 roles.</p> <p>and i cannot determine which roles user will select , so passing those values to stored procedure is again difficult task.</p> <p>But whats wrong with my query? I am using <b>pattern matching</b> ..Do i have to modify pattern matching?<br> </p> 2008-09-14T13:48:45-04:002620311http://forums.asp.net/p/1319861/2620311.aspx/1?Re+problem+in+pattern+matching+Re: problem in pattern matching.. <p>&nbsp;in that table, in 1 row there will be only role or multiple roles. </p> <p>if it is one role i am giving query now. if there will be multiple roles, tomorrow i will give you solution as i am not at my system now.</p> <p>if you have split userdefined function fn_split(), then we can do like</p> <p>select * from (select value,c.* from company c cross join fn_split(@Roles,','))t where charindex (value,role) &gt; 0<br> </p> <p>&nbsp;</p> <p>&nbsp;</p> 2008-09-14T14:05:56-04:002620327http://forums.asp.net/p/1319861/2620327.aspx/1?Re+problem+in+pattern+matching+Re: problem in pattern matching.. <p></p> <blockquote><span class="icon-blockquote"></span> <h4>ramireddyindia</h4> <p></p> <p>&nbsp;in that table, in 1 row there will be only role or multiple roles. </p> <p>if it is one role i am giving query now. if there will be multiple roles, tomorrow i will give you solution as i am not at my system now.</p> <p>if you have split userdefined function fn_split(), then we can do like</p> <p>select * from (select value,c.* from company c cross join fn_split(@Roles,','))t where charindex (value,role) &gt; 0<br> </p> <p>&nbsp;</p> </blockquote> &nbsp; <p></p> <p>Thanks rami reddy ..</p> <p>Each row of table holds just single role...</p> <p>Please modify my original query if possible as I want to use it in stored procedure..</p> <p>Also, fn_split() is in-built function?<br> </p> <p>&nbsp;<br> </p> 2008-09-14T14:29:22-04:002620339http://forums.asp.net/p/1319861/2620339.aspx/1?Re+problem+in+pattern+matching+Re: problem in pattern matching.. <p>I hope you already have fn_split() user defined function. you need that function before you run this query. if it is not at you i will give you tomorrow as i don't have development environment now.<br> </p> <p>&nbsp;</p> <p>select&nbsp; Companyname, role, Location, date from (SELECT Companyname, role, Location, date,value FROM callcenter cross join fn_split(@role,',')) t where Location Like '%' &#43; @city &#43; '%'&nbsp; and DATEDIFF(Day, postdate, getdate( ) ) &lt;= @datediff and charindex(value,role) &gt; 0<br> </p> 2008-09-14T14:36:25-04:002620342http://forums.asp.net/p/1319861/2620342.aspx/1?Re+problem+in+pattern+matching+Re: problem in pattern matching.. <p>Create the comma separated string of role from the client side and use dynamic sql to achieve this.</p> <p>Let us assume your roles string contains the value 'VP-Operations,Fresher,Administrator'</p> <p>DECLARE @SQL NVARCHAR(1000)</p> <p>SET @SQL = 'SELECT Companyname, role, Location, date FROM callcenter where Location Like '''%''' &#43; @city &#43; '''%'''&nbsp; and DATEDIFF(Day, postdate, getdate( ) ) &lt;= @datediff and Role&nbsp;in (' &nbsp;&#43; @role &#43; ')'</p> <font color="#0000ff" size="2"> <p>EXEC</font><font size="2">&nbsp;</font><font color="#800000" size="2">SP_EXECUTESQL</font><font size="2"> @SQL </p> </font> 2008-09-14T14:40:01-04:002620352http://forums.asp.net/p/1319861/2620352.aspx/1?Re+problem+in+pattern+matching+Re: problem in pattern matching.. <p></p> <blockquote><span class="icon-blockquote"></span> <h4>ramireddyindia</h4> I hope you already have fn_split() user defined function.</blockquote> &nbsp; <p></p> <p>Ok..i will wait..as I don't know whats that...In fact I am hearing it for the first time..</p> <p>Thanks anyways.. <br> </p> 2008-09-14T14:50:40-04:002620361http://forums.asp.net/p/1319861/2620361.aspx/1?Re+problem+in+pattern+matching+Re: problem in pattern matching.. <p></p> <blockquote><span class="icon-blockquote"></span> <h4>vijayakrishna</h4> <p></p> <p>Create the comma separated string of role from the client side and use dynamic sql to achieve this.</p> <p>Let us assume your roles string contains the value 'VP-Operations,Fresher,Administrator'</p> <p>DECLARE @SQL NVARCHAR(1000)</p> <p>SET @SQL = 'SELECT Companyname, role, Location, date FROM callcenter where Location Like '''%''' &#43; @city &#43; '''%'''&nbsp; and DATEDIFF(Day, postdate, getdate( ) ) &lt;= @datediff and Role&nbsp;in (' &nbsp;&#43; @role &#43; ')'</p> <font color="#0000ff" size="2"></font> <p><font color="#0000ff" size="2">EXEC</font><font size="2">&nbsp;</font><font color="#800000" size="2">SP_EXECUTESQL</font><font size="2"> @SQL </font></p> <p><font size="2"></font></p> </blockquote> &nbsp; <p></p> <p>Actually I am using this query to bind datalist dynamically. In such case I dont know from where to call</p> <p>&nbsp;<font color="#0000ff" size="2">EXEC</font><font size="2">&nbsp;</font><font color="#800000" size="2">SP_EXECUTESQL</font><font size="2"> @SQL </font></p> <p>&nbsp;</p> <p>Dim cmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(&quot;dbo.bporesults&quot;)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.CommandType = Data.CommandType.StoredProcedure<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&quot;@datediff&quot;, beforedays)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&quot;@city&quot;, city1)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&quot;@role&quot;, moreabout)<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Connection = objConn<br> <br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim ds As New System.Data.DataSet<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim da As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter(cmd)<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; da.Fill(ds, &quot;callcenter&quot;)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataList1.DataSource = ds<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataList1.DataBind() <br> </p> 2008-09-14T15:01:43-04:002622691http://forums.asp.net/p/1319861/2622691.aspx/1?Re+problem+in+pattern+matching+Re: problem in pattern matching.. <p>&nbsp;anybody know this?...<br> </p> 2008-09-15T18:02:17-04:002623419http://forums.asp.net/p/1319861/2623419.aspx/1?Re+problem+in+pattern+matching+Re: problem in pattern matching.. <font size="2">cmd.Parameters.AddWithValue(</font><font color="#800000" size="2">&quot;@role&quot;</font><font size="2">, <strong>moreabout</strong>)</font><font size="2"> <p></font>I assume that <strong>moreabout </strong>is a comma separated value of roles like&nbsp; &quot;VP-Operations, Fresher&quot;.</p> <p>Your dbo.bporesults&nbsp; stored procedure can&nbsp;look something like</p> <font size="2"> <p><font size="2"><span class="sqlkwd">CREATE</span> STOR <span class="sqlkwd">PROC&nbsp;dbo.bporesults</span>&nbsp;<span class="sqlkwd2">datediff</span> <span class="sqlkwd">int</span>, city <span class="sqlkwd">varchar</span>(32), role <span class="sqlkwd">varchar</span>(200)<br> <span class="sqlkwd">AS<br> BEGIN</span><br> <span class="sqlcmt">--Other code required if any<br> </span><span class="sqlkwd">DECLARE</span> @SQL <span class="sqlkwd">NVARCHAR</span>(1000)<br> <span class="sqlkwd">SET</span> @SQL = <span class="st">'SELECT Companyname, role, Location, date FROM callcenter where Location Like '''</span>%<span class="st">''' &#43; @city &#43; '''</span>%<span class="st">'''&nbsp; and DATEDIFF(Day, postdate, getdate( ) ) &lt;= @datediff and Role in ('</span>&nbsp; &#43; @role &#43; '<span class="st">)'</span><br> <span class="sqlkwd">EXEC</span> <span class="sqlkwd4">SP_EXECUTESQL</span> @SQL <br> <span class="sqlkwd">END</span></p> </font></font> 2008-09-16T04:00:45-04:002627015http://forums.asp.net/p/1319861/2627015.aspx/1?Re+problem+in+pattern+matching+Re: problem in pattern matching.. <p></p> <blockquote><span class="icon-blockquote"></span> <h4>vijayakrishna</h4> <font size="2">cmd.Parameters.AddWithValue(</font><font color="#800000" size="2">&quot;@role&quot;</font><font size="2">, <strong>moreabout</strong>)</font><font size="2"> </font> <p></p> <p><font size="2"></font>I assume that <strong>moreabout </strong>is a comma separated value of roles like&nbsp; &quot;VP-Operations, Fresher&quot;.</p> <p>Your dbo.bporesults&nbsp; stored procedure can&nbsp;look something like</p> <p><font size="2"></p> <p><font size="2"><span class="sqlkwd">CREATE</span> STOR <span class="sqlkwd">PROC&nbsp;dbo.bporesults</span>&nbsp;<span class="sqlkwd2">datediff</span> <span class="sqlkwd">int</span>, city <span class="sqlkwd">varchar</span>(32), role <span class="sqlkwd">varchar</span>(200)<br> <span class="sqlkwd">AS<br> BEGIN</span><br> <span class="sqlcmt">--Other code required if any<br> </span><span class="sqlkwd">DECLARE</span> @SQL <span class="sqlkwd">NVARCHAR</span>(1000)<br> <span class="sqlkwd">SET</span> @SQL = <span class="st">'SELECT Companyname, role, Location, date FROM callcenter where Location Like '''</span>%<span class="st">''' &#43; @city &#43; '''</span>%<span class="st">'''&nbsp; and DATEDIFF(Day, postdate, getdate( ) ) &lt;= @datediff and Role in ('</span>&nbsp; &#43; @role &#43; '<span class="st">)'</span><br> <span class="sqlkwd">EXEC</span> <span class="sqlkwd4">SP_EXECUTESQL</span> @SQL <br> <span class="sqlkwd">END</span></font></p> <font size="2"></font></font></blockquote> &nbsp; <p></p> <p>&nbsp;</p> <p>Yes ..variable moreabout holds comma seperated values</p> <p>seeing your post I wrote stored procedure</p> <p><b>CREATE PROCEDURE dbo.bporesults <br> &nbsp;&nbsp;&nbsp; (<br> &nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; @datediff int ,<br> &nbsp;&nbsp;&nbsp; @city varchar(20),<br> &nbsp;&nbsp;&nbsp; @role varchar(100)<br> &nbsp;&nbsp;&nbsp; @SQL NVARCHAR(1000)<br> &nbsp;&nbsp;&nbsp; )<br> &nbsp;&nbsp;&nbsp; <br> AS</b></p> <p><b><br> </b>&nbsp;&nbsp;&nbsp; BEGIN<br> &nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; <b>SET @SQL='SELECT Companyname, Role, Moreaboutjob, candidate,Experience,Location,salarymin,salarymax,Interviewdate,postdate FROM callcenter where Location Like '%' &#43; @city &#43; '%'&nbsp; and DATEDIFF(Day, postdate, getdate( ) ) &lt;= @datediff and Role in (' &#43; @role &#43; ')'&nbsp; <br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; EXEC SP_EXECUTESQL @SQL</b></p> <p><br> &nbsp;&nbsp;&nbsp; END </p> <p>&nbsp;</p> <p>But I am not able to save it ( hence some syntax error is there).. <br> </p> 2008-09-17T12:08:14-04:002629814http://forums.asp.net/p/1319861/2629814.aspx/1?Re+problem+in+pattern+matching+Re: problem in pattern matching.. <font color="#0000ff" size="2"> <p>Now @role has&nbsp;a value&nbsp;<strong>VP-Operations,Fresher</strong>. But we require it as <strong>'VP-Operations','Fresher' </strong>to use it IN constraint. To achieve this we use the following statement. Here we added single quotes to the current value and replaced all the commas (,) with (single quote &#43; comma &#43; sigle quote)&nbsp;',' </p> <font color="#0000ff" size="2">SET</font><font color="#000000" size="2"> @role </font> <font color="#808080" size="2">=</font><font color="#000000" size="2"> </font><font color="#ff0000" size="2">''''</font><font color="#000000" size="2"> </font><font color="#808080" size="2">&#43;</font><font color="#000000" size="2"> </font> <font color="#ff00ff" size="2">REPLACE</font><font color="#808080" size="2">(</font><font color="#000000" size="2">@role</font><font color="#808080" size="2">,</font><font color="#ff0000" size="2">','</font><font color="#808080" size="2">,</font><font color="#ff0000" size="2">''','''</font><font color="#808080" size="2">)</font><font color="#000000" size="2"> </font><font color="#808080" size="2">&#43;</font><font color="#000000" size="2"> </font> <font color="#ff0000" size="2">''''</font></font><font color="#ff0000" size="2"> <p></font>&nbsp;The SET @SQL statement would be </p> <p>SET<font size="2"> @SQL</font><font color="#808080" size="2">=</font><font color="#ff0000" size="2">'SELECT Companyname, Role, Moreaboutjob, candidate,Experience,Location,salarymin,salarymax,Interviewdate,postdate FROM callcenter where Location Like ''%'</font><font size="2"> </font><font color="#808080" size="2">&#43;</font><font size="2"> @city </font><font color="#808080" size="2">&#43;</font><font size="2"> </font><font color="#ff0000" size="2">'%'' and DATEDIFF(Day, postdate, getdate( ) ) &lt;= @datediff and Role in ('</font><font size="2"> </font><font color="#808080" size="2">&#43;</font><font size="2"> @role </font><font color="#808080" size="2">&#43;</font><font size="2"> </font><font color="#ff0000" size="2">')'</font><font size="2"> </p> </font> 2008-09-18T12:53:21-04:002630046http://forums.asp.net/p/1319861/2630046.aspx/1?Re+problem+in+pattern+matching+Re: problem in pattern matching.. <p></p> <blockquote><span class="icon-blockquote"></span> <h4>vijayakrishna</h4> <font color="#0000ff" size="2"> <p>Now @role has&nbsp;a value&nbsp;<b>VP-Operations,Fresher</b>. But we require it as <b>'VP-Operations','Fresher' </b>to use it IN constraint. To achieve this we use the following statement. Here we added single quotes to the current value and replaced all the commas (,) with (single quote &#43; comma &#43; sigle quote)&nbsp;',' </p> <font color="#0000ff" size="2">SET</font><font color="#000000" size="2"> @role </font> <font color="#808080" size="2">=</font><font color="#000000" size="2"> </font><font color="#ff0000" size="2">''''</font><font color="#000000" size="2"> </font><font color="#808080" size="2">&#43;</font><font color="#000000" size="2"> </font> <font color="#ff00ff" size="2">REPLACE</font><font color="#808080" size="2">(</font><font color="#000000" size="2">@role</font><font color="#808080" size="2">,</font><font color="#ff0000" size="2">','</font><font color="#808080" size="2">,</font><font color="#ff0000" size="2">''','''</font><font color="#808080" size="2">)</font><font color="#000000" size="2"> </font><font color="#808080" size="2">&#43;</font><font color="#000000" size="2"> </font> <font color="#ff0000" size="2">''''</font></font><font color="#ff0000" size="2"> </font> <p></p> <p><font color="#ff0000" size="2"></font>&nbsp;The SET @SQL statement would be </p> <p>SET<font size="2"> @SQL</font><font color="#808080" size="2">=</font><font color="#ff0000" size="2">'SELECT Companyname, Role, Moreaboutjob, candidate,Experience,Location,salarymin,salarymax,Interviewdate,postdate FROM callcenter where Location Like ''%'</font><font size="2"> </font><font color="#808080" size="2">&#43;</font><font size="2"> @city </font><font color="#808080" size="2">&#43;</font><font size="2"> </font><font color="#ff0000" size="2">'%'' and DATEDIFF(Day, postdate, getdate( ) ) &lt;= @datediff and Role in ('</font><font size="2"> </font><font color="#808080" size="2">&#43;</font><font size="2"> @role </font><font color="#808080" size="2">&#43;</font><font size="2"> </font><font color="#ff0000" size="2">')'</font></p> <p></p> </blockquote> &nbsp; <p></p> <p>&nbsp;Hi,</p> <p>Please let me know where I am going wrong .. I copied your code only but not able to save the stored procedure</p> <p><b><u>GOT ERRORS</u><br> <br> Incorrect syntax nr @SQL<br> Must declare scalar variable @role.</b><br> </p> <p><u><b>Here is my stored procedure</b></u> :<br> </p> <p>ALTER PROCEDURE dbo.bporesults <br> &nbsp;&nbsp;&nbsp; (<br> &nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; @datediff int ,<br> &nbsp;&nbsp;&nbsp; @city varchar(20),<br> &nbsp;&nbsp;&nbsp; @role varchar(100)<br> &nbsp;&nbsp;&nbsp; @SQL NVARCHAR(1000)<br> &nbsp;&nbsp;&nbsp; )<br> &nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; <br> AS<br> &nbsp;&nbsp;&nbsp; SET @role = '''' &#43; REPLACE(@role,',',''',''') &#43; ''''<br> &nbsp;&nbsp;&nbsp; SET @SQL='SELECT Companyname, Role, Moreaboutjob, candidate,Experience,Location,salarymin,salarymax,Interviewdate,postdate FROM callcenter where Location Like ''%' &#43; @city &#43; '%'' and DATEDIFF(Day, postdate, getdate( ) ) &lt;= @datediff and Role in (' &#43; @role &#43; ')'<br> &nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; RETURN<br> &nbsp;</p> <p>Also, where should I write <br> </p> <p><font color="#0000ff" size="2">EXEC</font><font size="2">&nbsp;</font><font color="#800000" size="2">SP_EXECUTESQL</font><font size="2"> @SQL&nbsp;&nbsp;&nbsp;&nbsp; ( as i told you I am binding my datalist dynamically using this query..please check my previous posts)<br> </font></p> <p>&nbsp;</p> <p>&nbsp;</p> 2008-09-18T13:58:27-04:002630111http://forums.asp.net/p/1319861/2630111.aspx/1?Re+problem+in+pattern+matching+Re: problem in pattern matching.. <p>Your SP would be </p> <p>&nbsp;<pre class="prettyprint">ALTER PROCEDURE dbo.bporesults ( @datediff int , @city varchar(20), @role varchar(100) ) AS DECLARE @SQL NVARCHAR(1000) SET @role = '''' &#43; REPLACE(@role,',',''',''') &#43; '''' SET @SQL='SELECT Companyname, Role, Moreaboutjob, candidate,Experience,Location,salarymin,salarymax,Interviewdate,postdate FROM callcenter where Location Like ''%' &#43; @city &#43; '%'' and DATEDIFF(Day, postdate, getdate( ) ) &lt;= @datediff and Role in (' &#43; @role &#43; ')' EXEC sp_executesql @SQL</pre>&nbsp;</p> 2008-09-18T14:19:31-04:002630169http://forums.asp.net/p/1319861/2630169.aspx/1?Re+problem+in+pattern+matching+Re: problem in pattern matching.. <p>&nbsp;<pre class="prettyprint">--select * from dbo.FN_SPLIT('Vijaya,Krishna,Paruchuri') CREATE FUNCTION FN_SPLIT (@Val VARCHAR(MAX)) RETURNS @Table1 TABLE (SplitItem VARCHAR(100)) AS --This function expects a comma separated string --and it splits the values on comma basis --and returns a table having a column SplitItem BEGIN DECLARE @ValTemp VARCHAR(100) DECLARE @ValT VARCHAR(100) DECLARE @Start int DECLARE @End int IF SUBSTRING(@VaL,LEN(@Val),1) &lt;&gt; ',' SET @Val = @Val &#43; ',' WHILE CHARINDEX(',',@Val)&gt;0 BEGIN SET @Start = CHARINDEX(',',@Val) SET @End = LEN(@Val) - @Start SET @ValT = SUBSTRING(@Val,1,@start-1) SET @Val = SUBSTRING(@Val,@start&#43;1,@End) INSERT INTO @Table1 (SplitItem) VALUES (@ValT) END RETURN END</pre>&nbsp;</p> 2008-09-18T14:32:55-04:002630193http://forums.asp.net/p/1319861/2630193.aspx/1?Re+problem+in+pattern+matching+Re: problem in pattern matching.. <p></p> <blockquote><span class="icon-blockquote"></span> <h4>vijayakrishna</h4> <span class="sqlkwd">ALTER PROCEDURE</span> dbo.bporesults ( @<span class="sqlkwd2">datediff</span> <span class="sqlkwd">int</span> , @city <span class="sqlkwd">varchar</span>(20), @role <span class="sqlkwd">varchar</span>(100) ) <span class="sqlkwd">AS DECLARE</span> @SQL <span class="sqlkwd">NVARCHAR</span>(1000) <span class="sqlkwd">SET</span> @role = <span class="st">''''</span> &#43; <span class="sqlkwd">REPLACE</span>(@role,<span class="st">','</span>,<span class="st">''','''</span>) &#43; <span class="st">''''</span> <span class="sqlkwd">SET</span> @SQL=<span class="st">'SELECT Companyname, Role, Moreaboutjob, candidate,Experience,Location,salarymin,salarymax,Interviewdate,postdate FROM callcenter where Location Like ''%'</span> &#43; @city &#43; <span class="st">'%'' and DATEDIFF(Day, postdate, getdate( ) ) &lt;= @datediff and Role in ('</span> &#43; @role &#43; <span class="st">')'</span> <span class="sqlkwd">EXEC</span> <span class="sqlkwd4"> sp_executesql</span> @SQL</blockquote> &nbsp; <p></p> <p>Thanks..now I am able to save my procedure</p> <p>But while execution I got error <span></span></p> <h2><i><i>Must declare the scalar variable &quot;@datediff&quot;.</i></i></h2> <p>&nbsp;</p> 2008-09-18T14:39:29-04:002630246http://forums.asp.net/p/1319861/2630246.aspx/1?Re+problem+in+pattern+matching+Re: problem in pattern matching.. <p></p> <blockquote><span class="icon-blockquote"></span> <h4>vijayakrishna</h4> If you want fn_split try this,</blockquote> <p></p> <p>First , i think its better to complete your previous way...as Swithcing different ways would be time consuming</p> <p>If that does not work I will try this..</p> <p>Thanks for this post though..</p> <p>also, please tell me why i am getting that error when i am mentioning &nbsp;&nbsp;&nbsp;<b> @datediff int </b>in stored procedure?<br> </p> <p>ALTER PROCEDURE dbo.bporesults <br> (<br> &nbsp;&nbsp;&nbsp; @datediff int ,<br> &nbsp;&nbsp;&nbsp; @city varchar(20),<br> &nbsp;&nbsp;&nbsp; @role varchar(100)<br> )<br> AS<br> &nbsp;&nbsp;&nbsp; DECLARE @SQL NVARCHAR(1000)<br> &nbsp;&nbsp;&nbsp; SET @role = '''' &#43; REPLACE(@role,',',''',''') &#43; ''''<br> &nbsp;&nbsp;&nbsp; SET @SQL='SELECT Companyname, Role, Moreaboutjob, candidate,Experience,Location,salarymin,salarymax,Interviewdate,postdate FROM callcenter where Location Like ''%' &#43; @city &#43; '%'' and DATEDIFF(Day, postdate, getdate( ) ) &lt;= @datediff and Role in (' &#43; @role &#43; ')'<br> &nbsp;&nbsp;&nbsp; EXEC sp_executesql @SQL <br> </p> 2008-09-18T14:55:08-04:002630256http://forums.asp.net/p/1319861/2630256.aspx/1?Re+problem+in+pattern+matching+Re: problem in pattern matching.. <font color="#0000ff" size="2"> <p>SET</font><font size="2"> @SQL</font><font color="#808080" size="2">=</font><font color="#ff0000" size="2">'SELECT Companyname, Role, Moreaboutjob, candidate,Experience,Location,salarymin,salarymax,Interviewdate,postdate FROM callcenter where Location Like ''%'</font><font size="2"> </font><font color="#808080" size="2">&#43;</font><font size="2"> @city </font><font color="#808080" size="2">&#43;</font><font size="2"> </font><font color="#ff0000" size="2">'%'' and DATEDIFF(Day, postdate, getdate( ) ) &lt;='''</font><font size="2"> </font><font color="#808080" size="2">&#43;</font><font size="2"> @datediff </font><font color="#808080" size="2">&#43;</font><font size="2"> </font><font color="#ff0000" size="2">''' and Role in ('</font><font size="2"> </font> <font color="#808080" size="2">&#43;</font><font size="2"> @role </font><font color="#808080" size="2">&#43;</font><font size="2"> </font><font color="#ff0000" size="2">')'</p> </font> 2008-09-18T14:57:24-04:002630280http://forums.asp.net/p/1319861/2630280.aspx/1?Re+problem+in+pattern+matching+Re: problem in pattern matching.. <p></p> <blockquote><span class="icon-blockquote"></span> <h4>vijayakrishna</h4> <font color="#0000ff" size="2"></font> <p></p> <p><font color="#0000ff" size="2">SET</font><font size="2"> @SQL</font><font color="#808080" size="2">=</font><font color="#ff0000" size="2">'SELECT Companyname, Role, Moreaboutjob, candidate,Experience,Location,salarymin,salarymax,Interviewdate,postdate FROM callcenter where Location Like ''%'</font><font size="2"> </font><font color="#808080" size="2">&#43;</font><font size="2"> @city </font><font color="#808080" size="2">&#43;</font><font size="2"> </font><font color="#ff0000" size="2">'%'' and DATEDIFF(Day, postdate, getdate( ) ) &lt;='''</font><font size="2"> </font><font color="#808080" size="2">&#43;</font><font size="2"> @datediff </font><font color="#808080" size="2">&#43;</font><font size="2"> </font><font color="#ff0000" size="2">''' and Role in ('</font><font size="2"> </font> <font color="#808080" size="2">&#43;</font><font size="2"> @role </font><font color="#808080" size="2">&#43;</font><font size="2"> </font><font color="#ff0000" size="2">')'</font></p> <p><font color="#ff0000" size="2"></font></p> </blockquote> &nbsp; <p></p> <p><b>&nbsp;Ok..as i told you i am using this query to bind my datalist dynamically, I am writing following code on search button</b></p> <p>&nbsp; cmd.Parameters.AddWithValue(&quot;@datediff&quot;, beforedays)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&quot;@city&quot;, city1)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&quot;@role&quot;, moreabout)<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Connection = objConn<br> <br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim ds As New System.Data.DataSet<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim da As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter(cmd)<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>&nbsp; da.Fill(ds, &quot;callcenter&quot;)</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>//GOT ERROR HERE</b><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataList1.DataSource = ds<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataList1.DataBind()<br> <br> </p> <p>Now following error comes <br> </p> <p><b>Conversion failed when converting the varchar value</b> 'SELECT Companyname, Role, Moreaboutjob, candidate,Experience,Location,salarymin,salarymax,Interviewdate,postdate FROM callcenter where Location Like '%mumbai%' and DATEDIFF(Day, postdate, getdate( ) ) &lt;='' <b>to data type int.</b> <br> </p> 2008-09-18T15:05:21-04:00