SQL Patch that will change all INT values to JSON - mysql

I have a table called photoProcess. There is a column within this table photoProcess.storyId.
Currently, all values within the storyId column are being stored as a single integer. I am changing the column type to VARCHAR so that I can store the values as JSON. My question is how I can write a MySQL patch that will change all storyId values of these values to a JSON string.
Example:
Original photoProcess.storyId value for the first row is 6649.
I'd like to change that value to ["6649"] and do so on a large scale for all others.

Use CONCAT:
UPDATE photoProcess SET storyId = CONCAT('["', storyId , '"]')

Related

Insert empty value into mysql date field?

I need to insert an empty value into a date field.
I've read how to insert an empty value in mysql date type field? which states to set the field to allow null. I've done that.
It also states to isnert null rather than an empty value. Unfortunatly that's not possible with the current set up - is there a way to allow it to input an empty string?
Say, your table is:
CREATE TABLE MyTable (A INT ,
B INT ,
C INT
) ;
The statement:
INSERT INTO MyTable (B,C) VALUES (3,4) ;
will leave A null.

Converting String Data Value into date

Good Morning All;
I currently have a MySQL table where there are 3 date fields (Columns) that were loaded as strings in this format 20140101 YYYYmmdd. I would like to convert this to a date format 2014/01/01 YYYY/mm/dd. Can someone please provide a simple sql syntax that would alter the table to a date format from a string and change the column to display the dates like this 2014/01/01 and not like 20140101. Thanks to all
Try this:
date_format(str_to_date(datecolumn, '%Y%m%d'),'%Y/%m/%d')
If you just want to reformat the values in the VARCHAR column, assuming that the column with sufficient length e.g. VARCHAR(10), and all the values are eight characters in length...
You could do something like this:
UPDATE mytable t
SET t.mycol = CONCAT( LEFT( t.mycol ,4)
, '/'
, SUBSTR( t.mycol ,5,2)
,'/'
, SUBSTR( t.mycol ,7,2)
)
WHERE CHAR_LENGTH(t.mycol) = 8
We want something in the statement that will prevent the statement from "working" a second time, if it's inadvertently re-run. It doesn't have to be CHAR_LENGTH. We might want to include a check that the value doesn't already contain a slash character AND t.mycol NOT LIKE '%/%'.
But why on earth are "date" values being stored in character columns, rather than in DATE datatype, which is custom designed for storing and working with date values?
ALTER TABLE mytable MODIFY mycol DATE ... ;
(If the column is defined as NOT NULL, has a default value, has a comment, those attributes can be retained, they need to be included in the new column specification, e.g.
ALTER TABLE mytable MODIFY mycol DATE NOT NULL COMMENT 'creation date';
Note that DATE columns do not have a "format" per se. When converting to string, MySQL uses date format '%Y-%m-%d'. And MySQL expects string literals representing date values to be in that same format. To get a value from a DATE column converted to string in format 'yyyy/mm/dd'.
SELECT DATE_FORMAT(date_col,'%Y/%m/%d') AS date_col
To get a string value in that format converted to DATE datatype
SELECT STR_TO_DATE('2015/06/01','%Y/%m/%d')

MySql SET datatye

i try to make column in my database table , with set data type to store in it data in this fomat " 10,2,44" i made SET column like this SET('A' , 'B' , 'C')
but when i try to insert data in it i got this response
Error Code: 1265. Data truncated for column 'setcol' at row 1
and this is my query
INSERT INTO `voting`.`questionnaires` (`name`, `cat_id`, `init_date`, `end_date`, `setcol`) VALUES ('sad', '2', '2008-02-02', '2008-02-02', 'A, B');
should i use specific format ? and does set accept repetition ?
To set your desired value you would store a 3 (without quotes) instead of 'A,B'. That is because values in a SET field are stored numerically in bitwise fashion. So each value in a set definition corresponds to on/off bits in a binary number.
Therefore you statement would be:
INSERT INTO `voting`.`questionnaires` (`name`, `cat_id`, `init_date`, `end_date`, `setcol`) VALUES ('sad', '2', '2008-02-02', '2008-02-02', 3);
For example, from the MySQL Documentation:
For a column specified as SET('a','b','c','d'), the members have the
following decimal and binary values.
So since A has a value of 1 and B has a value of 2, add them together and you get 3. Or to set C and D you would store 12, etc.
For this reason, it's ill-advised to have numbers as members of a SET field (as you hinted you might) since storing '5' has a completely different meaning than storing 5.

MySql insert statement to binary datatype?

I am using MySQL database.
I have one table having column with datatype binary(16).
I need help with the insert statement for this table.
Example:
CREATE TABLE `assignedresource` (
`distid` binary(16) NOT NULL
)
insert into assignedresource values ('9fad5e9e-efdf-b449');
Error : Lookup Error - MySQL Database Error: Data too long for column 'distid' at row 1
How to resolve this issue?
You should remove the hyphens to make the value match the length of the field...
Example:
CREATE TABLE `assignedresource` (
`distid` binary(16) NOT NULL
)
insert into assignedresource values ('9fad5e9eefdfb449');
Also, MySQL standard is to use this notation to denote the string as binary... X'9fad5e9eefdfb449', i.e.
insert into assignedresource values (X'9fad5e9eefdfb449');
Well, assuming that you want to strictly insert a hexadecimal string, first you need to remove the dashes and then "unhex" your string before inserting it into a binary(16) data type column, the code would go like this:
INSERT INTO `assignedresource` VALUES(UNHEX(REPLACE('9fad5e9e-efdf-b449','-','')));
Also... the "usable" data you are inserting is actually 8 bytes after undashing it, so binary(8) would do fine if you plan on not storing the dashes.
You can strip the hyphens and perpend 0x to the value unquoted, like this:
insert into assignedresource values (0x9fad5e9eefdfb449);
As well as, as this (mentioned in other answers):
insert into assignedresource values (X'9fad5e9eefdfb449');
Both are valid notation for a hexadecimal literal.
Your string is 18 char long, change the database
CREATE TABLE `assignedresource` (
`distid` binary(18) NOT NULL
)

How to select value number of ENUM types in MySql?

I need to select a row from table below, but the problem is the value in $row['city'] is the textual represent of the value, and i need its number(Toronto = 2). (Same as when we INSERT INTO, and we use value number instead of text)
Requests Table Structure:
req_id INT
uname VARCHAR(30)
city ENUM('New York', 'Toronto', 'Las Vegas')
You just need to force your city into a numeric context, from the fine manual:
If you retrieve an ENUM value in a numeric context, the column value's index is returned. For example, you can retrieve numeric values from an ENUM column like this:
mysql> SELECT enum_col+0 FROM tbl_name;
So you want this sort of thing:
select req_id, city+0
from your_table
where city = 'Toronto'
BTW, you can insert an enum using either the string or integer representation.
You can use the CAST function. The documentation doesn't mention this specific use case, but it works as expected. I prefer it because it looks elegant and clear:
SELECT CAST(city AS UNSIGNED) FROM your_table;