Problem with Split Function in SQL...http://forums.asp.net/t/1775860.aspx/1?Problem+with+Split+Function+in+SQL+Wed, 07 Mar 2012 06:34:44 -050017758604860121http://forums.asp.net/p/1775860/4860121.aspx/1?Problem+with+Split+Function+in+SQL+Problem with Split Function in SQL... <p>Hello All,</p> <p>I have database column values like ('1-Jan-11;29-Sep-11','22-Jun-11')...</p> <p>So I have used Split function for ';' &amp; Then trying to pass it in Stored Procedure...</p> <p>Actually I am displaying Gridview by this Stored Procedure, So getting errors ::</p> <p>Msg 512, Level 16, State 1, Line 1<br> Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, &lt;, &lt;= , &gt;, &gt;= or when the subquery is used as an expression.</p> <p></p> <p>Anyone Please help me...</p> <p>Thank you...</p> 2012-03-02T06:03:15-05:004860122http://forums.asp.net/p/1775860/4860122.aspx/1?Re+Problem+with+Split+Function+in+SQL+Re: Problem with Split Function in SQL... <p>To Split data use below split function..</p> <pre class="prettyprint">CREATE FUNCTION [dbo].[SplitString](@String varchar(8000), @Delimiter char(1)) returns @temptable TABLE (items varchar(8000)) as begin declare @idx int declare @slice varchar(8000) select @idx = 1 if len(@String)&lt;1 or @String is null return while @idx!= 0 begin set @idx = charindex(@Delimiter,@String) if @idx!=0 set @slice = left(@String,@idx - 1) else set @slice = @String if(len(@slice)&gt;0) insert into @temptable(Items) values(@slice) set @String = right(@String,len(@String) - @idx) if len(@String) = 0 break end return end GO --now call your parameter as below select * From dbo.SplitString('1-Jan-11;29-Sep-11;22-Jun-11',';') --output --items --1-Jan-11 --29-Sep-11 --22-Jun-11</pre> <p>for error resolution, post your store procedure query..</p> <p></p> 2012-03-02T06:05:54-05:004860198http://forums.asp.net/p/1775860/4860198.aspx/1?Re+Problem+with+Split+Function+in+SQL+Re: Problem with Split Function in SQL... <p></p> <blockquote><span class="icon-blockquote"></span> <h4>yrb.yogi</h4> To Split data use below split function..</blockquote> <p></p> <p>I have this Split Function Already...</p> <p>My <strong>Stored Procedure</strong> :::</p> <p>ALTER PROCEDURE [dbo].[proc_SelectProjectDetails]<br> <br> &nbsp;&nbsp;&nbsp; @PS_Due_dateFrom as datetime,<br> &nbsp;&nbsp; &nbsp;@PS_Due_dateTo as datetime,<br> &nbsp;&nbsp;&nbsp; <br> AS&nbsp;&nbsp; &nbsp;<br> BEGIN<br> &nbsp;&nbsp; &nbsp;SELECT<br> &nbsp;&nbsp; &nbsp;<br> &nbsp;&nbsp;&nbsp; Question, Q_Attach_File, Q_Date, Response, R_Attach_File, R_Date, Status, S_Date From tblClarification Where (R_Date &gt;= @Response_dateFrom) AND (R_Date &lt;= @Response_dateTo)</p> <p>END</p> <p><strong>R_Date is the column about which im talking</strong>...</p> <p>Instead of R_Date, I used (SELECT Res_Date FROM tblClarification CROSS APPLY dbo.SplitForDate(R_Date))</p> <p>But got error; <strong>Conversion failed when converting date and/or time from character string.</strong></p> 2012-03-02T06:58:43-05:004860204http://forums.asp.net/p/1775860/4860204.aspx/1?Re+Problem+with+Split+Function+in+SQL+Re: Problem with Split Function in SQL... <pre class="prettyprint">SELECT Res_Date FROM tblClarification CROSS APPLY dbo.SplitForDate(CAST(R_Date AS VARCHAR(100))) --pass delimiter --if your function require</pre> 2012-03-02T07:01:49-05:004860209http://forums.asp.net/p/1775860/4860209.aspx/1?Re+Problem+with+Split+Function+in+SQL+Re: Problem with Split Function in SQL... <p></p> <blockquote><span class="icon-blockquote"></span> <h4>yrb.yogi</h4> SELECT Res_Date FROM tblClarification CROSS APPLY dbo.SplitForDate(CAST(R_Date AS VARCHAR(100))) --pass delimiter --if your function require</blockquote> <p></p> <p>Nope, Same Error...</p> 2012-03-02T07:04:09-05:004860212http://forums.asp.net/p/1775860/4860212.aspx/1?Re+Problem+with+Split+Function+in+SQL+Re: Problem with Split Function in SQL... <p></p> <blockquote><span class="icon-blockquote"></span> <h4>jeeten09</h4> Nope, Same Error...</blockquote> <p></p> <p>can you show <span>Res_Date&nbsp;</span>column data?</p> 2012-03-02T07:07:47-05:004860218http://forums.asp.net/p/1775860/4860218.aspx/1?Re+Problem+with+Split+Function+in+SQL+Re: Problem with Split Function in SQL... <p></p> <blockquote><span class="icon-blockquote"></span> <h4>yrb.yogi</h4> can you show <span>Res_Date&nbsp;</span>column data?</blockquote> <p></p> <p>Res_Date data is coming from the Split Function...</p> <p>But R_Date are shown as below :::</p> <p><strong>&nbsp;&nbsp; Unique_Number&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; R_date</strong></p> <p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong> GJ_1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1-Sep-11;9-Sep-11;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GJ_2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 10-Dec-11;12-Dec-11;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GJ_3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9-Jan-12;</p> <p>&nbsp;</p> 2012-03-02T07:11:37-05:004860250http://forums.asp.net/p/1775860/4860250.aspx/1?Re+Problem+with+Split+Function+in+SQL+Re: Problem with Split Function in SQL... <pre class="prettyprint">declare @table table ( Unique_Number varchar(100),R_date VARCHAR(100) ) INSERT INTO @table select 'GJ_1','1-Sep-11;9-Sep-11;' union all select 'GJ_2','10-Dec-11;12-Dec-11;' union all select 'GJ_3','9-Jan-12;' select Unique_Number,R_date,items As SplitedR_date From @table CROSS APPLY dbo.SplitString(R_date,';') --output --Unique_Number R_date SplitedR_date --GJ_1 1-Sep-11;9-Sep-11; 1-Sep-11 --GJ_1 1-Sep-11;9-Sep-11; 9-Sep-11 --GJ_2 10-Dec-11;12-Dec-11; 10-Dec-11 --GJ_2 10-Dec-11;12-Dec-11; 12-Dec-11 --GJ_3 9-Jan-12; 9-Jan-12</pre> 2012-03-02T07:31:33-05:004860253http://forums.asp.net/p/1775860/4860253.aspx/1?Re+Problem+with+Split+Function+in+SQL+Re: Problem with Split Function in SQL... <p></p> <blockquote><span class="icon-blockquote"></span> <h4>yrb.yogi</h4> declare @table table ( Unique_Number varchar(100),R_date VARCHAR(100) ) INSERT INTO @table select 'GJ_1','1-Sep-11;9-Sep-11;' union all select 'GJ_2','10-Dec-11;12-Dec-11;' union all select 'GJ_3','9-Jan-12;' select Unique_Number,R_date,items As SplitedR_date From @table CROSS APPLY dbo.SplitString(R_date,';') --output --Unique_Number R_date SplitedR_date --GJ_1 1-Sep-11;9-Sep-11; 1-Sep-11 --GJ_1 1-Sep-11;9-Sep-11; 9-Sep-11 --GJ_2 10-Dec-11;12-Dec-11; 10-Dec-11 --GJ_2 10-Dec-11;12-Dec-11; 12-Dec-11 --GJ_3 9-Jan-12; 9-Jan-12</blockquote> <p></p> <p>Thats I know But I am doing Inner Join of 6 Tables &amp; There are very large number of Parameters So Its not possible for me to do like that...</p> <p>Even I use code like which u told though I am getting error of conversion...</p> 2012-03-02T07:34:42-05:004860257http://forums.asp.net/p/1775860/4860257.aspx/1?Re+Problem+with+Split+Function+in+SQL+Re: Problem with Split Function in SQL... <p>I am showing you a simple demo of how to use split data...</p> <p>you just need to cast your splitdata to datetime as follow..and use that column for join..</p> <pre class="prettyprint">declare @table table ( Unique_Number varchar(100),R_date VARCHAR(100) ) INSERT INTO @table select 'GJ_1','1-Sep-11;9-Sep-11;' union all select 'GJ_2','10-Dec-11;12-Dec-11;' union all select 'GJ_3','9-Jan-12;' select Unique_Number,R_date,CAST(items AS DATETIME) As DataInDate From @table CROSS APPLY dbo.SplitString(R_date,';') --output --Unique_Number R_date DataInDate --GJ_1 1-Sep-11;9-Sep-11; 1-Sep-11 --GJ_1 1-Sep-11;9-Sep-11; 9-Sep-11 --GJ_2 10-Dec-11;12-Dec-11; 10-Dec-11 --GJ_2 10-Dec-11;12-Dec-11; 12-Dec-11 --GJ_3 9-Jan-12; 9-Jan-12</pre> <p></p> 2012-03-02T07:38:08-05:004860262http://forums.asp.net/p/1775860/4860262.aspx/1?Re+Problem+with+Split+Function+in+SQL+Re: Problem with Split Function in SQL... <p></p> <blockquote><span class="icon-blockquote"></span> <h4>yrb.yogi</h4> <p></p> <p>I am showing you a simple demo of how to use split data...</p> <p>you just need to cast your splitdata to datetime as follow..and use that column for join..</p> <p></p> </blockquote> <p></p> <p>I tried it but not able to solve same error...</p> 2012-03-02T07:39:58-05:004860269http://forums.asp.net/p/1775860/4860269.aspx/1?Re+Problem+with+Split+Function+in+SQL+Re: Problem with Split Function in SQL... <p></p> <blockquote><span class="icon-blockquote"></span> <h4>jeeten09</h4> I tried it but not able to solve same error...</blockquote> <p></p> <p>You must need to cast your varchar column to datetime. Check weather your varchar column contain valid datetime value..like MM/DD/YYYY etc.</p> <p></p> <p>for better, you must check in where cluase IsDate(yourColumn)=1 to get proper datetime..</p> 2012-03-02T07:42:23-05:004860271http://forums.asp.net/p/1775860/4860271.aspx/1?Re+Problem+with+Split+Function+in+SQL+Re: Problem with Split Function in SQL... <p></p> <blockquote><span class="icon-blockquote"></span> <h4>yrb.yogi</h4> <p></p> <p>You must need to cast your varchar column to datetime. Check weather your varchar column contain valid datetime value..like MM/DD/YYYY etc.</p> <p></p> <p>for better, you must check in where cluase IsDate(yourColumn)=1 to get proper datetime..</p> <p></p> </blockquote> <p></p> <p>Im using Date Format as dd-MMM-yyyy &amp; You can see it in the data as i gave u before...</p> 2012-03-02T07:44:31-05:004860278http://forums.asp.net/p/1775860/4860278.aspx/1?Re+Problem+with+Split+Function+in+SQL+Re: Problem with Split Function in SQL... <p></p> <blockquote><span class="icon-blockquote"></span> <h4>jeeten09</h4> Im using Date Format as dd-MMM-yyyy &amp; You can see it in the data as i gave u before...</blockquote> <p></p> <p>That is the reason you got error..</p> <p>Set Dateformat to dd-mm-yyyy</p> <p>like below..</p> <pre class="prettyprint">SET DATEFORMAT DMY; --rest of query will come here SET DATEFORMAT MDY;</pre> 2012-03-02T07:50:13-05:004860280http://forums.asp.net/p/1775860/4860280.aspx/1?Re+Problem+with+Split+Function+in+SQL+Re: Problem with Split Function in SQL... <p></p> <blockquote><span class="icon-blockquote"></span> <h4>yrb.yogi</h4> <p></p> <p>Set Dateformat to dd-mm-yyyy</p> <p>like below..</p> <pre class="prettyprint">SET DATEFORMAT DMY; --rest of query will come here SET DATEFORMAT MDY;</pre> <p></p> </blockquote> <p></p> <p>Can i set dateformat in Query only ???</p> <p>How ???</p> <p>Or anyother easy way to do it ???</p> 2012-03-02T07:55:55-05:004860305http://forums.asp.net/p/1775860/4860305.aspx/1?Re+Problem+with+Split+Function+in+SQL+Re: Problem with Split Function in SQL... <p></p> <blockquote><span class="icon-blockquote"></span> <h4>jeeten09</h4> <p></p> <p>Can i set dateformat in Query only ???</p> <p>How ???</p> <p>Or anyother easy way to do it ???</p> <p></p> </blockquote> <p></p> <p>You can set dateformat in query too.</p> <p>i think this is easy way..</p> 2012-03-02T08:06:00-05:004860326http://forums.asp.net/p/1775860/4860326.aspx/1?Re+Problem+with+Split+Function+in+SQL+Re: Problem with Split Function in SQL... <p></p> <blockquote><span class="icon-blockquote"></span> <h4>yrb.yogi</h4> <p></p> <p>You can set dateformat in query too.</p> <p>i think this is easy way..</p> <p></p> </blockquote> <p></p> <p>huh ?</p> <p>still same here...</p> 2012-03-02T08:16:15-05:004860335http://forums.asp.net/p/1775860/4860335.aspx/1?Re+Problem+with+Split+Function+in+SQL+Re: Problem with Split Function in SQL... <p></p> <blockquote><span class="icon-blockquote"></span> <h4>jeeten09</h4> <p></p> <p>huh ?</p> <p>still same here...</p> <p></p> </blockquote> <p></p> <p>Now you must need to show your entire query here...and where &amp; which column you are getting error</p> 2012-03-02T08:21:53-05:004860356http://forums.asp.net/p/1775860/4860356.aspx/1?Re+Problem+with+Split+Function+in+SQL+Re: Problem with Split Function in SQL... <p></p> <blockquote><span class="icon-blockquote"></span> <h4>yrb.yogi</h4> Now you must need to show your entire query here...and where &amp; which column you are getting error</blockquote> <p></p> <p>msg me ur email id</p> 2012-03-02T08:42:08-05:004867610http://forums.asp.net/p/1775860/4867610.aspx/1?Re+Problem+with+Split+Function+in+SQL+Re: Problem with Split Function in SQL... <p>Hi jeeten09,</p> <p>Have you solved your problem? If not, please post your whole query, it will provide us more information about your issue. Thanks for your understanding.</p> <p>Thanks.</p> 2012-03-07T06:34:44-05:00