I have tried:
SET #my_var = 'one,two,three';
SET #to_replace = 'two,';
SET #my_check = 1;
SET #my_var = SELECT IF(
ISNULL(#my_check),
#my_var,
REPLACE(#my_var, #to_replace,'')
);
I have tried also SET #my_var = IF( ... Or SELECT REPLACE instead REPLACE in condition
But got syntax error
What I expect is:
If #my_check = null so #my_var = 'one,two,three' (still unchanged)
Else replace two, with '' but using #to_replace variable
Expected result: #my_var = 'one,three'
set #my_var = if(isnull(#my_check), #my_var, replace(#my_var, #to_replace, ''));
Works well in MySQL v8.
Related
How add to json varible a new element in mySQL
From this:
Value = ['123']
Make this:
Value = ['123','456']
JSON_ARRAY_APPEND() function
SET #Value = '["123"]';
SET #Value = JSON_ARRAY_APPEND(#Value, '$', '456');
SELECT #Value;
#Value
["123", "456"]
fiddle
I have wrote a stored proc in sqlyog. It is quite long and performing all the desired functionality except for the concat statement so I will only list that particular query.
UPDATE recipes_new
SET co_auths = CONCAT(co_auths,',',c1id)
WHERE id = name_in;
I basically want a separation in the two fields and this statement is placed in a cursor so it is iterative. co_auths is null at the moment so I get the result as ,1,2,3 where as I want it to be 1,2,3. Any guesses what can the most appropriate solution be?
By using an IF:
UPDATE recipes_new
SET co_auths = IF(co_auths IS NULL, c1id, CONCAT(co_auths, ',', c1id))
WHERE id = name_in;
If the value of co_auths is an empty string instead of NULL:
UPDATE recipes_new
SET co_auths = IF(LENGTH(co_auths), CONCAT(co_auths, ',', c1id), c1id)
WHERE id = name_in;
MySQL returning an empty field: CONCAT(nonEmpty1,empty2,nonEmpty3) = NULL
CONCAT_WS is what you are looking for
UPDATE recipes_new SET co_auths = CONCAT_WS(co_auths,',',c1id) WHERE id = name_in;
This should work using CASE to check if it's NULL:
CONCAT(
CASE
WHEN IFNULL(co_auths,'') = ''
THEN ''
ELSE CONCAT(co_auths, ',')
END, c1id)
I am generating the sql statement below based on some coldfusion logic, but it is erroring and I can't seem to find the cause, I have tried making many different modifications to it and nothing seems to be helping.
UPDATE MAIN_RECORDS
SET JONUM = NULL,
SET CUSTNAME = 'Super Sweet Name',
SET CONTACTDT = 02/28/2011,
SET ENGRECDT = 03/07/2011,
SET HOW_WR_DT = 03/07/2011,
SET COMM_DT = 03/29/2011,
SET FACAVALDT = NULL,
SET FAX_SUPDT = 03/07/2011,
SET LINENUM = 'CLPRO L6',
SET POLENUM = 'CLPRO 125 T T3',
SET REASON = '03/07/11 NO VAC FAC THIS IS THE WRONG INFORMATION IT WAS ON HERE TWICE',
SET REC_TYPE = 'H',
SET ORDER_TYPE = 'P',
SET CANCEL_ORDER = 'Y',
SET State_abbr = 'IL',
SET dbfk_state = 17,
SET xx_streetnumber = '2626',
SET xx_street = 'Fake St',
SET xx_city = 'NEWTON',
SET xx_class_of_service_ind = 'R',
SET xx_cellphone_ind = '1',
SET xx_assigned_phone = '3045653897',
SET xx_exchange_name = 'NEWTON',
SET XX_new_ref_code = '60',
SET xx_new_service_type = '11',
SET ORD_COMDT = 03/11/2011,
SET delivery_date = NULL
WHERE ordernum = '08824112' AND exchnum = '304565'
Currently the error that management studio is giving me is:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'SET'.
You only need 1 SET statement, instead of the multiple ones you have.
Also, your dates need to have single quotes around them.
e.g.:
UPDATE MAIN_RECORDS
SET JONUM = NULL,
CUSTNAME = 'Super Sweet Name',
CONTACTDT = '02/28/2011',
ENGRECDT = '03/07/2011',
HOW_WR_DT = '03/07/2011', .....
Look at the UPDATE statement. The syntax in the post is all wrong :)
The relevant portion:
SET
{ column_name = { expression | DEFAULT | NULL }
| #variable = expression
| #variable = column = expression } [ ,...n ]
Note that SET can only be specified once. The ,...n signifies the previous consuct (that in the {}) can be specified an additional n times, separated with a comma: the SET keyword itself, however, is outside that construct.
Happy coding.
Well generally the command syntax for this would follow this logic
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
so only one SET not the multiple that you have
Only one SET is needed in update Keyword to update n number of columns - example:
Update Employee
set City = Chennai,Country ='India',Employee name = 'Vignesh'
where Employee Id = 1X234
I have a MySQL stored procedure and in it, the following WHILE statement.
I have confirmed that #RowCnt is 1, and #MaxRows is 6090, however after further debugging, I realized that the WHILE statement is going through a single iteration and not continuing; so I'm hoping to have some light shed on what could possibly be causing this.
Full disclosure: I ported this from SQL Server to a MySQL stored procedure, something I have never taken on before. (meaning SQL Server, porting OR stored procedures..)
WHILE #RowCnt <= #MaxRows DO
SELECT #currentReadSeq:=ReadSeq, #currentReadStrength:=ReadStrength, #currentReadDateTime:=ReadDateTime, #currentReaderID:=ReaderID FROM tblTempRead WHERE rownum = #RowCnt;
IF ( ((#lastReadSeq + 10) > #currentReadSeq) AND (#lastReaderId = #currentReaderId) ) THEN
SET #lastReadSeq = #currentReadSeq, #lastReadStrength = #currentReadStrength, #lastReadDateTime = #currentReadDateTime, #lastReaderID = #currentReaderID;
ELSE
INSERT INTO tblreaddataresults (SiteID, ReadDateTimeStart, ReadDateTimeEnd, ReadSeqStart, ReadSeqEnd, ReaderID, DirectSeconds) VALUES ('1002', #saveReadDateTime, #lastReadDateTime, #saveReadSeq, #lastReadSeq, #lastReaderID, timestampdiff(SECOND,#saveReadDateTime,#lastReadDateTime));
SET #saveReadSeq = #currentReadSeq, #saveReadStrength = #currentReadStrength, #saveReadDateTime = #currentReadDateTime, #saveReaderID = #currentReaderID;
SET #lastReadSeq = #saveReadSeq, #lastReadStrength = #saveReadStrength, #lastReadDateTime = #saveReadDateTime, #lastReaderID = #saveReaderID;
END IF;
SET #RowCnt = #RowCnt+1;
END WHILE;
Try This Construct
WHILE (#RowCnt <= #MaxRows)
BEGIN
SELECT #currentReadSeq:=ReadSeq, #currentReadStrength:=ReadStrength, #currentReadDateTime:=ReadDateTime, #currentReaderID:=ReaderID FROM tblTempRead WHERE rownum = #RowCnt;
IF (((#lastReadSeq + 10) > #currentReadSeq) AND (#lastReaderId = #currentReaderId))
BEGIN
SET #lastReadSeq = #currentReadSeq, #lastReadStrength = #currentReadStrength, #lastReadDateTime = #currentReadDateTime, #lastReaderID = #currentReaderID;
END
ELSE
BEGIN
INSERT INTO tblreaddataresults (SiteID, ReadDateTimeStart, ReadDateTimeEnd,ReadSeqStart, ReadSeqEnd, ReaderID, DirectSeconds) VALUES ('1002',#saveReadDateTime, #lastReadDateTime, #saveReadSeq, #lastReadSeq, #lastReaderID,timestampdiff(SECOND,#saveReadDateTime,#lastReadDateTime));
SET #saveReadSeq = #currentReadSeq, #saveReadStrength = #currentReadStrength, #saveReadDateTime = #currentReadDateTime, #saveReaderID = #currentReaderID;
SET #lastReadSeq = #saveReadSeq, #lastReadStrength = #saveReadStrength,#lastReadDateTime = #saveReadDateTime, #lastReaderID = #saveReaderID;
END
SET #RowCnt = #RowCnt+1;
END
I'm using GROUP_CONCAT() in a MySQL query to convert multiple rows into a single string.
However, the maximum length of the result of this function is 1024 characters.
I'm very well aware that I can change the param group_concat_max_len to increase this limit:
SET SESSION group_concat_max_len = 1000000;
However, on the server I'm using, I can't change any param. Not by using the preceding query and not by editing any configuration file.
So my question is:
Is there any other way to get the output of a multiple row query into a single string?
SET SESSION group_concat_max_len = 1000000;
is a temporary, session-scope, setting. It only applies to the current session You should use it like this.
SET SESSION group_concat_max_len = 1000000;
select group_concat(column) from table group by column
You can do this even in sharing hosting, but when you use an other session, you need to repeat the SET SESSION command.
The correct parameter to set the maximum length is:
SET ##group_concat_max_len = value_numeric;
value_numeric must be > 1024; by default the group_concat_max_len value is 1024.
Include this setting in xampp my.ini configuration file:
[mysqld]
group_concat_max_len = 1000000
Then restart xampp mysql
You can try this
SET GLOBAL group_concat_max_len = 1000000;
The correct syntax is mysql> SET ##global.group_concat_max_len = integer;
If you do not have the privileges to do this on the server where your database resides then use a query like:
mySQL="SET ##session.group_concat_max_len = 10000;"or a different value. Next line:
SET objRS = objConn.Execute(mySQL) your variables may be different.
then
mySQL="SELECT GROUP_CONCAT(......);" etc
I use the last version since I do not have the privileges to change the default value of 1024 globally (using cPanel).
Hope this helps.
The short answer: the setting needs to be setup when the connection to the MySQL server is established. For example, if using MYSQLi / PHP, it will look something like this:
$ myConn = mysqli_init();
$ myConn->options(MYSQLI_INIT_COMMAND, 'SET SESSION group_concat_max_len = 1000000');
Therefore, if you are using a home-brewed framework, well, you need to look for the place in the code when the connection is establish and provide a sensible value.
I am still using Codeigniter 3 on 2020, so in this framework, the code to add is in the application/system/database/drivers/mysqli/mysqli_driver.php, the function is named db_connect();
public function db_connect($persistent = FALSE)
{
// Do we have a socket path?
if ($this->hostname[0] === '/')
{
$hostname = NULL;
$port = NULL;
$socket = $this->hostname;
}
else
{
$hostname = ($persistent === TRUE)
? 'p:'.$this->hostname : $this->hostname;
$port = empty($this->port) ? NULL : $this->port;
$socket = NULL;
}
$client_flags = ($this->compress === TRUE) ? MYSQLI_CLIENT_COMPRESS : 0;
$this->_mysqli = mysqli_init();
$this->_mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 10);
$this->_mysqli->options(MYSQLI_INIT_COMMAND, 'SET SESSION group_concat_max_len = 1000000');
...
}
CREATE TABLE some_table (
field1 int(11) NOT NULL AUTO_INCREMENT,
field2 varchar(10) NOT NULL,
field3 varchar(10) NOT NULL,
PRIMARY KEY (`field1`)
);
INSERT INTO `some_table` (field1, field2, field3) VALUES
(1, 'text one', 'foo'),
(2, 'text two', 'bar'),
(3, 'text three', 'data'),
(4, 'text four', 'magic');
This query is a bit strange but it does not need another query to initialize the variable; and it can be embedded in a more complex query.
It returns all the 'field2's separated by a semicolon.
SELECT result
FROM (SELECT #result := '',
(SELECT result
FROM (SELECT #result := CONCAT_WS(';', #result, field2) AS result,
LENGTH(#result) AS blength
FROM some_table
ORDER BY blength DESC
LIMIT 1) AS sub1) AS result) AS sub2;