MS Access - DateSerial? - sql

I apologize in advance because I don't know how to properly ask this question.
I am using access to pull in data from several tables and want to filter/query the data by the first 10 days in any month, in any year. I understand that I could use operators to say something like:
=#1/1/2017# and <=#1/10/2017 Or >=#2/1/2017# and <=#2/10/2017# Or....on and on
But that would be a long expression. Is there something easy I could do using DateSerial, or something else?

The Day Function Returns a Variant (Integer) specifying a whole number between 1 and 31, inclusive, representing the day of the month.
So use that function in your query's WHERE clause ...
WHERE Day(YourDateField) BETWEEN 1 AND 10

Use this:
SELECT *
FROM [MyTable]
WHERE DatePart("d", [MyColumn]) > DatePart("d", #01/01/2018#)
AND DatePart("d", [MyColumn]) < DatePart("d", #01/11/2018#)

Related

Is there a way to turn these multiple SQL queries into one?

I have a query that looks like this:
SELECT COUNT(*)
FROM table
WHERE date_field >= '2018-04-08'
AND date_field <= '2018-04-14'
I need to do this 26 times, for the current week and for 25 previous weeks, with each result separated by a carriage return. Is this possible with a single SQL query or do I need to put it in a loop, as I'm now doing?
Note that this is in FileMaker. I don't think that's relevant, but now you know, just in case.
Look into using "group by". Assuming that you are looking at calendar weeks, grouping by the week of the date will give you the counts per week and an extra condition can limit the overall range.
FileMaker has a WeekOfYearFiscal() function. 2nd parameter is the day of week start date.
SELECT WeekOfYearFiscal(dateField;2), COUNT(*)
FROM table
WHERE date_field >= '2018-01-01'
AND date_field <= '2018-04-14'
group by WeekOfYearFiscal(dateField;2)
See this documentation - http://www.filemaker.com/help/12/fmp/html/func_ref1.31.28.html
Give this a go. If the GROUP BY doesn't work on the function, you can nest the inner part and give an alias.

Specifying custom date range in SQL query

I want to write a query where in I need to specify the custom range (instead of hardcoded date range) for date starting from the order day. In the table being used, I have the date for the order.
As of now I have hardcoded the date range like:
where owh.order_day between TO_DATE('2016/07/15','YYYY/MM/DD') and TO_DATE('2017/01/17','YYYY/MM/DD')
where order_day is a date.
But rather I want something like:
where owh.order_day between TO_DATE(owh.order_day - 1,'YYYY/MM/DD') and TO_DATE(owh.order_day +3,'YYYY/MM/DD')
I am doing "-1" as it's "between", so it will take from order_day - order_day+2
For example, If the order_day is: "17/01/2016" then I want the condition to be where the date range is dynamically calculated as: "16/01/2016 - 20/01/2016" .
Is something like this possible? If yes, how can we achieve in in SQL??
The DB in question is Oracle
Any leads appreciated
Since you have not told us which RDBMS you are using, and since you are saying "any leads appreciated", I suppose we are free to give an answer for any RDBMS. The following will work for MySQL:
BETWEEN DATE_SUB( somedate, INTERVAL 1 DAY ) AND DATE_ADD( somedate, INTERVAL 1 DAY )
(source: https://www.tutorialspoint.com/sql/sql-date-functions.htm#function_date-add)

SQL to filter for records more than 30 days old

Suppose I have the following query:
select customer_name, origination_date
where origination_date < '01-DEC-2013';
I would like to select all customers that have an origination date older than 30 days. Is there a way in SQL (oracle, if specifics needed) to specify it in a more dynamic approach than manually entering the date so that I don't need to update the query every time I run it?
Thanks!
Sure try something like this:
select customer_name, origination_date where
origination_date >= DATEADD(day, -30, GETUTCDATE());
This basically says where the origination_date is greater or equal to 30 days from now. This works in Microsoft SQL, not sure but there is probably a similar function on Oracle.
in Oracle, when you subtract dates, by default you get the difference in days, e.g.
select * from my_table where (date_1 - date_2) > 30
should return the records whose date difference is greater than 30 days.
To make your query dynamic, you parameterize it, so instead of using hard coded date values, you use:
select * from my_table where (:date_1 - :date_2) > :threshold
If you are using oracle sql developer to run such a query, it will pop up a window for you to specify the values for your paramteres; the ones preceded with colon.

how to get data whose expired within 45 days..?

HI all,
i have one sql table and field for that table is
id
name
expireydate
Now i want only those record which one is expired within 45 days or 30 days.
how can i do with sql query .?
I have not much more exp with sql .
Thanks in advance,
If you are using mysql then try DATEDIFF.
for 45 days
select * from `table` where DATEDIFF(now(),expireydate)<=45;
for 30 days
select * from `table` where DATEDIFF(now(),expireydate)<=30;
In oracle - will do the trick instead of datediff and SYSDATE instead of now().[not sure]
In sql server DateDiff is quite different you have to provide unit in which difference to be taken out from 2 dates.
DATEDIFF(datepart,startdate,enddate)
to get current date try one of this: CURRENT_TIMESTAMP or GETDATE() or {fn NOW()}
You can use a simple SELECT * FROM yourtable WHERE expireydate < "some formula calculating today+30 or 45 days".
Simple comparison will work there, the tricky part is to write this last bit concerning the date you want to compare to. It'll depend of your environment and how you stored the "expireydate" in the database.
Try Below:-
SELECT * FROM MYTABLE WHERE (expireydate in days) < ((CURRENTDATE in days)+ 45)
Do not execute directly! Depending of your database, way of obtaining a date in days will be different. Go look at your database manual or please precise what is your database.

mysql return rows matching year month

How would I go about doing a query that returns results of all rows that contain dates for current year and month at the time of query.
Timestamps for each row are formated as such: yyyy-mm-dd
I know it probably has something to do with the date function and that I must somehow set a special parameter to make it spit out like such: yyyy-mm-%%.
Setting days to be wild card character would do the trick but I can't seem to figure it out how to do it.
Here is a link to for quick reference to date-time functions in mysql:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
Thanks
I think EXTRACT is the function you are looking for:
SELECT * FROM table
WHERE EXTRACT(YEAR_MONTH FROM timestamp_field) = EXTRACT(YEAR_MONTH FROM NOW())
you could extract the year and month using a function, but that will not be able to use an index.
if you want scalable performance, you need to do this:
SELECT *
FROM myTable
WHERE some_date_column BETWEEN '2009-01-01' AND '2009-01-31'
select * from someTable where year(myDt) = 2009 and month(myDt) = 9 and day(myDt) = 12