This is in continuation to my previous post on SQL Server Tips.
1. SQL function RAND() is used to generate random numbers. It can have an input parameter as SEED
Syntax: RAND([SEED])
Use the below query to get new random number always (The chance of duplicate random number is possible if the query is executed more than 1 time with-in the same milli-second)
SELECT RAND(
(
DATEPART(MM, GETDATE()) * 100000 ) + (DATEPART(SS, GETDATE()) * 1000 ) + DATEPART(MS, GETDATE())
)
2. NULLIF(EXP1, EXP2) is used to compare two expressions are equal and return a NULL value if so. This can be used as a handy tool to compare two column data having same values
The below SQL statement will return the value True
Select ISNULL( NULLIF(‘ABC’, ‘ABD’), ‘True’ )
Suppose there are two columns FirstName and LastName in a table TblNames and you want to cross check and compare the two columns having same values, use the below query against each record in the table
Select RecID, ISNULL( NULLIF(FirstName, LastNam), ‘True’ ) from TblNames
3. Changing Object Owners: If you want to change the Object Owner for an the objects in the database, you can use this stored procedure
sp_changeobjectowner @objname = 'objectName' , @newowner = 'ownerName'