i use mysql version 5.7
i have a field devicetime which is a datetime field.
for some reason i want to add a generated column which stores only the date part of the devicetime field.
i have tried the following statement
alter table mytable
add COLUMN recorddate date generated always as date(devicetime) stored;
i get an error
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'date(devicetime) stored' at line 2
i have taken the inputs from MySQL documentation from this link.
my current table structure is like this
For whatever reason MySQL needs some ()'s around the expression:
ALTER TABLE `mytable `
ADD COLUMN `recorddate` DATE GENERATED ALWAYS AS (date(devicetime)) STORED;
One solution came into my mind is,you can store TIMESTAMP into table.
And when try to get anywhere in your SYSTEM you can use DATE_FORMAT() and STR_TO_DATE() function to whatever part you required from that actual data.
Related
CREATE TABLE Meta_data(Name_list_id VARCHAR DEFAULT (CONCAT('NL', AUTO_INCREMENT)),Result_id VARCHAR DEFAULT (CONCAT('RS', AUTO_INCREMENT)),No_of_subject int);
enter image description here
in above Name_list_id generate NL01,NL02,NL03....,
and Result_id generate RS01,RS02,RS03...,
can help to correct solution provide this query
for "mysql database"
It will be better if your ID values are numbers, not strings. Your syntax is incorrect - you are not allowed to create AUTO_INCREMENT on strings. Also, you are not able to use AUTO_INCREMENT column in GENERATED columns.
And now you are looking for the following:
NL01,NL02,NL03, ...
but what if we have 99+ records?
If you are using numbers, you will be able to format them when the data is read. For example, using LPAD function:
SELECT CONCAT('NL',LPAD(result_id,5,'0'))
FROM mytable
Apologies I'm brand new to SQL. I'm trying to add a column to SQL that calculates the number of days difference between as shipping date and todays date.
The following works perfectly when I want to view the days
SELECT DATEDIFF(now(),shipping_date) from tracking as days_transit
But when I try to make a new column with the following code I get errors
alter table tracking add days_transit as DATEDIFF(now(),shipping_date)
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'as cast(DATEDIFF(now(),shipping_date))' at line 1
What am I doing wrong?! I am using phpmyadmin
Just alter table to add the new column first with no values. Then next update the column values accordingly with update command.
I've got table products, and want to add column with type time.
I've got statement as follows:
ALTER TABLE products ADD openTime1 TIME DEFAULT TIME(now());
it works on MariaDB, but it doesnt work on Mysql.
On mysql it produces Error -
[42000][1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(now())' at line 1
Can someone tell me why? whats wrong here? I thought that this should be the same.
In MySQL the expression used in DEFAULT field attribute must be wrapped into the parenthesis:
ALTER TABLE products ADD openTime1 TIME DEFAULT (TIME(now()));
db<>fiddle here
I have a column dateTime which consists of dates of the format "MM-DD-YYYY, hh-mm-ss" and I need to create a STORED column on the same table to get rid of the time element. I've tried:
ALTER TABLE table ADD COLUMN startOfDay AS(date(dateTime)) STORED;
but this gives a wrong syntax error. How do I make it work? I think the error is due to the AS part.
First when asking a question and you tell that you have a error, always show the error message in your post.
Secondly to use STORED columns you need MySQL 5.7 instance or higher.
At the moment I only have a 5.6 instance running so I can't test the query. But looking at the MySQL documentation I would suggest the following query syntax:
ALTER TABLE <table-name> ADD COLUMN <column-name> DATE GENERATED ALWAYS AS (DATE_FORMAT(<name-of-datetime-column>, `%Y-%m-%d`)) STORED COMMENT '<description>';
Just replace the placeholders with the names you have. To be sure and learn how things work, always check the MySQL reference manual on the subject.
See: https://dev.mysql.com/doc/refman/5.7/en/create-table-generated-columns.html
I´m practicing Data Types (Dates) and MySQL date functions. So, just for practice, I´m trying to create a table and use both (the previous ones) for “x” column. I also want to do it with other date functions in case if it´s possible to do it.
So,
CREATE TABLE testing (
idtesting INT NOT NULL AUTO_INCREMENT KEY,
testingdate date NOT NULL DEFAULT CURDATE()
) ENGINE MyISAM;
If I´m not wrong date data type expects something like: YYYY-MM-DD and curdate() returns YYYY-MM-DD
But, I´m receiving this ERROR
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CURDATE()
) ENGINE MyISAM' at line 3
Even when I searched ERROR 1064 (42000), I can´t find a solution.
Anyone can help me?
And yes, I want to mix data types and dates functions just to learn.
Thanks a lot!
MySQL is very particular about setting default datetime/timestamp columns. The current date is not permitted -- either a timestamp or datetime (depending on the version of MySQL).
You can read about the defaults in the documentation.
(Note: initializing datetime columns is only allowed since 5.6.5. In earlier versions, only timestamp columns could be initialized.)