Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
mailtosaja
Starting Member
28 Posts |
Posted - 2008-07-29 : 03:45:43
|
| declare @project_id varchar(800)set @project_id='1019,1006,1019,1019'select * from trn_defect_details wherei_project_id in ( ( case when @project_id is null then i_project_id when @project_id != '' then @project_id END ) )when i am executing above query i got the below error..Msg 245, Level 16, State 1, Line 3Conversion failed when converting the varchar value '1019,1006,1019,1019' to data type int...)i_project_id Datatype is bigint ..)case return the varchar value....)How will fix this problem.....any one can help me.. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-29 : 04:10:53
|
| All values returned by case conditions should be of same type.In your case i_project_id is int while @project_id is varchar. so you need to cast i_project_id to varchar to avoid this error. |
 |
|
|
mailtosaja
Starting Member
28 Posts |
Posted - 2008-07-29 : 04:35:43
|
| Hi, Thanks for your reply..But @project_id it's con't convert to bigint .Because it's contain different formate for example "project_id='1019,1006,1019,1019'" Did you have any other idea...Thanks & Regards,S.Sajan |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-29 : 04:46:12
|
quote: Originally posted by mailtosaja Hi, Thanks for your reply..But @project_id it's con't convert to bigint .Because it's contain different formate for example "project_id='1019,1006,1019,1019'" Did you have any other idea...Thanks & Regards,S.Sajan
i was suggesting to cast the field i_project_id to varchar not the parameter |
 |
|
|
mailtosaja
Starting Member
28 Posts |
Posted - 2008-07-29 : 05:13:26
|
| Hi, Can you rewrite the query and send to me..Thanks & Regards,S.Sajan |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2008-07-29 : 05:14:21
|
He means this...declare @project_id varchar(800)set @project_id='1019,1006,1019,1019'select *from trn_defect_detailswhere CAST(i_project_id AS VARCHAR(800)) IN ( ( CASE when @project_id is null then CAST(i_project_id AS VARCHAR(800)) when @project_id <> '' then @project_id END ) ) Are you aware that there is a condition that won't be resolved in your CASE statement?If @project_Id = '' nothing will go into your IN list.-------------Charlie |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2008-07-29 : 05:38:55
|
Actually, maybe this would work better.DECLARE @project_Id VARCHAR(800)SET @projectId = '1019,1006,1019,1019'SELECT *FROM trn_defect_detailsWHERE i_project_id = CASE WHEN NULLIF(@project_id, '') IS NULL THEN i_project_id ELSE CAST(REPLACE(@project_Id,',','') AS BIGINT) END -------------Charlie |
 |
|
|
VGuyz
Posting Yak Master
121 Posts |
Posted - 2008-07-29 : 05:41:56
|
| Hi chk this,declare @project_id varchar(60)set @project_id='200,202'declare @x xmlset @x='<i>' + replace(@project_id,',','.</i><i>') + '</i>'select @xselect * from trn_defect_details where i_project_id in(select replace(x.i.value ('.','varchar(50)'),'.','') project_id from @x.nodes('//i')x(i)) |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2008-07-29 : 05:45:13
|
| Wait a moment........What are you trying to achieve here?Because your original post used the IN clause I'm thinking that you want to return a number of results where i_project_Id = any of the numbers in your comma separated string.If you are you cannot do it this way. You would need to build a function to split the string and return a table of values or use dynamic sql. (edit: or one of the alternatives, XML, string comparators (LIKE) as shown by posters below)With the latest block of code, the SELECT statement will either return:1) 1 Value where i_project_Id = the stripped BIGINT casted @project_Id (assuming that I_project_Id is a primary key that is).2) 2 All the records in your table because each i_project_Id is always going to equal itself (unless it's null).Please post some data and expected results...-------------Charlie |
 |
|
|
VGuyz
Posting Yak Master
121 Posts |
Posted - 2008-07-29 : 05:51:56
|
| Hi charlie,Is there any wrong with my query..? |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2008-07-29 : 06:11:26
|
| Try thisdeclare @project_id varchar(800)set @project_id='1019,1006,1019,1019'select * from trn_defect_details where(@project_id is null or ','+@project_id+',' like '%,'+cast(i_project_id as varchar(10))+',%') |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2008-07-29 : 06:18:37
|
quote: Originally posted by VGuyz Hi charlie,Is there any wrong with my query..?
Hi VGuys,Don't think so. I'm not sure what Original Poster wants though. I'm wondering if he wants to check for any value in a comma separated string or if the commas represent a logical split of a big integer. Like splitting (1019100610191019) into (1019,1006,1019,1019) for readability.Your query does the former doesn't it? == returns rows that contain any sub values in a comma separated list.I don't think OP wants this though because why would you search for the same value 3 times? (1019)-------------Charlie |
 |
