This is my SQL query
DECLARE #strManualRefundIdList AS VARCHAR(MAX)
SELECT #strManualRefundIdList = COALESCE(#strManualRefundIdList + '|', '') + CONVERT(VARCHAR(MAX), ManualRefund_strReasonCode)
FROM tblManualRefunds WHERE ManualRefund_lngId IN ( 20 ,21 ,22 )
SELECT #strManualRefundIdList;
This gives like pivot, all the rows concatenated in a single row.
The Same i need to convert into MySql Query.
I tried like this
SELECT
CONCAT(COALESCE(CONCAT(v_strManualRefundIdList, '|'), '') , ( ManualRefund_lngId))
INTO
v_strManualRefundIdList
FROM tblManualRefunds
WHERE Trans_lngId IN ( 20 ,21 ,22 ) ;
But it throws error as Error Code: 1172. Result consisted of more than one row
How to translate that query. I am new to database.
Please help me in figuring out this.
UPDATE :
The way i found out was assigning into a cursor and loop through and concatenate it.
But is that the only way ? or any better way is available ?
SELECT group_concat(ManualRefund_strReasonCode SEPARATOR '|')
FROM tblManualRefunds
WHERE manualRefund_lngId in (20,21,22)
SQL Fiddle
As per xQbert Comment i tried this
SELECT
GROUP_CONCAT(COALESCE(CONCAT('shan', '|'), '') , ( ManualRefund_lngId))
INTO
#v_strManualRefundIdList
FROM tblManualRefunds ;
Its working fine.
Thanks Man.. !!!
Related
I've got the following tables in my database:
this is the table km_kondomanager_millesimal_table_values
this is the table km_kondomanager_millesimal_table
The two tables are joined together by km_kondomanager_millesimal_table_value_table_id and km_kondomanager_millesimal_table_id
I've managed to creat a pivot table using the followinf query:
SELECT km_kondomanager_millesimal_table_value_building_id
,SUM(CASE WHEN km_kondomanager_millesimal_table_value_table_id = 9
THEN km_kondomanager_millesimal_table_millesimal_value END) tabella_gruppo_A
,SUM(CASE WHEN km_kondomanager_millesimal_table_value_table_id = 10
THEN km_kondomanager_millesimal_table_millesimal_value END) tabella_gruppo_B
FROM km_kondomanager_millesimal_table_values
WHERE km_kondomanager_millesimal_table_value_group_id = 15
GROUP BY km_kondomanager_millesimal_table_value_building_id
and this is how it looks like:
My problem is that I need to take the table name from km_kondomanager_millesimal_table and use it as column, these columns are not always the same as you can see in the immage there are other stored in the database. The query I've created requires to specify km_kondomanager_millesimal_table_value_table_id in the case and also to specify the column name in the example tabella_gruppo_A and tabella_gruppo_B. Can anyboby help me to achieve this? Also I need to convert the pit table to HTML table. Many thanks
I've managed to get it sorted using mysql stored procedure and this is the code:
BEGIN
SELECT
GROUP_CONCAT(
CONCAT("MAX(IF(km_kondomanager_millesimal_table_value_table_id='", km_kondomanager_millesimal_table_value_table_id, "',km_kondomanager_millesimal_table_millesimal_value ,NULL)) AS '", km_kondomanager_millesimal_table_name, "'"), "
"
)INTO #answers
FROM (
SELECT DISTINCT km_kondomanager_millesimal_table_value_table_id, km_kondomanager_millesimal_table_name FROM km_kondomanager_millesimal_table_values INNER JOIN km_kondomanager_millesimal_table
ON km_kondomanager_millesimal_table_values . km_kondomanager_millesimal_table_value_table_id = km_kondomanager_millesimal_table. km_kondomanager_millesimal_table_id WHERE km_kondomanager_millesimal_table_value_group_id = 13
) A;
SET #query :=
CONCAT(
'SELECT km_kondomanager_millesimal_table_value_building_id, ', #answers,
' FROM km_kondomanager_millesimal_table_values WHERE km_kondomanager_millesimal_table_value_group_id = 13 GROUP BY km_kondomanager_millesimal_table_value_building_id'
);
PREPARE statement FROM #query;
EXECUTE statement;
END
How i can get value like this in a variable 'TFSEP-2019','TFjul-2018','TFJun-2018' without spaces.
SELECT s.house, s.grade, s.homeroom AS Campus
FROM student s,fees_jrl f
WHERE s.studnum = f.studnum AND
f.name IN (' TFJun-2018 TFJul-2018 ') AND
f.trans_type= 'chg' AND
f.paid_id is NULL AND s.house LIKE '%'
GROUP BY s.house
I am getting like this ('TFJun-2018 TFJUL-2018 TFSEP-2019') but I want like ('TFSEP-2019','TFjul-2018','TFJun-2018') please help
You may refer the following statements with like operators, whenever you need quotes('), use doubled-quotes('') :
create table tab ( Id int, expression varchar(100));
insert into tab values(1 ,'TFJun-2018 TFJUL-2018 TFSEP-2019');
insert into tab values(2,'''TFJun-2018'',''TFJUL-2018'',''TFSEP-2019''');
select * from tab where expression like '''TFJun-2018''%';
Id expression
2 'TFJun-2018','TFJUL-2018','TFSEP-2019'
or
select * from tab where expression like '''TFJun-2018'',''TFJUL-2018''%';
Id expression
2 'TFJun-2018','TFJUL-2018','TFSEP-2019'
or
select * from tab where expression like '''TFJun-2018'',''TFJUL-2018'',''TFSEP-2019''%';
Id expression
2 'TFJun-2018','TFJUL-2018','TFSEP-2019'
Rextester Demo
You can use like for strings
% means any chars, _ means one char
So doing
F.name like %2018%
Will give you all of 2018..
use explicit join and concat
SELECT
concat( concat( concat( concat("'",s.house),"'"),concat( concat("'",s.grade),"'")),
concat( concat("'",s.homeroom),"'"))
FROM student s join fees_jrl f
on s.studnum = f.studnum
where f.name IN (' TFJun-2018 TFJul-2018 ') AND
f.trans_type= 'chg' AND
f.paid_id is NULL AND s.house LIKE '%'
I think you dont need group by as you have no aggregation function
I am trying to format the licenseenum column in my table by removing everything starting the before space and also I would like to remove any character in licenseenum column starting after '-' including '-'
For example:
current data in Licenseenum GA 350-0
What I'm trying to get 350
Here is my code
select Licenseenum, SUBSTRING (Licenseenum, 4, LEN(Licenseenum)-1)
from licensee
this results
350-0
How would I remove -0 from the results?
Thanks for the help
Try it like this
DECLARE #YourString VARCHAR(100)='GA 350-0';
SELECT SUBSTRING(#YourString,CHARINDEX(' ',#YourString,1),CHARINDEX('-',#YourString,1)-CHARINDEX(' ',#YourString,1));
UPDATE 1
This is quite the same, but better to read
DECLARE #YourString VARCHAR(100)='GA 350-0';
WITH Positions AS
(
SELECT CHARINDEX(' ',#YourString,1) AS posBlank
,CHARINDEX('-',#YourString,1) AS posMinus
)
SELECT SUBSTRING(#YourString,posBlank,posMinus-posBlank)
FROM Positions;
UPDATE 2 Avoid the leading blank...
My logic needs small correction in order to cut the blank before the number:
SELECT SUBSTRING(#YourString,posBlank+1,posMinus-posBlank-1)
Would be the same with the first example...
Please try the below code. Its working fine in SQL Server 2012.
DECLARE #Licenseenum varchar(max)
SET #Licenseenum ='GA 350-0'
DECLARE #TempLicense VARCHAR(100)
DECLARE #License VARCHAR(100)
IF (len(#Licenseenum ) - len(replace(#Licenseenum ,' ',''))>=1)
SET #TempLicense = (SELECT REVERSE(LEFT(REVERSE(#Licenseenum ),CHARINDEX(' ', REVERSE(#Licenseenum ), 1) - 1)))
ELSE
SET #TempLicense = #Licenseenum
SELECT #License = (SELECT LEFT(#TempLicense,LEN(#TempLicense) - charindex('-',reverse(#TempLicense),1)))
SELECT #License AS Licenseenum
SQL statement:
(select top 1 [egrp_name] from [Enotify Group] where [egrp_id] in (a.grp_id) )
e value of a.grp_id is '0,1145' and i am getting error
Conversion failed when converting the varchar value '0,1145' to data type int.
Can anybody tell me how can i change '0,1145' to 0,1145 in above case, so my query does work and also if their is any other way to do this
You can use a split-string function to change your comma delimited string into a table.
select top(1) [egrp_name]
from [Enotify Group]
where [egrp_id] in (
select Value
from dbo.SplitInts(a.grp_id)
);
One version of a split-string function that you can use if you like:
create function dbo.SplitInts(#Values nvarchar(max)) returns table with schemabinding
as
return
(
select T2.X.value(N'.', N'int') as Value
from (select cast(N'<?X '+replace(#Values, N',', N'?><?X ') + N'?>' as xml).query(N'.')) as T1(X)
cross apply T1.X.nodes(N'/processing-instruction("X")') as T2(X)
);
Or you can use like.
select top(1) [egrp_name]
from [Enotify Group]
where ','+a.grp_id +',' like '%,'+cast(egrp_id as varchar(11))+',%' ;
I'm don't have a lot of knowledge of MySql (or SQL in general) so sorry for the noobness.
I'm trying to update a bunch of String entries this way:
Lets say we have this:
commands.firm.pm.Stuff
Well I want to convert that into:
commands.firm.pm.print.Stuff
Meaning, Add the .print after pm, before "Stuff" (where Stuff can be any Alphanumerical String).
How would I do this with a MySql Query? I'm sure REGEXP has to be used, but I'm not sure how to go about it.
Thanks
Try something like this. It finds the last period and inserts your string there:
select insert(s, length(s) - instr(reverse(s), '.') + 1, 0, '.print')
from (
select 'commands.firm.pm.Stuff' as s
) a
To update:
update MyTable
set MyColumn = insert(MyColumn, length(MyColumn) - instr(reverse(MyColumn), '.') + 1, 0, '.print')
where MyColumn like 'commands.firm.pm.%'
Perhaps use a str_replace to replace commands.firm.pm to commands.firm.pm.print
$original_str = "commands.firm.pm.15hhkl15k0fak1";
str_replace("commands.firm.pm", "commands.firm.pm.print", $original_str);
should output: commands.firm.pm.print.15hhkl15k0fak1
then update your table with the new value...How to do it all in one query (get column value and do the update), I do not know. All I can think of is you getting the column value in one query, doing the replacement above, and then updating the column with the new value in a second query.
To update rows that end in '.Stuff' only:
UPDATE TableX
SET Column = CONCAT( LEFT( CHAR_LENGTH(Column) - CHAR_LENGTH('.Stuff') )
, '.print'
, '.Stuff'
)
WHERE Column LIKE '%.Stuff'
To update all rows - by appending .print just before the last dot .:
UPDATE TableX
SET Column = CONCAT( LEFT( CHAR_LENGTH(Column)
- CHAR_LENGTH(SUBSTRING_INDEX(Column, '.', -1))
)
, 'print.'
, SUBSTRING_INDEX(Column, '.', -1)
)
WHERE Column LIKE '%.%'