I have a tenure table where I have columns link id, name, join date of an employee.
I am trying to get only those employee whose tenure(work experience) is 2 to 5 years from join date to today's date.
I used query
SELECT ID FROM tenure WHERE DATEADD(year, 2, JoinDate) >= '2018-08-06' AND DATEADD(year, 5, JoinDate) <= '2018-08-06';
My tabular structure is
How can I get only those employees whose working period or tenure is between 2 to 5 years from their join date to today's date?
You can use TimeStampDiff() :
SELECT ID
FROM tenure t
WHERE timestampdiff(YEAR, JoinDate, now()) >= 2 AND
timestampdiff(YEAR, JoinDate, now()) <= 5;
use TIMESTAMPDIFF function
SELECT ID FROM tenure WHERE
TIMESTAMPDIFF(YEAR, date(joindate), date(now()))>= 2 and
TIMESTAMPDIFF(YEAR, date(joindate), date(now()))<=5
To me, your syntax suggests SQL Server. The logic would be:
where joindate >= dateadd(year, -5, getdate()) and
joindate < dateadd(year, -2, getdate())
Compare the Joindate to the expected range based on today's date
select *
from tenure
where joindate between DATE_ADD(cur_date(), INTERVAL -5 YEAR)
and DATE_ADD(cur_date(), INTERVAL -2 YEAR)
Related
Why does comparison works weird in this example?
Dates, which are birthdays, are:
1990-03-22
1998-03-20
1990-03-22
2002-12-02
2004-03-18
2004-03-20
2004-03-25
I'm doing this
WHERE DATEADD(YY, 18, jun.birth_date) >= DATEADD(YY, 1, GETDATE())
Which, I think, should cut off all the entries of people older than 18 in a year from today. But it does absolutely opposite, here's what I get:
2004-03-18
2004-03-20
2004-03-25
And I get right results when I seacrh for entries less and even than date in the next year.
Why does it work like this?
This is what your have...
WHERE
someone's date of birth + 18 years
>=
Today's date + 1 year
In English, thats; where the 18th birthday is a year or more in the future.
I think what you really want (if you want people who are 18 or older) is... WHERE the birthdate is earlier than today's date minus 18 years (18 or more years ago)
WHERE
Jun.birth_date <= DATEADD(YY, -18, GETDATE())
If you want people who are 18, or older today, then take 18 years off today, don't add years to the DoB column, as that makes the query non-SARGable:
SELECT birth_date
FROM dbo.YourTable
WHERE birth_date <= DATEADD(YEAR, -18, CONVERT(date,GETDATE()));
Why don't you use datediff, instead of dateadd?
with d as (
select cast('1990-03-22' as date) as birthDate union all
select cast('1998-03-20' as date) as birthDate union all
select cast('1990-03-22' as date) as birthDate union all
select cast('2002-12-02' as date) as birthDate union all
select cast('2004-03-18' as date) as birthDate union all
select cast('2004-03-20' as date) as birthDate union all
select cast('2004-03-25' as date) as birthDate
)
select *
from d
where datediff(year,birthDate, getdate()) >= 18
It's easier to understand the behaviour.
I am trying to update certain fields for employees whose date of joining falls in between 10 Jun and 31 Dec, irrespective of the year. I am trying using 'Between' Operator but it requires year to be included in the dates. Is there a way to generalise it in order to consider Day and Month excluding the Year?
Use the DatePart function - replace thedate with your column, and thetable with the column.
Something like this:
select datepart(MONTH, thedate), datepart(DAY, thedate),*
from thetable
where datepart(MONTH, thedate) between 6 and 12
and datepart(DAY, thedate) between 10 and 31
You may try this:
WITH Emp AS (
SELECT *, DATEPART(MONTH, JoinDate) AS MonthJoin, DATEPART(DAY, JoinDate) AS DayJoin
FROM Employees)
SELECT *
FROM Emp
WHERE (MonthJoin > 1 AND MonthJoin < 12)
OR (MonthJoin = 1 AND DayJoin >= 10)
OR (MonthJoin = 12 AND DayJoin <= 31)
Where Employees is your table and JoinDate is your date of joining in this table
I have a table with birthdates and I want to select all the birthdays that will come in the next 30 days.
The situation is, that all the birthdays are written in the form off 1999-09-15 which means that even if I tried selecting the next 30 days, the birthdays wouldn't show up because the year is 1999.
I am running Microsoft Server 2016.
SELECT * from dbo.EMPLOYEES
WHERE DATE <= DATEADD(day, +30,GETDATE())
and DATE >= getdate()
order by "DATE"
To get the birthdate, we need to work only on the days and the months, not on the year. Thats why we cannot get Where date between 2 dates.
SELECT
dateofbirth_c AS BIRTHDAY
,FLOOR(DATEDIFF(dd,EMP.dateofbirth_c,GETDATE()) / 365.25) AS AGE_NOW
,FLOOR(DATEDIFF(dd,EMP.dateofbirth_c,GETDATE()+30) / 365.25) AS AGE_30_Days_FROM_NOW
FROM
Employees EMP
WHERE 1 = (FLOOR(DATEDIFF(dd,EMP.dateofbirth_c,GETDATE()+30) / 365.25))
-
(FLOOR(DATEDIFF(dd,EMP.dateofbirth_c,GETDATE()) / 365.25))
Try the following; check the month and day because year will not match with birthday year that's why you are not getting any data.
SELECT *
from dbo.EMPLOYEES
WHERE month(DATE)>= month(GETDATE())
and day(DATE) >= day (getdate()) and day(DATE) < = day( getdate()) + 30
order by "DATE"
I'm note sure about microsoft server but on postgres just generate list of days and compare like this.
SELECT * FROM table WHERE to_char(user_dob, 'MM-DD') IN
(SELECT to_char(date, 'MM-DD')
FROM generate_series(current_date, current_date + 30, '1 day') AS date);
And now all you have to change that 30 to any days to make it work.
try this
SELECT *
FROM dbo.EMPLOYEES
WHERE DATEFROMPARTS(YEAR(GETDATE()) , MONTH(Date), DAY(Date)) >= GETDATE()
AND DATEFROMPARTS(YEAR(GETDATE()) , MONTH(Date), DAY(Date)) <= DATEADD(day, +30, GETDATE())
ORDER BY Date
If you want an accurate result that works for leap years and so on, then:
SELECT e.*
FROM dbo.EMPLOYEES e CROSS APPLY
(VALUES (DATEFROMPARTS(YEAR(GETDATE()),
MONTH(e.date),
DAY(e.date)
)
)
) v(this_year_date)
WHERE DATEDIFF(day, GETDATE(), this_year_date) BETWEEN 0 AND 29 OR
DATEDIFF(day, GETDATE(), DATEADD(year, 1, this_year_date)) BETWEEN 0 AND 29
order by "DATE"
I need a SQL select statement to retrieve all employees that their enddate contract will end three month from now and only three month not more or less
It depends what you mean by 3 months from now. If you mean on that exact date, then:
WHERE my_date_column = cast(dateadd(month, 3, getdate()) as date)
Note the cast to date to remove the time component. This is ambiguous and under some circumstances might miss employees or count them twice.
If you want employees whose contract ends in the 3rd month from today, then use:
WHERE DATEDIFF(month, getdate(), my_date_column) = 3
So, if this is January, this will return employees whose contract ends any time in April.
SELECT *
FROM TABLE_NAME
WHERE Date_Column >= DATEADD(DAY, +90, GETDATE())
OR
SELECT *
FROM TABLE_NAME
WHERE Date_Column >= DATEADD(MONTH, +3, GETDATE())
OR
SELECT *
FROM TABLE_NAME
WHERE DATEDIFF(MONTH, my_date_column, GETDATE()) <= 3
I have a DateTime column named EXP_Date which contains date like this :
2014-07-13 00:00:00.000
I want to compare them, like this query :
SELECT COUNT(*)
FROM DB
WHERE ('2014-07-15' - EXP_DATE) > 1
I expect to see the number of customers who have their services expired for over a month.
I know this query wouldn't give me the correct answer, the best way was if I separate the Year / Month / Day into three columns, but isn't any other way to compare them as they are?
You can use DATEADD
SELECT COUNT(*)
FROM DB
where EXP_DATE < DATEADD(month, -1, GETDATE())
Try this
SELECT COUNT(*)
FROM DB
where DATEADD(month, -1, GETDATE()) > EXP_DATE
SELECT COUNT(EXPIRE)FROM
(Select CASE WHEN EXP_DATE < DATEADD(month, -1, GETDATE())THEN 1 ELSE 0 END)AS EXPIRE FROM DB
)tt
Another way using DATEDIFF
SET DATEFORMAT DMY --I like to use "dateformat"
SELECT COUNT(*)
FROM DB
WHERE (DATEDIFF(DAY,#EXP_DATE,GETDATE())) >= 30 --Remember, instead "Day" you can use week, month, year, etc
Syntax: DATEDIFF ( datepart , startdate , enddate )
Depart: year, quarter, month, day, week...
For more information you can visit MSDN