Hi I'm trying to create a new table and want to create a column with the following syntax:
CREATE TABLE tablename(
dinnerTime TIME(0) NOT NULL
);
I'm being told that I have an error in the SQL syntax, but shouldn't this just give me time in the format HH:MM:SS since I'm putting 0 as the precision?
Related
I code in the SQL, but I want to change the date format from default of My SQL to different format "DD/MM/YYYY" to use this format to code, but I recieved an error.
More about version of SQL:
SQL: My SQL Workbench 8.0.30 build 2054668
Window 11 Pro
Language: English
So, how can I change the date format or what is the true type of "dmy"? Please help me.
This is code that I used:
set dateformat dmy
And error is:
"Error Code: 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 'dmy' at line 1"
At the moment, we don't really know the reason why you want to change the date format, but I have a couple of assumption:
You want to insert date data from front end into the table but couldn't do so due to the mismatched date format.
You want the end output to show the date format of DD/MM/YYYY instead.
Although, it may look like you need to change the table column date format, there's a way to avoid that operation entirely. However, since you've mentioned changing from YYYY/MM/DD in your title, I'm not sure if your date column is actually DATE datatype because the default MySQL should be YYYY-MM-DD. Nonetheless, I'll address the matter in this answer altogether.
Modify the date format in query and leave the table date column datatype as it is:
From DD\MM\YYYY to MySQL DATE datatype format YYYY-MM-DD
... STR_TO_DATE(data, '%d/%m/%Y')
.. from YYYY-MM-DD to DD/MM/YYYY
... DATE_FORMAT(data, '%d/%m/%Y')
You can use any of that anywhere in a query; whether in SELECT or WHERE.
If "in query" is not what you want and you still want to update the table:
Well, you have two options here:
Directly modify the column datatype then update the value:
ALTER TABLE mytable MODIFY COLUMN date_col VARCHAR(255);
UPDATE mytable SET date_col =DATE_FORMAT(date_col , '%d/%m/%Y');
Or you can add another column, populate the desired date format there and keep the original date column as it is:
ALTER TABLE mytable ADD COLUMN my_date VARCHAR(255);
UPDATE mytable SET my_date =DATE_FORMAT(date_col , '%d/%m/%Y');
this way you have the option to directly use MySQL date functions on the default MySQL date column without the hassle of converting your desired date format into the default before you can use date functions. What I'm saying is something like this:
DAY(mysql_default_dateformat)
is similar to
DAY(STR_TO_DATE(your_dateformat, '%d/%m/%Y'))
which means that you can use DAY() (date function) on the default date format directly without the need to convert what is not default first.
Here's demo fiddle examples
We are storing datetime in a column on our MySQL database, formatted in TEXT, but when our datetime is supposed to look like below:
'xxxx-xx-xx 00:00:00'
The time is deleted or not show on our datetime, and therefore our datetime, at that specific time, only contains the date:
'xxxx-xx-xx'
What we want is first of all to figure out why this is occurring, but for now we need to edit every row, and make sure the datetime is also showing the time. We have tried to change the impacted rows by using this query:
UPDATE table SET TIME(col_datetime) = '00:00:00' WHERE LENGTH(TIME(col_datetime)) = 0;
Above query should update the time on the datetime for col_datetime, where length of time is 0. Unfortunately, we receive an error, and we can't run the query. This is the error:
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 '(time_start) = '00:00:00' WHERE LENGTH(TIME(time_start)) = 0' at line 2
How can we change time on our datetime, where time is not shown?
Don't store dates as strings. Instead, you want to use the datetime datatype: it has a time part, that defaults to 00:00:00 when not specified.
Here is a small conversion script for that purpose:
alter table mytable add col_datetime_new datetime;
update mytable set col_datetime_new = col_datetime;
alter table mytable drop col_datetime;
alter table mytable change column col_datetime_new col_datetime datetime;
This leverages the fact that you are using format YYYY-MM-DD in your string dates, so conversion to datetime is seemless.
I will be getting datetime from JsonFile in "2019-05-03T06:45:06.000+0000" this format. I want to put this into DB.
I tried using datetime datatype in MySQL table. Its not working.
create table employee(
empId varchar(15),
requestorId varchar(15),
profile int ,
createdTime datetime,
reqId int
);
insert into employee values("x","y",1,2019-05-03T06:45:06.000+0000,2);
Error executing INSERT statement. 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 ':45:06.000+0000,1)' at line 1 - Connection: Connection 1: 87ms
Use STR_TO_DATE, which can convert an input date string into a bona fide MySQL datetime value:
SELECT STR_TO_DATE('2019-05-03T06:45:06.000+0000', '%Y-%m-%dT%H:%i:%s.%f+0000');
The format mask used here is %Y-%m-%dT%H:%i:%s.%f+0000. Of note, the %f term matches fractional seconds. In the case of your timestamp, there are three places of fractional precision.
I'm trying to convert all mm/dd/yyyy to a SQL readable date in a table/column.
I've tried:
ALTER TABLE `raw` CHANGE `Most Recent PC ENC` `Most Recent PC ENC` DATE NULL DEFAULT NULL;
but it kicks back an error most likely due the mm/dd/yyyy format.
I don't think MySQL provide you the ability to migrate data from varchar to date automatically. Because we can describe a time with string in a variety of formats.
If I were you, I will add a new column with date type, and then migrate the data existed in the varchar column to the new column manually. As you can use Date and Time Functions here, so you can achieve this easily.
ALTER TABLE raw ADD COLUMN 'new_column' DATE NOT NULL DEFAULT '0000-00-00 00:00:00';
UPDATE raw SET new_column = STR_TO_DATE(old_column, '%m/%d/%Y');
ALTER TABLE raw DROP COLUMN old_column;
The DEFAULT constraint has no problem in accepting string or current date values. What i need is an constraint that will create an random 4 digit number every time an entity is created. I tried the following code but it returns an syntax error.
ALTER TABLE client_number ADD(code INT(4) DEFAULT FLOOR(RAND()*9999)+1111);
The above statement returns following error:
#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 'floor(rand()*9999)+1111)' at line 1
Need solution.
The documentation is quite clear:
The DEFAULT value clause in a data type specification indicates a
default value for a column. With one exception, the default value must
be a constant; it cannot be a function or an expression.
The expression you have is not a constant, so it doesn't work. You would need to use a trigger instead.
Run the below query
ALTER TABLE table_name ADD field_name INT(4) NOT NULL DEFAULT (rand() * (9999 - 1000) + 1000) AFTER some_feild;
The result gives a random number between 1000-9999