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
Related
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 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)
i have table with DOB column ('2012-05-29 00:00:00.000') and few other fields , i need to select the data for DOB between 6 months to 6 Years. I tried using the below SQL but this is not giving me the right data. any help will be appreciated.
select * from dbo.xyz
where ( FLOOR(DATEDIFF(MONTH, birth_date , GETDATE()) % 12) >=6
AND FLOOR(DATEDIFF(DAY, birth_date , GETDATE()) / 365.25) <= 6
)
When using dates, the advice is to use functions only on the non-column values. In other words, modify getdate(), not birth_date:
select *
from dbo.xyz
where birth_date between dateadd(year, -6, getdate()) and dateadd(month, -6, getdate())
This has two advantages. First, it makes the where clause "sargable", which means an index can be used on the comparison. More importantly, the alternative of using datediff() doesn't quite work as expected. datediff() counts the number of calendar boundaries between two values. So, 2014-12-31 and 2015-01-01 are one day apart, one month apart, and even one year apart.
Try this
select * from dbo.xyz
where DATEDIFF(MONTH, birth_date , GETDATE()) between 6 and 72
Here is another option that will allow indexing on birthdate.
select *
from dbo.xyz
where birthdate > DATEADD(YEAR, -6, GETDATE())
and birthdate < DATEADD(MONTH, -6, GETDATE())
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
In SQLExpress, I have a table that contains a datetime-column. It is formatted like this:
19.03.2012 00:00:00
Now, there are a lot of dates in there and I want to build a WPFChart, that shows me, how much dates are in march, in april and so on.
How can I manage this in sql that I get the count of one month?
Use:
select month(dateColumn), count(*)
from table
group by month(dateColumn)
You can extract the month of a date with Month() funciton.
than with a simple group by, you get the count for every month
To get only one month...
SELECT
COUNT(*),
SUM(valueColumn)
FROM
yourTable
WHERE
dateColumn >= '20120101'
AND dateColumn < '20120201'
To get multiple months, but grouped by month (and accounting for year).
SELECT
DATEADD(MONTH, DATEDIFF(MONTH, 0, dateColumn), 0),
COUNT(*),
SUM(valueColumn)
FROM
yourTable
WHERE
dateColumn >= '20110301'
AND dateColumn < '20120301'
GROUP BY
DATEADD(MONTH, DATEDIFF(MONTH, 0, dateColumn), 0)