Friday, April 18, 2008

A couple of Sql Server (useful) things

1) Changing sa password

A couple of days ago I discovered with horror that I forgot my "sa" password, on my sql 2005 local instance.

To solve this, googling around I found this couple of methods:

USE MASTER

ALTER LOGIN [sa] WITH PASSWORD=N'new_password'

or from a command prompt

    OSQL -S <server_name> -E
    1> EXEC sp_password NULL, 'new_password', 'sa'
    2> GO

2) Transact sql Split function

I needed a split function, and I fpound this good forum discussion exactly on this topic:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648

I used this one:

CREATE FUNCTION dbo.Split
(
@RowData nvarchar(2000),
@SplitOn nvarchar(5)

RETURNS @RtnValue table
(
Id int identity(1,1),
Data nvarchar(100)
)
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

No comments: