Tuesday, October 18, 2011

SQL Server in-built functions

Some basic programming in SQL Server that comes handy.
Need to pad your number with zeroes? Use this
RIGHT('00000' + cast(@i as varchar(6)), 6)

Need a random 3-digit access code? Use this
RIGHT('00' + CAST(floor(rand()*1000) as VARCHAR(3)), 3)

Need your guid without the hyphens? Use this
REPLACE(CAST(NEWID() AS varchar(36)), '-', '')

References:
http://msdn.microsoft.com/en-us/library/ms177610.aspx

No comments:

Post a Comment