Two date columns need to compare with current system date, if those falls into it, display into table
You are working with dates, so I suspect you want:
where start_date <= convert(date, getdate()) and
end_date >= convert(date, getdate())
Related
I have a column in a database table called EXPIRATION_DATE and need to filter my results to only bring in rows where the date in this column is within the last year.
The date format in the column is 18-JUN-21.
I imagine the filter need to be something like
WHERE EXPIRATION_DATE >= "logic to get todays date minus a year"
but I don't know what this logic is.
Thoughts?
Try with add_months :
WHERE EXPIRATION_DATE >= add_months( trunc(sysdate), -12);
I would do
where EXPIRATION_DATE >= sysdate - interval '1' year ;
WHERE EXPIRATION_DATE >= DATEADD(month, -12, GETDATE())
Or try the following:
WHERE EXPIRATION_DATE >= DATEADD(year, -1, GETDATE())
Although this is for T-SQL and I just noticed your tag is for ORACLE. I'll leave this here though for anyone using SSMS etc.
I have codes as below:
select date
from date_table
where date between '2019-05-01' and '2019-07-31'
and date between '2020-05-01' and '2020-07-31'
but when i run the code above it shows no rows to display despite i run successfully when only using
select date
from date_table
where date between '2019-05-01' and '2019-07-31'
Does anyone know why?
Presumably you want to OR these two date ranges:
SELECT date
FROM date_table
WHERE
date BETWEEN '2019-05-01' AND '2019-07-31'
OR
date BETWEEN '2020-05-01' AND '2020-07-31';
ANDing together the two ranges will never return any records, since the two ranges are mutually exclusive, and any date can only fall into one of the ranges.
As a side note, if you want to include 1st May 2019 up to, and including, 31st July 2019 (with similar logic on the other range as well), then you should be using inequalities:
SELECT date
FROM date_table
WHERE
date >= '2019-05-01' AND date < '2019-08-01'
OR
date >= '2020-05-01' AND date < '2020-08-01';
I want the exact date as 22/06/2015 from the query
whose Joining date should be exact
22/06/2015
which is exact 6 months back from todays date
I tried like below
Select date_of_joining,* from emp_mst Where Dt_Of_Join >= Dateadd(Month, Datediff(Month, 0, DATEADD(m, -6, getdate())), 0)
but it didn't worked.
what is the exact query for that ?
I am using SQL- server- 2005
if you want the EXACT joining date ( /date_of_joining.... /Dt_Of_Join)
what about
select distinct employee.name from emp_mst where date_of_joining = DATEADD(month, -6, GETDATE())
or if you want the actual date returned in a different format:
CONVERT(Date,DATEADD(month, -6, GETDATE()), 103)
which is applicable if you select this field
I'm not a SQL Server guru, but I found easy answers everywhere for this.
Try this link to another post which explains this exact question SQL Server 2005: how to subtract 6 month
You refer to the word "exact" date, so you don't need the datediff section, you can just subtract 6 months from the current date using "dateadd" which will give you a precise date. Just remember to correctly type cast else you will have to be accurate to the millisecond.
SELECT employee_name
FROM emp_mst
WHERE Dt_Of_Join = Cast(DATEADD(month, -6, GETDATE()) As Date)
ORDER BY Dt_Of_Join DESC
I think he just need to know how to remove time from date
Note that you need to handle time part effectively
Select * from emp_mst
Where
Dt_Of_Join >= dateadd(month,-6,dateadd(day,datediff(day,0,getdate()),0)
and
Dt_Of_Join < dateadd(month,-6,dateadd(day,datediff(day,0,getdate()),1)
You want previous 6 month date so use dateadd() and for your date formate DD/MM/YYYY you should try to convert(varchar(10),date,101).
Select date_of_joining,* from emp_mst Where
Dt_Of_Join=convert(varchar(10),dateadd(month,-6,getdate()),101)
I've got 2 date columns in my table (start_date, end_date).
I've tried Datediff(day, start_date, end_date), but I was prompt with:
invalid column name
How can I calculate the date difference between these 2 columns?
select DATEDIFF (day,start_date,end_date) from yourtablename;
Should be Datediff(day, start_date, end_date). There is no 's' at the end of the day
http://technet.microsoft.com/en-us/library/ms189794.aspx
It should be "day"
Datediff(day, start_date, end_date)
be certain that your columns are formated correctly to either be a DATE or DATETIME
I created a table in which I have entered the rates of hotels with dates from which date to which date those rates are valid. What i want in another form if i select a particular date in the rates for particular hotel should be displayed for specific date that falls under whichever slab of dates because every date slab have different hotel rates
I have tried this
select * from test_hotelrates where Datefrom >= convert(date, '2013-10-01', 103)
and Dateto <= convert(date, '2013-10-31', 103) and Season = 'Season'
but it doesn't work as per my need
It does not work because you use 2 different dates to compare with. In this example you want the rate for all the days of October. I assume someone wants to stay a full month at a hotel. But I think you want to check every single day in that time period, for the current rate. Because in that period the rates can change.
Your query will only return rates that are WITHIN the timeframe 10-01-2013 to 10-31-2013.
I think you will need to make a different query or run this one multiple times
select * from test_hotelrates where Datefrom <= convert(date, '2013-10-01', 103)
and Dateto >= convert(date, '2013-10-01', 103) and Season = 'Season'
select * from test_hotelrates where Datefrom <= convert(date, '2013-10-02', 103)
and Dateto >= convert(date, '2013-10-02', 103) and Season = 'Season'
select * from test_hotelrates where Datefrom <= convert(date, '2013-10-03', 103)
and Dateto >= convert(date, '2013-10-03', 103) and Season = 'Season'
Etc...
Change your query as below:
select * from test_hotelrates where convert(varchar(10),Datefrom,103) >= convert(varchar(10), '2013-10-01', 103)
and convert(varchar(10),Dateto,103) <= convert(varchar(10), '2013-10-31', 103) and Season = 'Season'
as both formats should be same. Hope this will work for you