I am facing a serious problem in mysql query while sorting a column in database. If i am sorting a column of table having rows with and with out html tags then i am not getting correct sorting order due to html tags added before some of the text Like below :-
Solutions : To prevent this problem we need first to ignore html tags and then run sorting query to perform sorting operation. But the question is how we can do this. After alot of troubling and googling finally i find out the sollution. Here is a sql function which will solve my problem and ofcouse your also.
Method of users :-
Step 1 - First of all you need to run below function in your sql/mysql database. Just copy and paste into your database.
Step 2 - Now we need to call the above function in our query like below "SELECT fnStripTags(message) FROM notes ORDER BY message desc"
Chears..
Happy Coding :)
Solutions : To prevent this problem we need first to ignore html tags and then run sorting query to perform sorting operation. But the question is how we can do this. After alot of troubling and googling finally i find out the sollution. Here is a sql function which will solve my problem and ofcouse your also.
Method of users :-
Step 1 - First of all you need to run below function in your sql/mysql database. Just copy and paste into your database.
DROP FUNCTION IF EXISTS fnStripTags;
DELIMITER |
CREATE FUNCTION fnStripTags( Dirty varchar(4000) )
RETURNS varchar(4000)
DETERMINISTIC
BEGIN
DECLARE iStart, iEnd, iLength int;
WHILE Locate( '<', Dirty ) > 0 And Locate( '>', Dirty, Locate( '<', Dirty )) > 0 DO
BEGIN
SET iStart = Locate( '<', Dirty ), iEnd = Locate( '>', Dirty, Locate('<', Dirty ));
SET iLength = ( iEnd - iStart) + 1;
IF iLength > 0 THEN
BEGIN
SET Dirty = Insert( Dirty, iStart, iLength, '');
END;
END IF;
END;
END WHILE;
RETURN Dirty;
END;
|
DELIMITER ;
Step 2 - Now we need to call the above function in our query like below "SELECT fnStripTags(message) FROM notes ORDER BY message desc"
Chears..
Happy Coding :)
Woking fine. !
ReplyDeleteThanks a Lot
Hi,
ReplyDeleteI have copied and pasted the code in a query but is not workin, I am getting the following errors:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'IF'.
Msg 111, Level 15, State 1, Line 3
'CREATE FUNCTION' must be the first statement in a query batch.
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near ','.
Msg 195, Level 15, State 10, Line 8
'Locate' is not a recognized built-in function name.
Msg 102, Level 15, State 1, Line 10
Incorrect syntax near '='.
Msg 156, Level 15, State 1, Line 12
Incorrect syntax near the keyword 'THEN'.
Msg 102, Level 15, State 1, Line 14
Incorrect syntax near '='.
Msg 102, Level 15, State 1, Line 16
Incorrect syntax near ';'.
Msg 156, Level 15, State 1, Line 18
Incorrect syntax near the keyword 'WHILE'.
Msg 178, Level 15, State 1, Line 19
A RETURN statement with a return value cannot be used in this context.
Can you please help me with this?
Thanks For the code.
ReplyDeleteIts Working good.
Thanks once again.
working but too slow
ReplyDeleteWOW.. Thanks!! Works great. Saved alot of work
ReplyDelete