Wednesday, March 12, 2008

Quotes

* Imagination is more important than knowledge

* We were so poor my daddy unplugged the clocks when we went to bed.

* To keep a lamp burning we have to put oil in it. .

Sunday, March 9, 2008

How to split a string based on given character?

Ans:

Create[dbo].[split_value](@RowData varchar(max),@SplitOn nvarchar(5))

RETURNS @RtnValue table
(

Data varchar(max)
)
AS
BEGIN
Declare @Cnt int
Set @Cnt = 1

While (Charindex(@SplitOn,@RowData)>0)
Begin
Insert Into @RtnValue (data)
Select
Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))

Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
Set @Cnt = @Cnt + 1
End

Insert Into @RtnValue (data)
Select Data = ltrim(rtrim(@RowData))

Return
END

1.How to concatenating multiple rows in a single column in Sql Server?

Create [dbo].[GetCartIds](@nuserid VARCHAR(200))

RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @Output VARCHAR(8000)
SET @Output = ''
SELECT @Output = CASE @Output
WHEN '' THEN convert(varchar(50),nCartId)
ELSE @Output + ',' +convert(varchar(50),nCartId)
END
from tblcart where nuserid=@nuserid
RETURN @Output
END