SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    395
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Update several entries with different date values

    I want to update some entries with different date values. For example, I have 50 entries. The table has a date column, and I want those entries's date field to be populated with random dates, ranging from January 1, 2008 to August 1 2008.

    Something like this : UPDATE mytable (date) VALUES(RAND());

    How can I do that?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,347
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    before running the query, note that jan 1 to aug 1 is 214 days

    so you need a random number between 0 and 213

    this can be obtained by the expression RAND() * 213

    then just add this number of days to the starting date
    Code:
    UPDATE daTable
       SET datecolumn = '2008-01-01' + INTERVAL RAND()*213 DAY
    neat, eh?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    395
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awesome! Thank you


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •