SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 newid()
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

connectd.ts
Starting Member

4 Posts

Posted - 09/01/2010 :  11:41:06  Show Profile  Reply with Quote
I was wondering if newid() value is different even if newid() is used in 2 different places in the same sql statement, i.e, one newid() is select clause and another newid() in where clause

SELECT *, ABS(CAST(
(BINARY_CHECKSUM
(tbl1.col1, NEWID())) as int))
% 100 FROM table1 tbl1
WHERE (ABS(CAST(
(BINARY_CHECKSUM
(tbl1.col1, NEWID())) as int))
% 100) < 20

The reason for my question: The results of the select do not seem to match with the where condition.

Thank you.

russell
Pyro-ma-ni-yak

USA
5071 Posts

Posted - 09/01/2010 :  11:49:48  Show Profile  Visit russell's Homepage  Reply with Quote
it will return 2 distinct values, so your query won't work. but you can do this

Declare @newid uniqueidentifier
set @newid = newid()

SELECT	*, ABS(CAST((BINARY_CHECKSUM(tbl1.col1, @newid)) as int)) % 100
FROM	table1 tbl1
WHERE	(ABS(CAST((BINARY_CHECKSUM(tbl1.col1, @newid)) as int)) % 100) < 20
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000