I need to select records based on a range of month/day values, disregarding the year. I am querying an Access database, and I found a query here but it seems to select a range of years.
SELECT *
FROM Factory
WHERE YEAR(date) BETWEEN 1998 AND 1999
I have tried this query, but it only shows birthdays in the current year :
SELECT * FROM user where birthday Between #09/05# and #10/10#;
In my database the birthday column contains the year to count how old they are. What query can I use to perform what I want?
Try
SELECT * FROM user
WHERE (month(birthday) * 100) + day(birthday) between 0131 and 1231
Try this.
SELECT *
FROM user
WHERE birthday between #1991/12/31# and #1992/01/31#;
OR
SELECT *
FROM user
WHERE birthday between #12/31/1991# and #01/31/1992#;
OR
SELECT * FROM
user
WHERE birthday between #1991/12/31# and #1992/01/31#
Related
I have created a employee table with column as Date_Of_Joining.
I have entered the data in YYYY-MM-DD Format.
Now I want to retrieve the data of the employee only by using the YEAR.
For example if I have entered Date_Of_Joining as 2010-10-15, 2012-05-18 and 2008-04-16.
Now I want the details of the employee whose Date_Of_Joining is after 2010.
try do a query like this
select * from employee-table where year(Date_Of_Joining) > 2010
Replace "employee-table" with your table name.
This query will give all the data that is after year 2010
SELECT * FROM Employee WHERE YEAR(Date_Of_Joining) > 2010
If Date_Of_Joining datatype is DATE the query will work, if the data type is varchar, then you need to convert it into DATE format then apply the query.
In case if you want to filter with month and year, you can use as
SELECT * FROM Employee
WHERE YEAR(Date_Of_Joining) > 2010 AND MONTH(Date_Of_Joining) > 1
If the range between Jan 2010 and May 2013, you can use as
SELECT * FROM Employee
WHERE (YEAR(Date_Of_Joining) >= 2010 AND MONTH(Date_Of_Joining) >= 1)
OR (YEAR(Date_Of_Joining) <= 2013 AND MONTH(Date_Of_Joining) <= 5)
just follow this
SELECT YEAR(Date_Of_Joining)
it is the general format to get year
I strongly recommend that you phrase this as:
Date_Of_Joining >= '2011-01-01'
That is, you should avoid using the expression YEAR(Date_Of_Joining). Why? Using a function on a column prevents certain optimization strategies -- especially those using indexes and partitions.
For this particular example, you probably have a small amount of data, so it won't actually affect the performance of this query. But you might as well learn how to write with performance in mind.
I'm querying imported data that has a date/time field that I can't format as date in the table.
Sample:
Ticket Name Date
INC000101 User1 9/5/2016 10:00:34AM
INC000102 User2 9/5/2016 12:02:00PM
INC000103 User1 9/7/2016 3:34:00PM
INC000104 User2 10/1/2016 9:30:23AM
INC000105 User1 10/5/2016 10:20:00AM
INC000106 USer2 10/6/2016 4:56:00PM
I'm trying to get a count of how many tickets each user has per month. Because the Date field comes from the database as a text field, I can't seem to make that format as date/time so I use "left" to filter by month. This is what I've used to get a return on a single User item for the month of October.
SELECT COUNT(*)
FROM 2016YTD
WHERE [Name]='User1' AND left(Date,3) = '10/';
I would like to add counts for User2 through UserX per month so that I can get a count row or column for each the quantity of tickets for each user each month in one report. Everything I've tried won't save the query due to syntax errors in one form or another. I've tried variations of the following query help post as well without success.
SELECT a.distributor_id,
(SELECT COUNT(*) FROM myTable WHERE level='personal' and distributor_id = a.distributor_id) as PersonalCount,
(SELECT COUNT(*) FROM myTable WHERE level='exec' and distributor_id = a.distributor_id) as ExecCount,
(SELECT COUNT(*) FROM myTable WHERE distributor_id = a.distributor_id) as TotalCount
FROM myTable a ;
I'm sure the answer is staring at me, just not sure what at the moment.
Thanks for reading
John
This is only an answer to your first question about how to deal with dates that are stored in text fields.
To get the count for all users for every month you can do:
SELECT [Name], Format([Date],'mmmm') AS Month, COUNT(*) as Count
FROM 2016YTD
GROUP BY [Name], Format([Date],'mmmm')
A text field containing a date that is always in the same format can be treated as a date with Format() so Format([Date],'mmmm') returns the full month name for each date.
you should just need conditional aggregation. I haven't looked at access in a while but probably just something like this:
SELECT
a.distributor_id
,Format([Date],'mmmm') as Month
,SUM(IIF(level='personal',1,0)) as PersonalCount
,SUM(IIF(level='exec',1,0)) as ExecCount
,COUNT(*) as TotalCount
FROM
myTable a
GROUP BY
a.distributor_id
,Format([Date],'mmmm')
i want to access date from my table and i only want only particular dates to display (for eg: date=1) .
Is there any way to access date,month and year individually from date format?
please help
Avinash is this what you are looking for ?
select MONTH('10/15/1981') --10
select DAY('10/15/1981') --15
select YEAR('10/15/1981') --1981
Select Dates
from tablename
Where MONTH(Dates) = 10
SQL isn't my greatest strength and I need some help building a select statement.
Basically, this is my requirement. The table stores a list of names and a timestamp of when the name was entered in the table. Names may be entered multiple times during a week, but only once a day.
I want the select query to return names that were entered anytime in the past 7 days, but not today.
To get a list of names entered today, this is the statement I have:
Select * from table where Date(timestamp) = Date(now())
And to get a list of names entered in the past 7 days, not including today:
Select * from table where (Date(now())- Date(timestamp) < 7) and (date(timestamp) != date(now()))
If the first query returns a set or results, say A, and the second query returns B, how can I get
B-A
Try this if you're working with SQL Server:
SELECT * FROM Table
WHERE Timestamp BETWEEN
dateadd(day,datediff(day,0,getdate()),-7),
AND dateadd(day,datediff(day,0,getdate()),0)
This ensures that the timestamp is between 00:00 7 days ago, and 00:00 today. Today's entries with time greater than 00:00 will not be included.
In plain English, you want records from your second query where the name is not in your first query. In SQL:
Select *
from table
where (Date(now())- Date(timestamp) < 7)
and (date(timestamp) != date(now()))
and name not in (Select name
from table
where Date(timestamp) = Date(now())
)
not in
like
select pk from B where PK not in A
or you can do something like
Select * from table where (Date(now())- Date(timestamp) < 7) and (Date(now())- Date(timestamp) > 1)
How best store year, month, and day in a MySQL database so that it would be easily retrieved by year, by year-month, by year-month-day combinations?
Let's say you have a table tbl with a column d of type DATE.
All records in 1997:
SELECT * FROM tbl WHERE YEAR(d) = 1997
SELECT * FROM tbl WHERE d BETWEEN '1997-01-01' AND '1997-12-31'
All records in March of 1997:
SELECT * FROM tbl WHERE YEAR(d) = 1997 AND MONTH(d) = 3
SELECT * FROM tbl WHERE d BETWEEN '1997-03-01' AND '1997-03-31'
All records on March 10, 1997:
SELECT * FROM tbl WHERE d = '1997-03-10'
Unless a time will ever be involved, use the DATE data type. You can use functions from there to select portions of the date.
I'd recommend the obvious: use a DATE.
It stores year-month-day with no time (hour-minutes-seconds-etc) component.
Store as date and use built in functions:day(), month() or year() to return the combination you wish.
What's wrong with DATE? As long as you need Y, Y-M, or Y-M-D searches, they should be indexable. The problem with DATE would be if you want all December records across several years, for instance.
This may be related to the problem that archivists have with common date datatypes. Often, you want to be able to encode just the year, or just the year and the month, depending on what information is available, but you want to be able to encode this information in just one datatype. This is a problem which doesn't apply in very many other situations. (In answer to this question in the past, I've had techie types dismiss it as a problem with the data: your data is faulty!)
e.g., in a composer catalogue you are recording the fact that the composer dated a manuscript "January 1951". What can you put in a MySQL DATE field to represent this? "1951-01"? "1951-01-00"? Neither is really valid. Normally you end up encoding years, months and days in separate fields and then having to implement the semantics at application level. This is far from ideal.
If you're doing analytics against a fixed range of dates consider using a date dimension (fancy name for table) and use a foreign key into the date dimension. Check out this link:
http://www.ipcdesigns.com/dim_date/
If you use this date dimension consider how easily it will be to construct queries against any kind of dates you can think of.
SELECT * FROM my_table
JOIN DATE_DIM date on date.PK = my_table.date_FK
WHERE date.day = 30 AND
date.month = 1 AND
date.year = 2010
Or
SELECT * FROM my_table
JOIN DATE_DIM date on date.PK = my_table.date_FK
WHERE date.day_of_week = 1 AND
date.month = 1 AND
date.year = 2010
Or
SELECT *, date.day_of_week_name FROM my_table
JOIN DATE_DIM date on date.PK = my_table.date_FK
WHERE date.is_US_civil_holiday = 1