I want to get the Day of the week as a string and not an integer value from a datetime column from the MySQL Table. So instead of getting 0,1,2.. I would like to have these as Sunday, Monday, tuesday and so on. Is there a date function to achieve this?
Seems you are looking for DAYNAME
SELECT DAYNAME("2008-05-15") from dual
Use DAYNAME
Query
SELECT DAYNAME(date_column) AS `DayName`
FROM your_table_name;
Related
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
I have a date column :
Report_Received
8/7/2019 11:39
6/18/2019 10:25
I want to check if the date falls before 10th of that month of that year.
If yes, need to say True else False under a new column(Complaince_Reporting).
Is there any way to do this?
You can use the DAY function to get the day of the month, but first, because you are not using a MySQL Datetime column or a valid MySQL date format, you need to convert your date using STR_TO_DATE. Something like this:
SELECT ...,
DAY(STR_TO_DATE(Report_received, '%c/%e/%Y')) < 10 AS Compliance_Reporting
FROM yourtable
Demo on dbfiddle
I have a table with year data in a year datatype field, now I want to insert new data with date data and I want to convert the year from the old data to date with default month and day 1.1.YearFromOldData.
I'm looking for something like the function STR_TO_DATE but for the datatype year NOT VARCHAR NOT VARCHAR and I fail to find it. How would I do this?
I want to do this
SELECT YEAR_TO_DATE(myYearField, '%1/%1/%Y')
FROM myTable
Assuming that you have a varchar field named year_dt with old years, use the following query to get a date with default day and month
SELECT DATE(CONCAT(table.year_dt, '-01-01')) as 'date' FROM table
this will return date in default format i.e. YYYY-MM-DD
If you want the first day of the year, I think the easiest way is with makedate():
select makedate(year, 1)
You can get the timestamp to insert to the database using
timestamp = (year - 1970) / 31557600
STR_TO_DATE() is exactly what you need. Try this if you have four-digit years, in a string, an integer, or a YEAR column. It works for them all.
select str_to_date(CONCAT(year_column,'-01-01'), '%Y-%m-%d')
If you have two-digit years try this (lower case %y)
select str_to_date(CONCAT(year_column,'-01-01'), '%y-%m-%d')
This is cool because you can do all sorts of date arithmetic: for example
select str_to_date(CONCAT(year_column,'-01-01'), '%Y-%m-%d')
+ INTERVAL 1 QUARTER
- INTERVAL 1 DAY
will give you the last day of the first quarter of your year.
I have a field in my table called created_date. The date format is 2010-02-28. I just wondering is it possible to do a mysql statement, only return the day instead of the entire date. eg. 28
SELECT
day(created_date)
FROM
table
This above query throw me error, is there a way i can do similar stuff?
cheers
Use MySQL built-in function called DAYOFMONTH
mysql> SELECT DAYOFMONTH('2007-02-03');
-> 3
DAYOFMONTH()
From Docs,
Returns the day of the month for date, in the range 1 to 31, or 0 for
dates such as '0000-00-00' or '2008-00-00' that have a zero day part.
MySql EXTRACT function extracts day, month or year from a given date.
select extract(day from created_date) as created_day from table
you can fetch the whole date in your format and display only the required field that is date by using this
date("j", strtotime(date('Y-m-d')) );
[MySQL/PHP] My table has a date column of datetime format. All records are of the YYYY-MM-DD HH:MM:SS variety.
MySQL queries like SELECT record FROM table WHERE date > '1941' AND date < '1945' work nicely.
MySQL queries like SELECT record FROM table WHERE date > '1941-03-01' AND date < '1945-01-30' also work nicely.
But what about if I wanted all records that were filed in March, regardless of year? Or all records filed on the 17th, regardless of month/year?
``SELECT record FROM table WHERE date = '03'` clearly doesn't work.
I know I could snag it with a LIKE '%-03-%' parameter, but that doesn't leave room for me to search for range, like all records from March to May.
Help? :-)
Try WHERE MONTH(DATE(`date`)) BETWEEN '03' AND '05'
The DATE() part is to extract the date from the timestamp to be used with MONTH().
You can use MySQL date functions:
SELECT record FROM table WHERE MONTH(date) = 3
SELECT record FROM table WHERE DAY(date) = 17
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
If you look at http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html, you will find many useful functions for your purpose.
... WHERE MONTH(date) = 3, e.g. =)