SQL   22

Age Calculation

Guest on 24th August 2022 04:31:43 AM

  1. SELECT  DATEDIFF(yy, @dob, CURRENT_TIMESTAMP) -
  2.         CASE WHEN (MONTH(CURRENT_TIMESTAMP) * 100 +
  3.                    DAY(CURRENT_TIMESTAMP)) <
  4.                    (MONTH(@dob)* 100 + DAY(@dob))
  5.              THEN 1 ELSE 0 END
  6.  
  7. --#2
  8. SELECT (CAST(CONVERT(CHAR(8),CURRENT_TIMESTAMP, 112) AS INT)
  9.        - CAST(CONVERT(CHAR(8), @dob, 112) AS INT))/10000
  10.  
  11. --#3
  12. SELECT YEAR(CURRENT_TIMESTAMP) - YEAR(@dob) -
  13.        (CASE WHEN (MONTH(@dob) > MONTH(CURRENT_TIMESTAMP))
  14.                OR (MONTH(@dob) = MONTH(CURRENT_TIMESTAMP)
  15.               AND DAY(@dob) > DAY(CURRENT_TIMESTAMP))
  16.              THEN 1 ELSE 0 END)
  17.  
  18. --

Raw Paste


Login or Register to edit or fork this paste. It's free.