MYSQL - Retrieving weeks in a month - mysql

I'm trying to list all weeks in a month, (e.g., 2015-02-01 and 2015-02-28) in the following format: (week yyyy-mm-dd to week yyyy-mm-dd etc.)
Tried using WEEK e.g., SELECT WEEK(2015-02-01) - WEEK(2015-02-28); but this just gave me the number of numbers in the month - 4.
What is the proper MYSQL statement to achieve this?

DATE_FORMAT should work, have you tried it that way :
select date_format(your_date, '%Y-%m-%d')
from yourTable;
If your field is a string you could also use str_to_date
select str_to_date(your_date, '%Y-%m-%d')
from yourTable;

Related

What is the MySQL Workbench version of DATEDIFF(YEAR,C.END_DT,GETDATE())?

I understand the syntax for the current date will change from GETDATE() to SYSDATE().
How do we write the difference between the date from c.end column and the current date in terms of years in MySQL?
Do we use TIMESTAMPDIFF() ?
There are many ways to get current year from MySQL, you just have to extract it using YEAR() function like this:
SELECT YEAR(NOW());
NOW() returns todays date + time in this format 'YYYY-MM-DD HH:MM:SS' so with YEAR(), you're just taking the year value from NOW(). Apply the same thing to the date field in your table like YEAR(C.END_DT) and do a subtraction between those year values. A simple query like this should work:
SELECT YEAR(NOW()) - YEAR(C.END_DT)
FROM mytable C;
But if you still want to use DATEDIFF, you can write something like this:
SELECT FROM_DAYS(DATEDIFF(NOW(),C.END_DT)) FROM mytable C;
DATEDIFF here return the differences in total days and using FROM_DAYS(), it will return the total year, month and day from the specific date in the comparison.
Refer to this updated fiddle

SQL between date with dd.mm.yyyy only matches on day

I want to use a Datepicker to select specific entries in my Database.
The Problem i face is that the entries i get as a reposnse from my query are only sorted for the day part of the date.
I use the date format (varchar field in database):
*dd.mm.yyyy hh:mm ($date = date("Y-m-d") . ' ' . date("H:i");)*
For example:
01.04.2016
10.04.2016
11.04.2016
01.04.2017
10.04.2017
11.04.2017
The Query to get the entries:
SELECT *
FROM order_date
WHERE date >= '10.04.2017' AND date <= '10.04.2017'
Now i expected to get only the to matching entries but
the result of the Query are all four table entries which start with day 10 or 11. Even two of them are in the year 2016.
As i saw in other posts the between XX and YY work if the date is set with yyyy-mm-dd or yyyymmdd but the problem is that i got a database with around 20k entries on which i need a datepicker so i would prefer to use the givin format of the date without rewriting all entries.
Is there a possibilty to get a datepicker between with dd.mm.yyyy format aswell?
Any help highly appriciated
Try something like this,
STR_TO_DATE('10.04.2017', '%d.%m.%Y')
SELECT *
FROM order_date
WHERE date >= STR_TO_DATE('10.04.2017', '%d.%m.%Y') AND date <= STR_TO_DATE('10.04.2017', '%d.%m.%Y')
You can typecast the column as DATE, and use the ORDER BY clause.
Use STR_TO_DATE function may help.
STR_TO_DATE('10.04.2017','%d.%m.%Y')
How about something around this idea ?
SELECT *
FROM Mytable WHERE date
BETWEEN CONVERT(datetime,'10.04.2017',104 )
AND CONVERT(datetime,'10.04.2017',104 )

MySQL date interval with specific date format

I have date column field it's data look like 2014-05. how i get previous 6th month interval. My mysql query below
SELECT name,grade from users where data_year BETWEEN DATE_FORMAT('2014-04', '%Y-%m') - INTERVAL 12 MONTH AND DATE_FORMAT('2014-04', '%Y-%m')
I got result when i put with days like 2014-04-31
The strings you used aren't valid dates as they don't contain the day. If you want to use a date literal in a SQL statement you should use the form DATE '2014-04-01', eg
SELECT name,grade
from users
where date_column BETWEEN DATE '2014-04-01' - INTERVAL 6 MONTH
AND DATE '2014-04-01'
UPDATE
From the comments it seems the question actually is - how to parse an incomplete date string. You can use STR_TO_DATE for that, but you'll have to append -01 at the end, otherwise you'll get an invalid date with 0 for the day of month, ie 20140-04-00 instead of 2014-04-01.

Convert month Fullname to month number

Is There any built-in function in MySQL for getting Month Number for given Month Full
Name like 'January','March',etc.
I tried with MONTH() function to get month number from the date like this:
SELECT MONTH(STR_TO_DATE('Apr','%b'))
but it is not working for Fullname like April
Try with DATE_FORMAT option
SELECT MONTH(STR_TO_DATE('April','%M')) as Month;
For matching full month names, you'll have to use the %M specifier in DATE_FORMAT:
%M Month name (January..December)
Therefore, the following would give this:
SELECT MONTH(STR_TO_DATE('April','%M')) /* result is 4

select a date lesser than a particular date

I have stored the dates as string in my database.I know it is not good,but project already has been half developed before i take over and where dates were stored as string,so i was continuing the same way.
Now i want to select dates from table where date is greater than a specific date.
I tried the following query
SELECT
*
FROM
dates
where
STR_TO_DATE(date, '%Y-%m-%d') > "2014-01-01"
but it is not returning only greater values.
Please help me to solve problem.
Demo
Your dates are not in YYYY-MM-DD format. Use the right format!
SELECT *
FROM dates
where STR_TO_DATE(date, '%m-%d-%Y') > date('2014-01-01')
If you are going to store dates as strings, then the best way is in the ISO format of YYYY-MM-DD.
You should read the documentation on str_to_date() (here).
Convert everything to date and it should be fine. Now you are comparing date and string.
What type has the date? I'd prefer a ' instead of " for strings in SQL. Let's assume that date is a VARCHAR or TEXT field (depending on which database you are using):
SELECT *
FROM dates
WHERE STR_TO_DATE(date, '%Y-%m-%d') > STR_TO_DATE('2014-01-01', '%Y-%m-%d')
If date is a real DATE
SELECT *
FROM dates
WHERE trim(date) > STR_TO_DATE('2014-01-01', '%Y-%m-%d')
Or you just convert it into a number format date_format(date,'%Y%m%d') > 20140101