Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Random default string for a column using CASE and NEWID Expand / Collapse
Author
Message
Posted Wednesday, February 6, 2008 12:26 AM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 9:56 AM
Points: 755, Visits: 130
Comments posted to this topic are about the item Random default string for a column using CASE and NEWID
Post #452034
Posted Thursday, June 14, 2012 3:14 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:49 AM
Points: 7,234, Visits: 13,719
SELECT
CAST(NEWID() AS CHAR(36)),
CASE CAST( ASCII (SUBSTRING( CAST( NEWID() AS VARCHAR(64)), 1, 1)) AS INT) % 2
WHEN 0 THEN 'OPTION 1' ELSE 'OPTION 2' END,
CASE ABS(CHECKSUM(NEWID()) % 2)
WHEN 0 THEN 'OPTION 1' ELSE 'OPTION 2' END
FROM (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12)) r (n)

Uniqueidentifier CASTS more correctly to a CHAR(36)

ABS(CHECKSUM(NEWID()) is handy for generating a pseudo-random integer.



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1315705
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse