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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 [HELP] Split and Looping

Author  Topic 

knockyo
Yak Posting Veteran

83 Posts

Posted - 2009-12-01 : 06:55:18
Hi All,

Below is my SQL,

Select *
From tbl_Item
Where StatusId IN (1,2)

Result:
ItemId ItemStatus StatusId UserName
------ ---------- -------- --------
151588 Donated 1 User1
151587 Donated 1 User1
154664 Donated 1 User3
155840 Scrap 2 User2
155840 Scrap 2 user2

Questions:
How can i set the value in the UserName field = ""
when the StatusId=2

So I need to involve 2 function Split and While Loop?

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-12-01 : 07:03:08
Hi, Try this

select itemid,itemstatus,statusid, case when statusid = 2 then '' else username end AS username
from tbl_Item
Where StatusId IN (1,2)
Go to Top of Page

knockyo
Yak Posting Veteran

83 Posts

Posted - 2009-12-01 : 07:06:58
Hi,
thanks your idea.

but i can't direct to that because from the

Select *
From tbl_Item
Where StatusId IN (1,2)


excatly the where clause there is a parameter
Select *
From tbl_Item
Where StatusId IN (@StatusId)


Because of that reason, i need split the @StatusId with delimiters (,) to loop the data

[url=http://angel.exabytes.com.my/idevaffiliate.php?id=3204138_14_1_55][/url]
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-12-01 : 07:27:36
Select *
From tbl_Item
Where ','+@StatusId+',' '%,'+cast(StatusId as varchar(10))+',%'


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -