STR_TO_TIME convert - mysql

have a string field in DB which I need to convert to datetime, my problem is I need to convert date format from 03/01/2019 11:30 to 2011-01-26 14:30:00. I can do it with an individual record, but can't figure out how to convert all the records in the table.
I followed the advice here
but, all converted dates are 0000-00-00 00:00:00 presumably because mysql not able to guess input date format.
create table wms_lw.wms_lw (
site_id int null,
name varchar(255) null,
site varchar(255) null,
date varchar(255) null,
new_date datetime not null,
value double null,
date_2 varchar(255) null
);

You can convert your string in date as
select str_to_date('03/01/2019 11:30', '%d/%m/%Y %i:%s')
so from your table
select str_to_date(my_col, '%d/%m/%Y %i:%s') from my_table
once you have a date you can format as you prefer wit date_format() or add the datte time using date_Add()

Related

When i Create table with Month he only given 0000-00-00

I create table for hour
CREATE TABLE hour (
Name1 varchar(25) not null,
Datee Datetime not null DEFAULT (CURRENT_TIMESTAMP()),
Monthh date not null DEFAULT (MONTH(CURRENT_DATE()))
);
Mysql only give me 0000-00-00 not Name where i use button in php. What is wrong with this and how correct? In my opinion my phpadmin dont have a MONTH function
CREATE TABLE hour ( Name1 varchar(25) not null,
Datee DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
Monthh TINYINT NOT NULL DEFAULT (MONTH(CURRENT_TIMESTAMP))
);
fiddle
PS. HOUR is Reserved word - I'd recommend to rename a table.
Store full timestamp and it's month part in separate columns in same table is absolutely redundant. You can story only datetime and get it's month in select query:
CREATE TABLE Godzina (
Nazwa varchar(25) not null,
Randka Datetime not null DEFAULT CURRENT_TIMESTAMP()
);
INSERT INTO Godzina (Nazwa) VALUES ('TestName');
SELECT
Nazwa,
Randka,
MONTH(Randka) AS Miesiac
FROM Godzina;
Test it on SQLize.online

How to query the mysql database by changing the date formats

This is my table structure
CREATE TABLE `stock_historic` (
`name` varchar(30) DEFAULT NULL,
`date` varchar(30) DEFAULT NULL,
`open` decimal(8,2) DEFAULT NULL
) ;
Parsing from an csv file , i have inserted data into the table this way
Insert Into stock_historic values('SRF', '20150930' , '11190.00');
Insert Into stock_historic values('SRF', '20151001' , '11456.00');
But from the front end the (Jquery date picker) date comes in this format
select * from stock_historic where symbol = "SRF.NS" and startDate = "2015-09-30" and endDate = "2015-10-01"
http://sqlfiddle.com/#!9/91ac8d/1
Please let me know how can i query the database in this case
Format the date of jQuery date picker, remove the dashes. For example:
$( "#datepicker" ).datepicker({
"dateFormat": "yymmdd"
});
I think it will be better formatting in javascript than in query, you can put it directly in your queries.
alter your table structure and give the date column another name (date is a reserved word) and an appropriate column type like timestamp or date
CREATE TABLE `stock_historic` (
`name` varchar(30) DEFAULT NULL,
`opendte` timestamp DEFAULT NULL,
`open` decimal(8,2) DEFAULT NULL
) ;
now you don't have to bother about formats as long as its not ambigous. All of your examples should work.

Convert Varchar containing date to DATETIME

I have a database with 6k+ rows and don't want to have to manually convert each date to DATETIME, they are currently in varchar.
They are in the UK format, DD/MM/YYYY.
Currently the date is in a column named datetime which is varchar(12)
I want to convert it to a datetime column named date_new.
How can I do this using an SQL statement
Solved using
UPDATE table SET date_test = STR_TO_DATE( DATETIME, '%d/%m/%Y' )

MySQL - Date Column Format

I am familiar with DATE_FORMAT function which can display the the record from my date field in the format specified. However, I would like to create a table with a date field that only accepts my format.
Here's what I have done so far:
CREATE TABLE test_table (
id INT AUTO_INCREMENT,
f_name VARCHAR(40) NOT NULL,
l_name VARCHAR(25) NOT NULL,
date_hired DATE NOT NULL
);
Inserting a record with a date_hired value of '2013-03-01' will be inserted as '1/03/2013 12:00:00 AM' which is far from my expected result (I would like the format the way it was inserted). Any feedback? Did I miss something?
Thanks,
Michael
You can't change the format during table create, you can change the format of date for displaying user by using you programming logic like if you are using PHP as your server side language the you can convert it your desired format.

Convert from SQLite to MySQL

This table I created in a SQLite database:
CREATE TABLE [tickets] (
[id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
[coupon_id] INTEGER NULL,
[size] FLOAT NULL,
[phone] VARCHAR(10) NULL,
[date] DATE DEFAULT CURRENT_DATE NULL,
[time] TIME DEFAULT CURRENT_TIME NULL,
[product] TEXT NULL
);
Now INSERT operation is:
INSERT INTO "tickets" VALUES(429,9,18.16,'949-893-5032','2010-11-30','17:46:39','Kids’ Kups Berry Interesting™');
INSERT INTO "tickets" VALUES(430,9,12.04,'847-188-1359','2010-11-25','10:54:00','Raspberry Collider™');
INSERT INTO "tickets" VALUES(431,9,14.1,'204-682-5560','2010-12-08','15:34:07','Celestial Cherry High™');
Now the same table I created in MySQL:
CREATE TABLE tickets (
id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
coupon_id INTEGER NULL,
size FLOAT NULL,
phone VARCHAR(10) NULL,
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,
time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,
product TEXT NULL
);
INSERT operation for MySQL is:
INSERT INTO tickets VALUES(429,9,18.16,'949-893-5032','2010-11-30','17:46:39','Kids’ Kups Berry Interesting™');
INSERT INTO tickets VALUES(430,9,12.04,'847-188-1359','2010-11-25','10:54:00','Raspberry Collider™');
INSERT INTO tickets VALUES(431,9,14.1,'204-682-5560','2010-12-08','15:34:07','Celestial Cherry High™');
When i am inserting those values I got an error :-there can be only one TIMESTAMP column with current_timestamp in default of on update clause
…but I am not able to insert all those values into MySQL. Help me?
In SQLite you have two columns
[date] DATE DEFAULT CURRENT_DATE NULL,
[time] TIME DEFAULT CURRENT_TIME NULL,
while on MySQL you have only one
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,
and you're trying to insert two values on it...
You should try
INSERT INTO tickets VALUES(..., '2010-11-30 17:46:39', ...)
At first glace, your varchar column is size 10, but you are inserting greater than length 10 data into it. Make sure your varchar column is wide enough for your data.
Your MySQL Schema appears to be incorrect for what you're trying to insert.
Excerpt from this post: Should I use field 'datetime' or 'timestamp'?
...Timestamps in MySQL generally used to track changes to records, and are updated every time the record is changed. If you want to store a specific value you should use a datetime field.
Change your MySQL schema to something closer to:
...
phone VARCHAR(12) NULL,
date DATE DEFAULT CURRENT_DATE NULL,
time TIME DEFAULT CURRENT_TIME NULL,
...