SQL Server randomly generates a specified range of dates

Source: Internet
Author: User
Tags abs add time

A paging problem, DTCms3.0, paging is based on the time page, if the Add Time (add_time) is the same value, regardless of the number of pages clicked, the data displayed is the same content, so there is a need to change the same time to specify a random date function.

The SQL script is as follows:

DECLARE @BDate datetime,@EDate datetimeSET @BDate = '20150701'          --Lower LimitSET @EDate = '20150714 23:59:59' --Upper LimitSELECTID, Random_datetime= DATEADD(Second,ABS(CHECKSUM (NEWID()))%DATEDIFF(Second,@BDate,@Edate),@BDate) fromdbo.dt_articleWHEREIdbetween 1  and  - 

The resulting content is:

IdRandom_datetime3 2015-07-05 07:55:10.0004 2015-07-08 06:15:06.0009 2015-07-12 19:14:24.00010 2015-07-11 20:13:05.00012 2015-07-06 19:09 : 12.00013 2015-07-09 10:28:59.00014 2015-07-03 07:18:42.00015 2015-07-06 09:46:29.000

The ID at this time is the ID in the corresponding dt_article table.

Then we can write the generated data to the temporary table, then associate the update!

We modify the script as follows:

SELECTID, Random_datetime= DATEADD(Second,ABS(CHECKSUM (NEWID()))%DATEDIFF(Second,@BDate,@Edate),@BDate) into#Temp  fromdbo.dt_article--Update DateUPDATEDbo.dt_articleSETAdd_time=B.random_datetime fromDbo.dt_article A, #TempbWHEREa.ID=b.ID

SQL Server randomly generates a specified range of dates

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.