I am trying to migrate data from one table to other. The issue is my target table has type date yyyy-mm-dd where as the source table has type varchar mm/dd/yy
I tried a few thing but seems none worked.
I am trying this but seems to give null
select year((datecreated)) * 10000 + month((datecreated)) * 100 + day((datecreated)) from employee
Here employee is my table and datecreated is my column.
If someone has come across this please let me know how to fix it.
You can try the STR_TO_DATE to convert a string to a date:
SELECT STR_TO_DATE(datecreated,'%m/%d/%Y') as date
FROM employee
Date format specifications (%m, etc) can be found here.
Related
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.
i want to do a count of two columns in mysql. One of the columns is a string but another is a date like 06/08/2017 and when i do my query i get 0 results.
SELECT count(*) FROM `castigos` WHERE inicio_normal=05/06/2017 AND cod_emplazamiento=1
I have entries of that data but its dont show me anything. Maybe the type of data in the date is wrong?
What should i do?
Add the date field to your select and group by it. Otherwise mysql extensions doesn't recognize you want to group by the date and will aggregrate all the results into 1 column. And since you are getting 0 count, you're where clause must not be working.
Your date format seems malformed. usually YYYY/MM/DD format (standard format);
or specify a format using SELECT STR_TO_DATE('17/09/2010','%d/%m/%Y');
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'.
the below uses the implicit casting and default date format to convert the string date to a valid date.
SELECT inicio_normal, count(*)
FROM `castigos`
WHERE inicio_normal='2017/05/06'
AND cod_emplazamiento=1
GROUP BY inicio_normal
Otherwise its doing math and comparing that date to the number stored for the date.
Understand dates should be stored in a date datatype and when you query dates you're passing in a string that is being cast to a date datatype for comparison. So you need to use the standard format, or cast your string to a date so the db engine knows how to convert your format to a date.
Try this :
SELECT count(*) FROM `castigos` WHERE inicio_normal="05/06/2017" AND cod_emplazamiento=1 GROUP BY inicio_normal
WHERE inicio_normal=05/06/2017
If you divide 3 by 6 then by 2017 you get a very small value indeed. OTOH if you reformat this as a date (e.g. 20170605, if you gave us a European formatted date - dd/mm/yyyy) then your query will find the rows you showed us.
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!
I have list of date in MySQL in the format of "MM-DD-YYYY" and When I was trying to fetch the latest date from table it just return the last date of a Year like 12-01-2014 instead of return latest date 03-16-2016.
Payment history table:
to_date
03-16-2016
12-01-2014
11-07-2014
10-03-2014
01-09-2014
I used following query:
SELECT MAX(to_date) FROM paymenthistory WHERE empid=59;
Result : 12-01-2014
Related post: Get the latest date from grouped MySQL data
Thanks in advance
You're working with strings, not native dates, so you're getting the maximum date.
Either convert those strings to ACTUAL mysql date/datetime values, or you'll have to go with ugly hacks, like
SELECT MAX(STR_TO_DATE(to_date, '%m-%d-%Y'))
and performance will be massively bad. MySQL's native date format is yyyy-mm-dd hh:mm:ss, which is a natural "most significant first" format. If your date strings were formatted like that, then even a max(string) would work.
It sounds like your date column is actually a VARCHAR format since it is seeing 12-01-2014 as the last date which is only true if stored as a VARCHAR.
Be sure your to_date column is a DATE type.
have you tried this?
SELECT TOP 1 * FROM paymenthistory WHERE empid = 29 ORDER BY to_date DESC;
For mysql try this
SELECT * FROM paymenthistory WHERE empid=59 ORDER BY to_date DESC LIMIT 1;
I'm having some troubles with converting varchar types to date types.
In SQL-server 2008 I'm having a table with different types of dates in type VARCHAR.
For example:
DDMMYYYY (05032013)<br/>
MMDDYYYY (03052013)<br/>
YYYYMMDD (20130305)<br/>
...
I have to convert these different string types to type "date" using a SQL-query.
Any suggestions how I can do this?
These are my records:
TYPE || FORMAT
____________________
DDMMYYYY||05032013
MMDDYYYY||03052013
YYYYMMDD||20130305
Try this
SELECT case
when type='DDMMYYYY' then convert(date,STUFF(STUFF('05032013',3,0,'-'),6,0,'-'),105)
when type='MMDDYYYY' then cast(substring('03052013',5,4)+'-'+substring('03052013',0,3)+'-'+substring('03052013',3,2) as date)
when type='YYYYMMDD' then cast(STUFF(STUFF('20130305',5,0,'-'),8,0,'-') as date)
end
If you can identify every row which date format is, you can convert them to date type. Otherwise i don't think that it will be completely correct. For example you can't implement logic for the following record - (03052013) can be 2013-05-03 or 2013-03-05.