Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Query to check if today is Employee's birthday [closed]

Can any one tell me how to get the list of an employee who have their birthdays today ....

Thanks, Vishal

like image 430
Vegeta Avatar asked Jan 10 '12 13:01

Vegeta


People also ask

How do I get today's birthday in SQL Server?

-- Query to find workers, whose birthday is in current week SELECT * FROM @Workers WHERE DATEPART( Week, DATEADD( Year, DATEPART( Year, GETDATE()) - DATEPART( Year, DOB), DOB)) = DATEPART( Week, GETDATE()); The basic idea is the same as in the first query I explained. Only this time the week numbers are compared.

How do I find birthdays in SQL?

field would be to compare the day and month of this against the current system date in the where clause of your query. Something along the following lines: select * from wherever where .... ( datepart(d, EmployeeDOB) = datepart(d, getdate()) and datepart(m, EmployeeDOB) = datepart(m, getdate()) ) .....

Is there a today function in SQL?

SQL Server provides several different functions that return the current date time including: GETDATE(), SYSDATETIME(), and CURRENT_TIMESTAMP.

How do I check if a date is correct in SQL?

In SQL Server, you can use the ISDATE() function to check if a value is a valid date. To be more specific, this function only checks whether the value is a valid date, time, or datetime value, but not a datetime2 value. If you provide a datetime2 value, ISDATE() will tell you it's not a date (it will return 0 ).

How to find birth date of customer or student in SQL?

Sql Birthdate Query. In this post you will learn how to find birth date of customer or student. We will use getdate(), month and day functions in the query. Month function returns back the month of the date.(1-12 values). Day function returns back the day of the date. Getdate() function returns the moment the query has run.

How to retrieve the birthday of workers in given days?

The first query, which retrieves the workers whose birthday is in given days, can also be formulated as the following: The idea is, that the actual birthday is shifted to current year by adding the number of years between the year of DOB and the current year.

How to get the current date in SQL?

However, it has another function named GETDATE () that returns the current date and time. To get the current date, you use the CAST () function with the GETDATE () function as shown in the following statement: SELECT CAST ( GETDATE () AS DATE) 'Current Date' ; In this tutorial, you have learned how to use the SQL CURRENT_DATE function to return ...

How to get birthday from DOB to current year?

The idea is, that the actual birthday is shifted to current year by adding the number of years between the year of DOB and the current year. After that, a simple date range comparison is used. The second query which retrieves the workers, whose birthday is on current week can be written like:


2 Answers

SELECT *
FROM Employees
WHERE DATEPART(d, DateOfBirth) = DATEPART(d, GETDATE())
    AND DATEPART(m, DateOfBirth) = DATEPART(m, GETDATE())
like image 174
Miika L. Avatar answered Sep 28 '22 14:09

Miika L.


Ideally a bit more information on the structure of your data sources would help, but a simple answer providing your employee records have a D.O.B. field would be to compare the day and month of this against the current system date in the where clause of your query.

Something along the following lines:

select * from wherever
where
....
(
  datepart(d, EmployeeDOB) = datepart(d, getdate()) and
  datepart(m, EmployeeDOB) = datepart(m, getdate())
)
.....
like image 43
Andy Pollitt Avatar answered Sep 28 '22 16:09

Andy Pollitt