How can I select a random date within a specific inclusive range, let's say '1950-01-01' and '1999-12-31' with SQL Server?
相关问题
- sql execution latency when assign to a variable
- What is the best way to cache a table from a (SQL)
- php PDO::FETCH_ASSOC doesnt detect select after ba
- Bulk update SQL Server C#
- SQL to Parse a Key-Value String
相关文章
- Entity Framework 4.3.1 failing to create (/open) a
- Code for inserting data into SQL Server database u
- SQL Server 2008 Change Data Capture, who made the
- Delete Every Alternate Row in SQL
- Linux based PHP install connecting to MsSQL Server
- SQL Azure Reset autoincrement
- How do we alias a Sql Server instance name used in
- Is recursion good in SQL Server?
This will give you 1000 rows of data to insert.
NB: This answer originally used
ABS(CAST(CRYPT_GEN_RANDOM(4) AS INT))
to generate the random numbers. UnlikeRAND()
which is only evaluated once per statement this is evaluated once per row so would work.However it seems that the Query Optimiser does not realise this and treats it as a constant. For the purposes of generating random data this probably won't matter (unless you are populating a column constrained by a foreign key)
but I just tested the alternative
ABS(CHECKSUM(NEWID()))
to see if there was any performance benefit of one over the other.Typical speeds to generate 1,000,000 rows using the numbers table above and select the
MAX
value (to avoid overhead of returning all these rows to the client)So unless you need a cryptographically secure PRNG it's probably best avoided!
EDIT
If this is to be executed as part of a statement that returns multiple rows or as part of update, the RAND() would return single value for the whole resultset. For that case RAND(CHECKSUM(NEWID())) can be used.