Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Output random dates that falls on a weekday


Output random dates that falls on a weekday

Author
Message
JStevenson1
JStevenson1
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 94
I have a column called Transaction Date. I'd like to populate a new date that falls within 12 days from the Transaction Date that falls on a weekday.

TransactionID: TransactionDate:
1 05/01/2014
2 05/01/2014
3 07/24/2014

Desired output:

TransactionID: TransactionDate: RandomDates:
1 05/01/2014 05/05/2014
2 05/01/2014 05/07/2014
3 07/24/2014 07/29/2014
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16542 Visits: 16997
JStevenson1 (5/1/2014)
I have a column called Transaction Date. I'd like to populate a new date that falls within 12 days from the Transaction Date that falls on a weekday.

TransactionID: TransactionDate:
1 05/01/2014
2 05/01/2014
3 07/24/2014

Desired output:

TransactionID: TransactionDate: RandomDates:
1 05/01/2014 05/05/2014
2 05/01/2014 05/07/2014
3 07/24/2014 07/29/2014


This would be a lot easier if you could provide ddl and sample data in a consumable format in the future.

If I understand your requirements you want a random date that is at least 1 but not more than 12 days later than TransactionDate?

Jeff Moden wrote an excellent article about generating test data. http://www.sqlservercentral.com/articles/Data+Generation/87901/

Using the logic in there to generate a random integer within a given range we can do something like this.


with SomeData as
(
   select 1 as TransactionID, cast('20140501' as datetime) as TransactionDate union all
   select 2, '20140501' union all
   select 3, '20140724'
)

select *, DATEADD(DAY, ABS(CHECKSUM(NEWID())) % 12 + 1, TransactionDate) as RandomDate
from SomeData



Make sure you read that article and understand what is going on here before you use this.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
JStevenson1
JStevenson1
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 94
Thanks. I will read the article.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search