Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8289511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:31:29+00:00 2026-06-08T12:31:29+00:00

Following is my sql query for cron in which i am trying to save

  • 0

Following is my sql query for cron in which i am trying to save records from one table to another table i just want one modification which i could not make that in my i_age field the age should get save in the db but if zero is comming from previous table then the age calculation formula doesnot apply and it saves only zero to i_age field kindly let me know how can i do that ,

THANKS,

INSERT IGNORE into z_census ( i_age)
            SELECT  IF((i_age = 0), i_age, FLOOR(DATEDIFF(DATE(NOW()),DATE_ADD('1970-01-01',INTERVAL users.i_dob SECOND))/365.25) )
            FROM users
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T12:31:30+00:00Added an answer on June 8, 2026 at 12:31 pm

    This is equivalent to the expression in your query that calculates “age” (in integer) years:

    FLOOR(DATEDIFF(DATE(NOW()),
      DATE_ADD('1970-01-01',INTERVAL users.i_dob SECOND)
    )/365.25)
    

    This expression is really of the form:

    FLOOR(DATEDIFF(DATE(NOW()), dob )/365.25)
    

    where dob is a DATE value that represents the user’s date of birth. We derive dob using this expression:

    DATE_ADD('1970-01-01',INTERVAL users.i_dob SECOND)
    

    (This is based on some assumptions, as noted below.)


    Alternative:

    This calculation returns nearly the same result, but is actually more precise:

    IF(YEAR(NOW())<=YEAR( dob ), 0,
      YEAR(NOW())-YEAR( dob ) /* yeardiff and subtract 1 if now is before birthday */
      - (MONTH(NOW())<MONTH( dob ) OR 
          (MONTH(NOW())=MONTH( dob ) AND DAY(NOW())<DAY( dob )))
    )
    

    NOTE: Again, in this expression, dob represents an actual MySQL DATE value, NOT an integer number of seconds. To use this, you would replace all five occurrences of dob with this expression:

    DATE_ADD('1970-01-01',INTERVAL users.i_dob SECOND)
    

    More details:

    It looks like the datatype of the users.i_dob column represents a date as an integer number of seconds from midnight Jan 1, 1970. (Given that this seems to be “working” for DOB after 1970, but not earlier. So this is most likely a (signed) INT or BIGINT. (It’s possible this is a character column, and the value is being implicitly converted to numeric. A TIMESTAMP column would not allow for storage of date values earlier than Jan 1, 1970.)

    (Having information about the actual datatypes of the columns involved would help some.)

    Given that leading “i_” on the column name, we’re going to go forward under the assumption the users.i_dob column is defined as INT, and represents the number of seconds from midnight Jan 1, 1970 UTC.

    That gives an effective range of “dob” values that can be represented of approx. ‘1901-12-14’ to ‘2038-01-19’

    It looks like this query:

    SELECT DISTINCT users.id
         , users.v_first_name
         , users.v_last_name
         , FLOOR((TO_DAYS(NOW())- TO_DAYS(FROM_UNIXTIME(users.i_dob))) / 365.25)
      FROM users
    

    is attempting to calculate a users “age” in years.

    That FROM_UNIXTIME function works with TIMESTAMP values, so it’s not going to “work” for values before Jan 1, 1970.

    To get the actual DATETIME value represented by i_dob, we can use the DATE_ADD function.

    DATE_ADD('1970-01-01',INTERVAL users.i_dob SECOND)
    

    To get a number of days between that DATETIME value and the current date, we use the DATEDIFF function:

    DATEDIFF(NOW(),DATE_ADD('1970-01-01',INTERVAL users.i_dob SECOND))
    

    (The DATEDIFF function ignores the time components, and only operates on the date portion, which is most likely what you want. The calculation in your query includes the time portion, which adds a bit of sloppiness.)

    From the number of days, it looks like you are calculating years by dividing by number of days in a year, there’s a tiny bit of sloppiness there, when NOW() is within a day of their birthday… but I’m not going to bother with demonstrating that here.)

    So, I think this should get it close enough for you,

    = FLOOR(DATEDIFF(DATE(NOW()),
        DATE_ADD('1970-01-01',INTERVAL users.i_dob SECOND)
      )/365.25)
    

    A more precise calculation of a persons “age” really needs to compare the year, month and day. If we are only interested in reporting non-negative ages, something like this will work:

    IF(YEAR(NOW())<=YEAR( dob ), 0,
      YEAR(NOW())-YEAR( dob ) /* yeardiff and subtract 1 if now is before birthday */
      - (MONTH(NOW())<MONTH( dob ) OR 
          (MONTH(NOW())=MONTH( dob ) AND DAY(NOW())<DAY( dob )))
    )
    

    NOTE: the dob in that expression represents a MySQL DATE value representing the “dob”, NOT an integer number of seconds.

    NOTE: this expression will returns only non-negative values; it will never return a negative age.

    I admit this looks a bit complicated, but all that’s really doing is checking that the age is not at least 1 year, and otherwise subtracting the years, and then using a conditional test (which that returns a boolean we interpret as an integer 1 or 0) to adjust the difference back by 1 year when the current month and day are prior to the month and day of the birthday.)

    In your case, to use this, you’d need to substitute all five occurrences of dob in that expression with the expression that gets dob DATE value from the integer, i.e.

    dob = DATE_ADD('1970-01-01',INTERVAL users.i_dob SECOND)
    

    That would give a more precise result, but would be way more uglier than the other expression, which returns a nearly equivalent result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following SQL query that selects some results from my table: select avg(c3),
I have the following SQL query: SELECT * FROM table WHERE field_1 <> field_2
I have the following SQL query: SELECT DISTINCT ProductNumber, PageNumber FROM table I am
If I run the following SQL query SELECT * FROM A LEFT JOIN B
I have the following sql query and I want to filter the results where
Just wondering how the following sql query would look in linq for Entity Framework...
I have following SQL query SELECT TOP 10000 AVG(DailyNodeAvailability.Availability) AS AVERAGE_of_Availability FROM Nodes INNER
I want to convert the following SQL Query to a SubSonic Query. SELECT [dbo].[tbl_Agency].[ParentCompanyID]
I have the following SQL query: select AuditStatusId from dbo.ABC_AuditStatus where coalesce(AuditFrequency, 0) <>
I am trying to write the following SQL query as a JPA query. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.