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.
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 need to only SELECT data where the date field is yesterdays date. The only problem I'm having is that the data in the date field looks like the following 20160412 062815.000
I don't really care about the time, I just want to search dynamically for anything with yesterdays date. I've tried a multitude of CURDATE() -1 but I'm unsure how to just search the first 8 digits of the field.
Assuming the date value is stored as a string, and the first 8 characters are always the date in YYYYMMDD format, then you can use a query like this:
select *
from your_table
where your_column like concat(date_format(current_date() - interval 1 day,'%Y%m%d'),'%')
One advantage of this query is that it can leverage an index on your date field, unlike the other answers so far.
Format yesterday's date to a number and convert the date string also to a number.
select * from your_table
where date_format(curdate() - interval 1 day, '%Y%m%d') * 1 = date_col * 1
SQLFiddle demo
*1 is a math operation that forces MySQL to convert strings to a number.
you can use subdate(currentDate, 1)
select * from your_table
where subdate(currentDate, 1) = DATE(your_date)
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.
I am trying to select records from a database only if they match today's date. The format for the date in the database is 2012-06-20 9:30:00 I am using the statement SELECT id FROMnewsreportsWHERE DATE(newsdate) = CURDATE() but it doesn't not return any records for today?
Screenshot of column with dates
http://img135.imageshack.us/img135/8399/2347f03df0394cd898c7fc5.png
DATE_FORMAT(NOW(),'%Y-%m-%d') = FORMAT_DATE(NOW(newsdate), '%Y-%m-%d')
Or better:
DATE(newsdate) = DATE(NOW())
The best way is to store the additional column with 2001-09-11 date format and compare this one
Example, thanks to #Conrad Frix
It looks like the curdate function is going to give you something different than the date format you've got in the database.
Take a look at the docs here
MySQL Date and Time Functions
It's supposed to return a date in this format:
2008-11-11
So, you could either search for a date range between curdate() and curdate() + INTERVAL 1 DAY (untested), or store the dates in the curdate() format.