Date difference between two dates with format 01/06/18 - mysql

Hi ALL I am trying to get date difference between two dates. I am getting data from cube and saving in temp table. I tried to defined date field as a varchar and also datetime and date
My date is in the format 03/04/18
And I am trying DATEDIFF(DAY, PAAR.DateReceived, O.DateReceived) for getting number of days difference. But I am getting error as below.
When I defined date field in temp table as DateTime or Date
"Operand type clash: ntext is incompatible with datetime"
When I defined date field in temp table as Varchar then
"Conversion failed when converting date and/or time from character string."
Thanks In Advance

I suggest you use str_to_date() with the appropriate format modifiers to convert those strings into dates.
DATEDIFF(str_to_date(PAAR.DateReceived,'%d/%m/%y'), str_to_date(O.DateReceived,'%d/%m/%y'))
see: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date

Try this
set #sdate = '01/01/18';
set #edate = '01/26/18';
set #sdate = CONCAT_WS('-',SUBSTR(#sdate FROM 7 FOR 2),SUBSTR(#sdate FROM 1 FOR 2),SUBSTR(#sdate FROM 4 FOR 2));
set #edate = CONCAT_WS('-',SUBSTR(#edate FROM 7 FOR 2),SUBSTR(#edate FROM 1 FOR 2),SUBSTR(#edate FROM 4 FOR 2));
SELECT DATEDIFF(date(#edate), date(#sdate));

Related

Mysql returns some records null in field of date as date have multiple format

I am trying to analyze order_Date column and column have multiple date format i want to convert all those date in same format which wull make be easier to analyze the order_date.
I am trying to analyze the order_date however this column have multiple date format 2019/07/15 and 1/13/2014
Howeever, while converting different format date with one format yyyy/mm/dd with query.
select date_format(order_date, '%y/%m/%d'),orderid from superstore;
it shows null values like this.
i have tried to use `CAST as well but it shows every single value as null.
select case when order_date like '%Y' then date_format(order_date, '%Y/%m/%d') else null end as newdate from superstore;
date_format funtion is used to format a date datatype you should use https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_str-to-date any null values returned by str_to_date either failed or started as null. You will need to examine these and adjust the str_to_date parameters appropriately. There is a catch though is 20/2/20 y/m/d or d/m/y (for example) and how can you differentiate month and day where both are <=12?
For example
drop table if exists t;
create table t
(dt varchar(10));
insert into t values
('1/1/2020'),('2020/1/12'),('12/12/12'),(null),('13-14-15');
select dt,
case when length(substring_index(dt,'/',-1)) = 4 then str_to_date(dt,'%d/%m/%Y')
when length(substring_index(dt,'/',1)) = 4 then str_to_date(dt,'%Y/%m/%d')
when length(substring_index(dt,'/',1)) = 2 then str_to_date(dt,'%y/%m/%d')
else str_to_date(dt,'%y/%m/%d')
end dateformatted
from t;
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=553219f33ad9e9a4404fc4c0cb6571c9
note in no case can I identify month and day and sometimes year..

SQL STR_TO_DATE

I'm having trouble with the following code:
INSERT into `fun` ( funner)
SELECT YEAR(STR_TO_DATE(SUBSTRING(time,1,4), '%Y'))
FROM `orig`
returning the warning:
Incorrect datetime value: '1880' for function str_to_date
time is a varchar column in the table orig with the format yyyy/mm.
I want to extract the year section from this varchar and translate it into a year datatype using STR_TO_DATE
I would recommend using one of the usual date and time MySQL datatypes, instead of the rarely used YEAR datatype : DATE, DATETIME and TIMESTAMP.
If you want to turn your string to a date datatype, then :
STR_TO_DATE(my_column, '%Y/%m')
You can use the YEAR() function on this date, and it will return an integer value :
YEAR(STR_TO_DATE(my_column, '%Y/%m'))
Finally : if all you want is get the year from a date stored as string, then you can directly extract it the string using SUBSTR :
SUBSTR(my_column, 1, 4)
This returns a string (not an integer), that MySQL will implictely convert to a number when used in numeric context.
You can convert SUBSTRING(time,1,4) to integer:
SELECT CONVERT(SUBSTRING(time,1,4),UNSIGNED INTEGER) FROM orig
there is no need to convert it first to a date and then convert to integer.
I guess you need to return an integer value since you use the YEAR() function.
If not a simple SELECT SUBSTRING(time,1,4) FROM orig will do.
What does time look like? If year has the data type year, then you can insert a date into the column:
INSERT into `clean` (year)
SELECT DATE(CONCAT(SUBSTRING(time, 1, 4), '-01-01'))
FROM `orig` ;
I am not a fan of the year data type. You should just put the entire date into the column.

How to remove 2 hours from all rows of a column string represent date and time

I have a text Column in mysql which the data of it represent date and time
the format is like this: 2018-03-06 03:18:17pm
Sometimes this colum instead of date show the word "NO"
I need to remove 2 hours from all of the rows
in the table.
would be happy for a code example.
the table name is: r_238
the col name is:Answer
Thanks for all who answer.
You are in luck. MySQL will recognize that format as a valid datetime, so you can just convert the column to a datetime. The rest is easy:
update r_238
set answer = nullif(answer, 'NO');
alter table r_238 modify column answer datetime;
update r_238
set answer = answer - interval 2 hour;
In the future, use proper types for your columns. datetime values should be stored as datetime, not as a string.

SQL Change date format from yyyy-mm-dd to dd-mm-yyyy

I have created MySQL table :
CREATE TABLE EMP(
EMPID INTEGER NOT NULL (5),
SURNAME VARCHAR(25),
SAL INTEGER(5),
JON VARCHAR(25),
START_DATE DATE,
END_DATE DATE,
DEPNO INTEGER(5)
);
with following records:
INSERT INTO EMP
(EMPID,SURNAME,SALARY,JOB,START_DATE,END_DATE,DEPNO)
VALUES
('1','Vorosila','500000','COO','20150101',null,'1');
however I need to change date format from 2015 01 01 to 01 01 2015
Can anybody show me or tell me how to do that ?
THANK YOU IN ADVANCE
DATE values do not have a "format", they are objects that represent instants in time (or entire days, but still independent of formatting).
Formats are applied on input and output, so you just need to apply the correct format, which you can find in the MySQL manual, to the SELECT statement.
You cannot change the default date format in mysql.
I once hoped for the default date to be editable so I wouldn't have to jump through these hoops to get the date I actually wanted, mysql even has a date format system variable, but it is unused. Date Format Mysql - link
What you should really do is store it as the default format Year-Month-Date and then convert it on select.
The first thing I'd suggest is having your date columns as date types, which would give your dates the following format '2015-01-01'.
If you do this then you can use DATE_FORMAT - link - the second value in the DATE_FORMAT function allows you to customise the returned date, and there are many different thing you can do with this if you look at the link:
SELECT
DATE_FORMAT(`START_DATE`,'%d-%m-%Y')
AS `START_DATE`
FROM ...
The other option you have is to store your dates in the format that you already want as a char or varchar column.
HOWEVER, as should be obvious, this column will not be treated as storing dates, and so will not give you the correct comparisons in a where clause when using > < BETWEEN or the correct ordering in an order by clause. It is after all just a string of numbers in this case.
However you can then use STR_TO_DATE - link if you did need to use a where or order by on this column to change it back to a date within the query - in this case the second value is the custom format of your 'dates' in the column. Keep in mind with a where you will need to compare it with the correct mysql format as shown below:
SELECT
`START_DATE`
FROM table
WHERE STR_TO_DATE(`START_DATE`,'%d-%m-%Y') BETWEEN '2015-01-01' and '2016-01-01'
In MySQL you can change the format of a date using DATE_FORMAT method which is similar to to_char in Oracle.
DATE_FORMAT(SYSDATE(), '%DD-%MM-%YYYY');
For more information about specifiers check this thread http://www.sqlines.com/oracle-to-mysql/to_char_datetime
You can do what you probably want by creating a view and referring to that instead of the (underlying) table.
CREATE VIEW emp_view AS
SELECT empid,
surname,
sal,
jon,
date_format(start_date, '%d-%m-%Y') as start_date,
date_format(end_date, '%d-%m-%Y') as end_date,
depno
FROM emp;
Note that this changes the type of the date columns to varchar, so comparisons will no longer work as expected:
SELECT * FROM emp_view WHERE start_date > '01-12-1924'; // fails!

How to add 1 year to date stored as varchar?

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')