I'm using this query to select data between dates:
SELECT id, date FROM table WHERE date BETWEEN '09/10/2015' and '09/23/2015';
I'm getting these results:
1 09/11/2015
2 09/13/2015
3 09/16/2015
4 09/21/2015
5 09/21/2015
6 09/21/2015
7 09/22/2015
8 09/23/2015
But when I change the date to one year before, instead of showing all these data again, It's returning a result ignoring the year:
SELECT id, date FROM table WHERE date BETWEEN '09/15/2014' and '09/23/2015';
Result:
3 09/16/2015
4 09/21/2015
5 09/21/2015
6 09/21/2015
7 09/22/2015
8 09/23/2015
What am I missing?
Edit:
Date column datatype: varchar.
It looks like you are committing a sin . . . storing date values as strings. This is bad. SQL has built-in data types for dates and a wealth of functions.
So, you need to convert the data, using str_to_date():
SELECT id, date
FROM table
WHERE str_to_date(date, '%m/%d/%Y') BETWEEN '2015-09-10' and '2015-09-23';
MySQL doesn't know that you intend for some string column to be a date, so all the comparisons are done as strings. You should also get used to using the ISO standard YYYY-MM-DD format for representing dates.
Try this ....
SELECT id, date FROM table WHERE date BETWEEN STR_TO_DATE('09/10/2015','%d/%m/%Y') and STR_TO_DATE('09/23/2015','%d/%m/%Y');
Related
I have a sample table here with the following columns and sample records. I want to be able to sum my column cases using with a specific date range (the helper column).
I want to get my results this way:
Sum all cases WHERE date range is in between 2022-03-23 - 2022-04-01 and so on.
date range
Sum of Cases
2022-03-23-2022-04-01
5 (sample result only)
2022-03-24-2022-04-02
9 (sample result only)
The logic of the date range is always n - n9 days.
I 've tried this type of query but it does not work, it there a way for me to get this without have to use a query to create another column?
SELECT Date,
sum([QUERY 1]) as "Reports 7 days prev",
sum ([QUERY 2]) as "Reports 7 days after"
FROM REPORTS
GROUP BY Date
Data:
Date
BuyerID
Cases
Helper (Date Range)
4/1/2022
20001
2
2022-03-23-2022-04-01
4/1/2022
20001
1
2022-03-23-2022-04-01
4/2/2022
20002
3
2022-03-24-2022-04-02
4/5/2022
20003
5
2022-03-27-2022-04-05
4/7/2022
20004
6
2022-03-29-2022-04-07
4/7/2022
20005
9
2022-03-29-2022-04-07
Are you looking to get total cases for last X number of days? What does your initial data look like?
you can try something like:
Step 1: You aggregate all the cases for each date.
WITH CASES_AGG_BY_DATE AS
(
SELECT Date,
SUM(Cases) AS Total_Cases
FROM REPORTS
GROUP BY Date
),
Step 2: you aggregate the last 7 days rolling cases sum for each date
LAST_7_DAY_AGG AS
(
SELECT Date, SUM(Total_Cases) OVER(ORDER BY Date ASC ROWS BETWEEN 7 PRECEDING AND CURRENT ROW) AS sum_of_cases,
LAG(Date, 7) AS 7th_day
FROM CASES_AGG_BY_DATE
)
Step 3: create final output and concatenate date and 7th day before that
SELECT Date, CONCAT(Date, "-", 7th_day), sum_of_cases
FROM LAST_7_DAY_AGG;
I have a date column in my table which is unfortunately in strings and has an inconsistent format(it has a mixed format).
for e.x:
for month march, day 1, year 2022 date can be represented in
-> 1-3-22 or 01-03-2022
how can I extract the day, month, and year separately from the date column?
description:
table name -> employee
column I'm focusing on -> date -> type-> string
language -> SQL
DB -> MySQL
In MySQL you can use the STR_TO_DATE functions to parse a string into a date type, then use the DAYOFMONTH, MONTH and YEAR functions to extract the components of interest.
For example, on current MySQL 8.x versions, using LATERAL to simulate an OUTER APPLY, you can do this:
create table Example (
Stringy varchar(10)
);
insert Example (Stringy) values ('1-3-22'), ('01-03-2022');
select Stringy, Datey, dayofmonth(Datey) "Day", month(Datey) "Month", year(Datey) "Year"
from Example
left join lateral (
select str_to_date(Stringy, '%d-%m-%Y') "Datey"
) S2D on 1=1;
Which returns the results:
Stringy
Datey
Day
Month
Year
1-3-22
2022-03-01
1
3
2022
01-03-2022
2022-03-01
1
3
2022
your data is string value with this form 'Day-Month-Year' that is separated by -, In MySQL you can use SUBSTRING_INDEX to split string based on charterer and change it into your desired result
-- a test data '01-03-2022'
SELECT
SUBSTRING_INDEX('01-03-2022','-', -1) AS year1,
SUBSTRING_INDEX(SUBSTRING_INDEX('01-03-2022', '-', 2),'-', -1) AS month1,
SUBSTRING_INDEX('01-03-2022','-', 1) AS day1
In SQL Server use _PARSENAME(REPLACE) as follows
-- a test data '01-03-2022'
SELECT
PARSENAME(REPLACE('01-03-2022' ,'-','.'),1) year1,
PARSENAME(REPLACE('01-03-2022' ,'-','.'),2) month1,
PARSENAME(REPLACE('01-03-2022' ,'-','.'),3) day1
I have both the week number and their corresponding day of week(i.e. mon,tue,wed,.....) stored in tables.
The following code is supposed to return week number from date but I'm unable to turn this around
select WEEKOFYEAR(CURDATE())
My table:
RecordID|Record|WeekID|DayofWeek
--------------------------------
1 |text1 |43 |mon
2 |text2 |43 |tue
3 |text3 |44 |wed
Desired output:
RecordID|Record|Date
--------------------------------
1 |text1 |2019/10/30
2 |text2 |2019/10/31
3 |text3 |2019/11/01
I want to retrieve the date from them(assuming current year). Is it possible in sql or can it be done only on server side?
*Dates just for representation
This following sample script might help you. Hope all necessary values are available in your database and you have pass them to the function accordingly-
SELECT STR_TO_DATE('2013 10 Tuesday', '%X %V %W');
--2013 is the year value
--10 is the week number
--Tuesday is the day name
If you have all three values available in your table and run the STR_TO_DATE function providing appropriate values - this will return you a date like - "2013-03-12".
You can check the below script-
SELECT
STR_TO_DATE(concat('2019',' ', WeekID,' ', DayofWeek), '%X %V %W')
FROM (
SELECT 1 RecordID, 'text1' Record, 43 WeekID,'mon' DayofWeek UNION ALL
SELECT 2,'text2',43,'tue' UNION ALL
SELECT 3,'text3',44,'wed'
)A;
Your final query should be as below-
SELECT
STR_TO_DATE(concat('2019',' ', WeekID,' ', DayofWeek), '%X %V %W')
FROM your_table_name A;
Note: Year 2019 is fixed as this value is not available in your table. If available, you can also use that column dynamically as other columns are used.
Your question isn't completely clear.
I guess you have the columns
year with values like 2001
WeekID with values like 36
DayOfWeek with values like tue.
Then, you can use an expression like this to get the DATE value. MySQL has date format strings for week and weekday.
SELECT STR_TO_DATE (CONCAT(year, '/', week, '/', weekday), '%Y/%v/%a')
Here's a fiddle. https://www.db-fiddle.com/f/iGrnkM5WgWTVPxuqxfPxdK/0
But beware, the computation of week number is a business rule subject to local and international standards. Be sure to test with dates in the first few days of several different calendar years to make sure you understand your situation.
You can read about the choices for week computation here. You use WEEKOFYEAR() to retrieve the week number; that corresponds to the %v format specifier.
You cant get the date from just week number and day of week, you would need the year too.
I have MySQL table
id product p_image
1 G images\20131030164545.jpg
2 S images\20131230164545.jpg
3 V images\20140110164545.jpg
4 R images\20140320164545.jpg
5 K images\20140526164545.jpg
6 L images\20150110164545.jpg
7 SK images\20150120164545.jpg
Here I need to extract products from above table where p_image timestamp between two dates (for example I need to extract from 2013/12/01 to 2014/07/30 dates)
In this query I need to extract timestamp from this string 'images\20140526164545.jpg' and convert this to date format and select values between two dates.
Assuming the format of the string is fixed (which it looks to be) you can use thesubstrfunction to extract the timestamp and then cast it to a date and filter by it. Something like this should work:
select * from table1
where cast(substr(p_image FROM 7 FOR 14) as date)
between '2013/12/01' and '2014/07/30'
Sample SQL Fiddle
There might be more efficient ways to do this, but this should give you an idea to start with.
Edit: if the string can vary the something like left(right(p_image, 18), 14) should work.
The dates in p_image are in YYYYMMDD format, so you can compare them as strings. That is, there is no reason to convert the strings to a date data type.
Hence you can just do:
where substr(p_image, 8, 8) between '20131201' and '20140730'
If the position of the date is not fixed but always after the /, you can do:
where left(substring_index(p_image, '/', -1), 8) between '20131201' and '20140730'
Try this it will working :
select * from `datetest` where substr(p_image, 8, 8) between '20131201' and '20140730'
Screenshot of Phpmyadmin :
How can add 1 year to a date value stored in the DB as varchar (format: "2011.03")?
I'm trying with this, but returns NULL :(
DATE_FORMAT(DATE_ADD(STR_TO_DATE('2011.03', '%Y.%m'), INTERVAL 1 YEAR),'%Y.%m')
Thank you very much!
edit.: this is the query i want to use in:
SELECT DISTINCT column FROM table WHERE column BETWEEN '2011.03' AND DATE_FORMAT(DATE_ADD(STR_TO_DATE('2011.03', '%Y.%m'), INTERVAL 1 YEAR),'%Y.%m') ORDER BY column DESC
("2011.03" is a parameter value and comes outside)
What you are trying is giving correct result
Try :
SELECT DATE_FORMAT(DATE_ADD(STR_TO_DATE('2011.03', '%Y.%m'), INTERVAL 1 YEAR),'%Y.%m')
it's giving 2012.03.
Edit:
You can use below trick to make it workable in mysql version > 5.0
Add month with date because in Mysql version > 5.0 str_to_date() sometimes would return NULL if the %D format specifier was not the last specifier in the format string input.
Try below:
SELECT DATE_FORMAT(DATE_ADD(STR_TO_DATE('2011.03.01', '%Y.%m.%d'), INTERVAL 1 YEAR),'%Y.%m')