SQL query to find date between a range of dates - date

Table X has start_date and end_date and related information associated with these dates. BOTH stored in DB in date format.
I need to find a specific date say 12th-Jan-2000 and extract the rows whose date range includes this date.
Could someone please help me out. I am new to SQL so need some guidance here.
Table X:
ID |start_date|end_date
1 |12/30/1999|01/12/2000
2 |01/20/2000|01/30/2000
3 |01/07/2000|01/15/2000
Thus my query should give back the ID-3 since 12th January falls in the range 01/07/2000-01/15/2000
Thanks

use the BETWEEN operator:
SELECT *
FROM TableX
WHERE DATE'2000-01-12' BETWEEN start_date AND end_date

Related

sql (or postgres) how to set the date as single year and month?

I am compiling data of multiple years but I only need time not date and year. How can I set the year and date data into a single year and a single date so that I can use the time information as accumulative data?
2020-01-01 + time value.
Thanks!!!!
The information you provided is a little light. Still with certain assumptions:
create table ts_sum (ts_fld timestamptz);
insert into ts_sum values ('2020-04-14 08:15:32'), ('2021-09-27 18:45:01'), ('2022-01-09 20:21:05');
select sum(to_char(ts_fld, 'HH24:MI:SS')::interval) from ts_sum;
sum
----------
47:21:38
Needs to be tested with your data. The procedure is extract the time portion out of the timestamp using to_char then cast that to an interval and then sum the time intervals.

Power Query - Subtract the earliest date in one column from the record-specific date in another column

Every month I download a set of data into a report. That data consists of multiple records and each record has a record specific date as well as having the month end report date on the record's data-row.
I have used Power Query to upload all of these month end reports. I want use Power Query to be able to compare the column of record dates with the earliest date in the column of report dates to see if anybody has fiddled any data entry. The query table has the following headings.
Record ID Record Date Report Date
I've tried adding a custom column using the formula = if Record Date < List.Min(Report Date) then "Old" else "New"
this didn't work and I've spent ages trying to get a solution. I've also tried using Groups to get the minimum value, but I lose all of the other columns, which I want to keep. Any help really appreciated.
You have to refer to the fields in [], so here [Report Date]
To pick a column use Source[Field], so here #"PriorStep"[Report Date]
The List.Min function is not pulling as a number so you cant use <
Insert a Number.From in front of the calculation to convert to number
Same need to add Number.From in front of [Record Date] pulling as a date
Combined code:
#"Added Custom" = Table.AddColumn(#"PriorStep", "Custom", each if Number.From([Record Date])<Number.From(List.Min(#"PriorStep"[Report Date])) then "Old" else "New")

To filter sales based on max date value minus 6 months

I'm trying to filter the data based on the date filter. The date column in my table is in date9. format(30JUN2017).
I want to filter the date column by subtracting 6 months from the existing date, which will be 31DEC2017.
e.g: date<31DEC2017
Can anyhow suggest on how to do this, I have tried using intx function and other options as well.
Thanks for your help.
Best Regards,
AJ
When manipulating dates in SAS using their formatted values you should enclose them in ""d, i.e. in your example it should be where date<"31DEC2017"d. If your date is stored in date9 format as a string, you can make it into a date like so: where input(date,date9.)<"31DEC2017"d
Now your question seems to differ slightly from its title. According to the latter, you want to filter on max_date - 6 months, whatever that max date might be in your dataset. This could be done so:
proc sql;
select *
from t
where intnx('month',date,6)<(select max(date) from t)
;
quit;
If however, for some strange reason, by "max date" you mean the current date, the above query simply becomes this:
proc sql;
select *
from t
where intnx('month',date,6)<date()
;
quit;

Cast varchar as date select distinct top 100

I am trying to fix a query that has come to light in SSRS after the new year. We have an input that comes from another application. It grabs a date and stores it as varchar. The SSRS report then fetches the top 100 'dates' but when 2017 dates have come around, this are not in the top 100.
The existing query is as follows
SELECT DISTINCT TOP (100)
FROM DenverTempData
ORDER by BY Date DESC
The date is stored as VARCHAR. So obviously this query doesn't grab a value such as 01012017 as being a top 100 (over values likes 12312016). I thought maybe I can simply change the datatype on this column to datetime. But the information comes from a flat file and is converted, so it's a little more difficult that that. So I'm hoping to do a select of the distinct top 100 while converting the date column to datetime or just date and grabbing the last 100 dates.
Can someone help with the query syntax? I'm thinking a cast to convert varchar to date, but how do I format with distinct top 100? I'm simply looking to retrieve the last 100 dates in chronological order from a column that is stored as varchar but contains a string representing a date.
Hopefully that makes sense
It is always a bad idea to store a date as string. This is highly culture specific!
You can cast your formatted string-date to a real date like this:
DECLARE #DateMMDDYYYY VARCHAR(100)='12312016';
SELECT CONVERT(DATE,STUFF(STUFF(#DateMMDDYYYY,5,0,'-'),3,0,'-'),110)
After the conversion your sorting (and therefore the TOP 100) should work as expected.
My strong advise: Try to store your dates in a column of a real date type to avoid such hassel!
SELECT DISTINCT TOP 100 (CAST(VarcharColumn as Date) as DateColumn)
FROM TABLE
Order by DateColumn desc

Query 2 fields Date Range Overlap Date Range

So this is a new one, I am thinking. We have an Access query with 2 date fields fdate1 and fdate2. The fdate1 is always the first date, and fdate2 is always the second. The two are a range. What we need to do is query the table to find all the records where the record is at any point in the year 2010. So for instance, here is some pretend data:
Fname fdate1 fdate2
John 2/18/2008 5/08/2014
Mary 1/6/2010 6/21/2010
Jane 9/25/2010 4/13/2012
We need to know any records that involve the date range of 1/1/2010 - 12/31/2010. As you can see, the above records all match, but because they are 2 separate fields, I am not sure how to find that those 2 columns represent a date range and that date range does or does not overlap with the date range criteria. Make sense?
Any help is appreciated.
One approach would be to place the criteria >DateSerial(2010,1,1) on fdate2, and <DateSerial(2011,1,1) on fdate1.
select * from tablename
Where (fdate1 between '1/1/2010' and '12/31/2010') OR (fdate2 '1/1/2010' and '12/31/2010')