Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,
I have two strings, now I need to merge one string into another in the place where I have mentioned using MySQL. I wrote the code but it is not working correctly

DELIMITER $$DROP FUNCTION IF EXISTS `test`.`dateformat1`$$
CREATE FUNCTION dateformat1 ( monthyear  nvarchar(80), day nvarchar(80), n Int)
	RETURNS  nvarchar(80)BEGIN
 Declare s1 varchar(80);
	Declare s2 varchar(80);
	declare i int;
	set i=1;
	set s1 = '';
	loop1: loop 
	              
			SET s1 = s1 + substring(monthyear,i,1);
                        IF i = n   then
			      LEAVE loop1;
			ELSE
				set i = i+1;
				ITERATE loop1;
                        END IF;
        END loop loop1;



        SET s1 = s1 + day + substring(monthyear,i,length(monthyear)-i+1 );
	RETURN s1;

END$$DELIMITER ;
select dateformat1 ('09/2009','01/',4);


I am getting incorrect answers. The output I need is 09/01/2009. I am using MySQL version 5.x
Posted
Updated 19-Dec-09 22:52pm
v2

This looks like a disaster. Why on earth do you need to do this inside the DB ? Why are you storing dates as strings at all ?
 
Share this answer
 
Who is the 'they' that gave you this task, and why ? Don't post an 'answer' to add to your question, edit your post, so when someone is replying, they can see all you've posted. SQL dialects differ, it's not surprising to me that MySQL doesn't work the same as SQL Server. What result are you getting ? Again, why are you doing this at all ?
 
Share this answer
 
they gave me this type of task ok and if i keep string in that also it is not appending. could u please find out where thertr is wrong. same program while i am using sqlserver2005 it is getting but in mysql it is not getting
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900