Statistics

Total Posts: 34
This Year: 0
This Month: 0
This Week: 0
Comments: 174


RSS 2.0   SocialTwist Tell-a-Friend


Admin

Sign In

Navigation


Recent Posts


On this page....

Smarter the better – SQL Server tips – part 03

Archives

 Full Archives By Category
 2007 Calendar View
<June 2009>
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

Categories

CDOSYS (1) Classic ASP (10) Command Line (2) Databases (16) Excel (1) HTML (1) IIS (10) Indexing Service (1) Internet Explorer (7) Media Streaming (1) MS.Net (2) SQA (7) SQL Server (16) Windows OS (2)

Blogroll - Fav Blogs


Acknowledgments

DasBlog Theme Design by: Tom Watts
E-mail: Send mail to the author(s)
Theme Image by: dreamLogic

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

Technology Blog

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'