|
|
VGuyz
Posting Yak Master
121 Posts |
Posted - 2008-07-29 : 06:23:27
|
quote: Hi VGuys,Don't think so. I'm not sure what Original Poster wants though. I'm wondering if he wants to check for any value in a comma separated string or if the commas represent a logical split of a big integer. Like splitting (1019100610191019) into (1019,1006,1019,1019) for readability.Your query does the former doesn't it? == returns rows that contain any sub values in a comma separated list.I don't think OP wants this though because why would you search for the same value 3 times? (1019)-------------Charlie
yes,there's no need to check the same value(1019) for three times. |
 |
|
|
mailtosaja
Starting Member
28 Posts |
Posted - 2008-07-29 : 07:48:55
|
| Hi, declare @project_id varchar(800)set @project_id='1019,1006,1019,1019'select * from trn_defect_details wherei_project_id in(( case when @project_id is null then i_project_idwhen @project_id != '' then @project_idEND))..)@project_id is a optional parameter user may or may not be give the value ..)@project_id is a Bigint datatype..)if you are convert the i_project_id into varchar means the condition not match....because '4545,454545,454' this values taken as single varchar...So this is mismatch...)I need the result is get the all column from trn_defect_details table ..)condition is i_project_id in (4545,454545,454)can any one tell the solution.. Thanks & Regards,S.Sajan |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2008-07-29 : 08:45:17
|
quote: ..)@project_id is a optional parameter user may or may not be give the value..)@project_id is a Bigint datatype..)if you are convert the i_project_id into varchar means the condition not match....because '4545,454545,454' this values takenas single varchar...So this is mismatch...)I need the result is get the all column from trn_defect_details table..)condition is i_project_id in (4545,454545,454)
Hi mailtosaja,So if I understand you correctly....If @project_Id has a value, we must parse this to get a list of numbers to put into an IN condition. If it doesn't have a value display everything.Therefore if : @project_Id = '4545,454545,454' : you want to the IN condition to be...WHERE i_project_id IN ( 4545, 454545, 454 ) -- a List of INTEGER VALUESRaky and Vguys have posted queries that will do this. (Vguys used an XML method and Raky used a string comparator called LIKEIf I needed to do this I would write a user defined function that takes your string and returns a table of integers. you could then do something like this... IF EXISTS ( SELECT * from dbo.sysobjects WHERE id = object_id(N'[dbo].[fn_Split]') AND xtype IN (N'FN', N'IF', N'TF') ) DROP FUNCTION [dbo].[fn_Split]GOCREATE FUNCTION fn_Split (@text VARCHAR(8000), @delimiter VARCHAR(20) = ' ') RETURNS @Strings TABLE ( [position] INT IDENTITY PRIMARY KEY , [value] VARCHAR(8000) )AS BEGIN DECLARE @index int SET @index = -1 WHILE (LEN(@text) > 0) BEGIN -- Find the first delimiter SET @index = CHARINDEX(@delimiter , @text) -- No delimiter left? -- Insert the remaining @text and break the loop IF (@index = 0) AND (LEN(@text) > 0) BEGIN INSERT INTO @Strings VALUES (@text) BREAK END -- Found a delimiter -- Insert left of the delimiter and truncate the @text IF (@index > 1) BEGIN INSERT INTO @Strings VALUES (LEFT(@text, @index - 1)) SET @text = RIGHT(@text, (LEN(@text) - @index)) END -- Delimiter is 1st position = no @text to insert ELSE SET @text = RIGHT(@text, (LEN(@text) - @index)) END RETURNENDGODECLARE @project_id varchar(800)SELECT @project_id = '1019,1006,1019,1019'SELECT *FROM trn_defect_detailsWHERE ( @project_Id IS NULL OR i_project_id IN (SELECT CAST([value] AS BIGINT) FROM dbo.fn_Split( @project_Id, ',' )) ) -------------Charlie |
 |
|
|
|
|
|
|
|