Split a String into fixed length parts in SQL Server 2008 - sql-server-2008

I am working with Split string method, I have done with splitting a string into fixed length chunks, but the issue is, it breaks the word , I want to keep intact the word and on the base of "SPACE" character ,Split down.
Below I am attaching the Function with test result as well.
Kindly guide or instruct me how to work with.
/*
Select dbo.SplitFixedLengthString('This is me , I am going to split this string in such a way that it will not break any word, rather it keeps word',16)
*/
CREATE FUNCTION [dbo].[SplitFixedLengthString]
(
#string NVARCHAR(MAX) ,
#stringlength INT
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
DECLARE #tempStr NVARCHAR(MAX)
DECLARE #finalString NVARCHAR(MAX)
IF LEN(#string) > 0
AND #stringlength > 0
BEGIN
SELECT #tempStr = ''
DECLARE #i INT
SET #i = 1
WHILE #i <= LEN(#string)
BEGIN
SELECT #tempStr = #tempStr + SUBSTRING(#string, #i,#stringlength) + (CHAR(13) + CHAR(10))
SET #i = #i + #stringlength
END
END
RETURN #tempStr
END
Test Result:
This is me , I a
m going to split
this string in
such a way that
it will not brea
k any word, rath
er it keeps word
(1 row(s) affected)
As we can see the line one of output is splitting the word "am" like wise the line 4,5.
suggest me solution please.
Here are my efforts:
--DECLARE #x VARCHAR(32) = 'xyzxyzyyythgetdghydgsh j';
--SELECT LEN(#x) - CHARINDEX(' ', REVERSE(#x)) + 1;
DECLARE #string NVARCHAR(MAX)
SELECT #string = 'This is me , I am going to split this string in such a way that it will not break any word, rather it keeps word'
--SELECT #string = 'This is me,I am going to Trim off things in such a way that it will add newline having '
DECLARE #stringlength INT
SELECT #stringlength = 11
BEGIN
DECLARE #tempStr NVARCHAR(MAX)
DECLARE #finalString NVARCHAR(MAX) = ''
IF LEN(#string) > 0
AND #stringlength > 0
BEGIN
SELECT #tempStr = ''
DECLARE #start_index INT = 1
DECLARE #last_index INT = 0
DECLARE #lastindex INT = LEN(#string)-1
WHILE #start_index < #lastindex
BEGIN
SELECT #tempStr = SUBSTRING(#string, #start_index,#stringlength )
IF RIGHT(#tempStr,1) = ' '
BEGIN
SET #finalString = #finalString + #tempStr + (CHAR(13) + CHAR(10))
SET #start_index = #start_index + #stringlength
END
ELSE
BEGIN
--SELECT #last_index = LEN(#tempStr) - CHARINDEX(' ', REVERSE(#tempStr)) + 1;
SELECT #last_index = LEN(#tempStr) - CHARINDEX(' ',REVERSE(#tempStr))
IF #last_index = 0
BEGIN
SET #finalString = #finalString + LTRIM(SUBSTRING(#tempStr, 1,LEN(#tempStr)+1))+ (CHAR(13) + CHAR(10))
SET #start_index = #start_index + LEN(#tempStr)
END
ELSE
BEGIN
SET #finalString = #finalString + LTRIM(SUBSTRING(#tempStr, 1,#last_index +1))+ (CHAR(13) + CHAR(10))
SET #start_index = #start_index + #last_index
END
END
IF #start_index + #stringlength >= #lastindex
BEGIN
SET #finalString = #finalString + LTRIM(SUBSTRING(#string, #start_index,(#lastindex - #start_index)+1))+ (CHAR(13) + CHAR(10))
SET #start_index = #start_index + (#lastindex - #start_index)
END
END
END
SELECT #finalString
END
Here is the output
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
This is me
, I am
going to
split
this
string in
such a way
that it
will not
break any
word,
rather it
keeps wor
(1 row(s) affected)

After a little effort I have written this code. and its working fine, although a bit tricky task.
ALTER FUNCTION [dbo].[SplitFixedLengthString]
(
#string NVARCHAR(MAX) ,
#stringlength INT
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
DECLARE #tempStr NVARCHAR(MAX)
DECLARE #finalString NVARCHAR(MAX) = ''
IF LEN(#string) > 0
AND #stringlength > 0
BEGIN
SELECT #tempStr = ''
DECLARE #start_index INT = 1
DECLARE #last_index INT = 0
DECLARE #lastindex INT = LEN(#string)
DECLARE #NextChar VARCHAR(1) = ''
WHILE #start_index < #lastindex
BEGIN
SELECT #tempStr = SUBSTRING(#string, #start_index,#stringlength )
IF RIGHT(#tempStr,1) = ' '
BEGIN
SET #finalString = #finalString + #tempStr + (CHAR(13) + CHAR(10))
SET #start_index = #start_index + #stringlength
END
ELSE
BEGIN
SELECT #last_index = LEN(#tempStr) - CHARINDEX(' ',REVERSE(#tempStr))
SET #NextChar = SUBSTRING(#string, #start_index +LEN(#tempStr),1)
IF #last_index = 0 AND #NextChar = ' '
BEGIN
SET #finalString = #finalString + LTRIM(SUBSTRING(#tempStr, 1,LEN(#tempStr)-1))+(CHAR(13) )
SET #start_index = #start_index + LEN(#tempStr)
END
IF #last_index = 0 AND #NextChar <> ' '
BEGIN
SET #finalString = #finalString + LTRIM(SUBSTRING(#tempStr, 1,LEN(#tempStr)-1))+ '_'+ (CHAR(13) )
SET #start_index = #start_index + LEN(#tempStr) -1
END
ELSE IF (#last_index) = LEN(#tempStr)
BEGIN
SET #finalString = #finalString + LTRIM(SUBSTRING(#tempStr, 1,LEN(#tempStr)-1))+ '_'+ (CHAR(13) )
SET #start_index = #start_index + LEN(#tempStr) -1
END
ELSE IF #last_index <> 0 AND #NextChar = ' '
BEGIN
SET #finalString = #finalString + LTRIM(SUBSTRING(#tempStr, 1,LEN(#tempStr)))+ (CHAR(13) )
SET #start_index = #start_index + LEN(#tempStr)
END
ELSE IF (LEN(#tempStr) - #last_index) = 2 AND #NextChar <> ' '
BEGIN
SET #finalString = #finalString + LTRIM(SUBSTRING(#tempStr, 1,LEN(#tempStr)-1))+ (CHAR(13) )
SET #start_index = #start_index + LEN(#tempStr) -1
END
ELSE IF (#last_index) <> 0 AND #NextChar <> ' '
BEGIN
SET #finalString = #finalString + LTRIM(SUBSTRING(#tempStr, 1,LEN(#tempStr)-1))+ '_'+ (CHAR(13) )
SET #start_index = #start_index + LEN(#tempStr) -1
END
ELSE
BEGIN
SET #finalString = #finalString + LTRIM(SUBSTRING(#tempStr, 1,#last_index ))+ (CHAR(13) )
SET #start_index = #start_index + #last_index
END
END
IF #start_index + #stringlength >= #lastindex
BEGIN
SET #finalString = #finalString + LTRIM(SUBSTRING(#string, #start_index,(#lastindex - #start_index)+1))+ (CHAR(13) )
SET #start_index = #start_index + (#lastindex - #start_index)
END
END
END
RETURN #finalString
END

Related

How to two used while Syntax in Mysql Procecure

I would like to write two While Syntax.
The results I expect are as follows.
when 'H'
tagName1, tagName2, tagname3
when 'D'
tagName1, tagName2, tagname3
when 'M'
tagName1, tagName2, tagname3
when 'Y'
tagName1, tagName2, tagname3
But it didn't worked....
Below is the code that I wrote, while I play it only once.
Any advice, please..
DECLARE tagList varchar(255) DEFAULT 'tagName1,tagName2,tagName3';
DECLARE tagTypeList varchar(150) DEFAULT 'H,D,M,Y';
WHILE tagTypeList != '' DO
WHILE tagList != '' DO
SET tagNameArray = SUBSTRING_INDEX(tagList, ',', 1);
SET tagTypeArray = SUBSTRING_INDEX(tagTypeList, ',', 1);
IF(tagTypeArray = 'H') THEN
SET aDate = (SELECT (DATE_ADD(nDateTime, INTERVAL -1 HOUR)));
SET ago_Y = (SELECT DATE_FORMAT(aDate,'%Y'));
SET ago_M = (SELECT DATE_FORMAT(aDate,'%m'));
SET ago_D = (SELECT DATE_FORMAT(aDate,'%d'));
SET ago_H = (SELECT DATE_FORMAT(aDate,'%H'));
SET ago_W = (SELECT DATE_FORMAT(aDate,'%w'));
ELSEIF(tagTypeArray = 'D') THEN
SET aDate = (SELECT (DATE_ADD(nDateTime, INTERVAL -1 DAY)));
SET ago_Y = (SELECT DATE_FORMAT(aDate,'%Y'));
SET ago_M = (SELECT DATE_FORMAT(aDate,'%m'));
SET ago_D = (SELECT DATE_FORMAT(aDate,'%d'));
SET ago_H = 0;
SET ago_W = 0;
ELSEIF(tagTypeArray = 'M') THEN
SET aDate = (SELECT (DATE_ADD(nDateTime, INTERVAL -1 MONTH)));
SET ago_Y = (SELECT DATE_FORMAT(aDate,'%Y'));
SET ago_M = (SELECT DATE_FORMAT(aDate,'%m'));
SET ago_D = 0;
SET ago_H = 0;
SET ago_W = 0;
ELSEIF(tagTypeArray = 'Y') THEN
SET aDate = (SELECT (DATE_ADD(nDateTime, INTERVAL -1 YEAR)));
SET ago_Y = (SELECT DATE_FORMAT(aDate,'%Y'));
SET ago_M = 0;
SET ago_D = 0;
SET ago_H = 0;
SET ago_W = 0;
END IF;
SET selectValue = (SELECT tagvalue FROM datasource WHERE tagname = tagNameArray and tagtype = tagTypeArray and y=ago_Y and m=ago_M and d=ago_D and h=ago_H);
IF(selectValue IS NULL OR selectValue = '')
THEN
SET old_Value_3M = (Select LastValue from BwAnalogTable where TagName = tagNameArray and LogDate >= ago_3M order by LogDate desc, LogTime desc limit 1);
# Here Insert Querty
END IF;
IF LOCATE(',', tagList) > 0 THEN
SET tagList = SUBSTRING(tagList, LOCATE(',', tagList) + 1);
ELSE
SET tagList = '';
END IF;
END WHILE;
IF LOCATE(',', tagTypeList) > 0 THEN
SET tagTypeList = SUBSTRING(tagTypeList, LOCATE(',', tagTypeList) + 1);
ELSE
SET tagTypeList = '';
END IF;
END WHILE;
I have modified the code as shown below.
DECLARE done INT DEFAULT FALSE;
DECLARE tagType2 VARCHAR(255) DEFAULT '';
DECLARE tagName2 VARCHAR(255) DEFAULT '';
DECLARE tagType_Cur CURSOR for select tagType from tbtagtype;
DECLARE tagName_Cur CURSOR for select tagName from tbtaglist;
OPEN tagType_Cur;
LOOP1: LOOP
FETCH tagType_Cur INTO tagType2;
IF done THEN
CLOSE tagType_Cur;
LEAVE LOOP1;
END IF;
########## here insert your core query ###############
OPEN tagName_Cur;
LOOP2: LOOP
FETCH tagName_Cur INTO tagName2;
IF done THEN
CLOSE tagName_Cur;
SET done = FALSE;
LEAVE LOOP2;
END IF;
########## here insert your core query ###############
END LOOP LOOP2;
END LOOP LOOP1;

get startdate and enddate from specified week of the month

I need to find start date and end date from specified (1/2/3/4/5) week of month. So that I can get the records where Date_Created comes in specified (1/2/3/4/5) Week.
For example if
I choose 1st week of febuary 2013 then
I want startdate = 2/1/2013 and enddate = 2/2/2013
I choose 2nd week of febuary 2013 then
I want startdate = 2/3/2013 and enddate = 2/9/2013
and similary for rest weeks.
I have SQL Server 2008 at Database Side. If anyone have Idea then Please Share.
Here is a query to get all weeks start and end dates for given month. So you can select from this query week you need:
SQLFiddle example
with C(i) as
( select CAST('2013-02-01' as datetime) i
UNION ALL
select DATEADD (day,1,i) i from C
where DATEADD (day,1,i)
<DATEADD(month,1,'2013-02-01')
), C1 as
(
select DATEPART(WEEK,i)-DATEPART(WEEK,'2013-02-01')+1 WeekOfMonth,i from C
)
select WeekOfMonth,min(i) StartDate, max(i) EndDate from C1 group by WeekOfMonth
I wrote a procedure to solve my problem which will take week no(1/2/3/4/5) as input and will return startdate and enddate
ALTER PROCEDURE dbo.SPGetStartAndEndDateofSpcifiedWeek
#Week int
AS
SET NOCOUNT ON
DECLARE #date DateTime
DECLARE #currdate DateTime
DECLARE #startdate DateTime
DECLARE #enddate DateTime
DECLARE #CurrWeek int
/*SET #date = CONVERT(date,GETDATE())*/
SET #currdate = CONVERT(date,GETDATE())
SET #CurrWeek = datediff(week, dateadd(week, datediff(week, 0, dateadd(month, datediff(month, 0, GETDATE()), 0)), 0), GETDATE() - 1) + 1
IF (#Week = 1)
BEGIN
IF (#CurrWeek = 1)
BEGIN SET #date = #currdate END
IF (#CurrWeek = 2)
BEGIN SET #date = #currdate - 7 END
IF (#CurrWeek = 3)
BEGIN SET #date = #currdate - 14 END
IF (#CurrWeek = 4)
BEGIN SET #date = #currdate - 21 END
IF (#CurrWeek = 5)
BEGIN SET #date = #currdate - 28 END
END
IF (#Week = 2)
BEGIN
IF (#CurrWeek = 1)
BEGIN SET #date = #currdate + 7 END
IF (#CurrWeek = 2)
BEGIN SET #date = #currdate END
IF (#CurrWeek = 3)
BEGIN SET #date = #currdate - 7 END
IF (#CurrWeek = 4)
BEGIN SET #date = #currdate - 14 END
IF (#CurrWeek = 5)
BEGIN SET #date = #currdate - 21 END
END
IF (#Week = 3)
BEGIN
IF (#CurrWeek = 1)
BEGIN SET #date = #currdate + 14 END
IF (#CurrWeek = 2)
BEGIN SET #date = #currdate + 7 END
IF (#CurrWeek = 3)
BEGIN SET #date = #currdate END
IF (#CurrWeek = 4)
BEGIN SET #date = #currdate - 7 END
IF (#CurrWeek = 5)
BEGIN SET #date = #currdate - 14 END
END
IF (#Week = 4)
BEGIN
IF (#CurrWeek = 1)
BEGIN SET #date = #currdate + 21 END
IF (#CurrWeek = 2)
BEGIN SET #date = #currdate + 14 END
IF (#CurrWeek = 3)
BEGIN SET #date = #currdate + 7 END
IF (#CurrWeek = 4)
BEGIN SET #date = #currdate END
IF (#CurrWeek = 5)
BEGIN SET #date = #currdate - 7 END
END
IF (#Week = 5)
BEGIN
IF (#CurrWeek = 1)
BEGIN SET #date = #currdate + 28 END
IF (#CurrWeek = 2)
BEGIN SET #date = #currdate + 21 END
IF (#CurrWeek = 3)
BEGIN SET #date = #currdate + 14 END
IF (#CurrWeek = 4)
BEGIN SET #date = #currdate + 7 END
IF (#CurrWeek = 5)
BEGIN SET #date = #currdate END
END
Select CONVERT(date, DATEADD(wk, DATEDIFF(wk, 0, #date), - 1)) as startdate,
CONVERT(date, DATEADD(wk, DATEDIFF(wk, 0, #date), 5)) as enddate
RETURN
I modified code given by Valex to get the exact output as I required.
CREATE PROCEDURE dbo.SPReturnStartEndDateOfSpecifiedWeek
#Week int,
#P_startdate DateTime OUTPUT,
#P_enddate DateTime OUTPUT
AS
/* SET NOCOUNT ON */
with C(i) as
(
select CAST((DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)) as datetime) i
UNION ALL
select DATEADD (day,1,i) i from C
where DATEADD (day,1,i)<DATEADD(month,1,(DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)))
),
C1 as
(
select DATEPART(WEEK,i)-DATEPART(WEEK,(DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)))+1 WeekOfMonth,i from C
),
C2 as
(
select WeekOfMonth,min(i) StartDate, max(i) EndDate from C1 group by WeekOfMonth
)
Select #P_startdate = StartDate,
#P_enddate = EndDate
from C2
WHERE WeekOfMonth=#Week
RETURN
You can try this to get the start or end date for the week. Just provide a date for which you find the dates.
Logic is quite simple just get the start and end date of the week and then check if the start date is greater than the month date if so take the month date as the week start date else the start date of the week.
Fiddle a bit to get what you want
Here is the code:
DECLARE #date datetime
SET #date = '2013-01-30'
DECLARE #startdate datetime
DECLARE #enddate datetime
DECLARE #MonthStart datetime
Declare #MonthEnd datetime
SET #startdate = DATEADD(wk, DATEDIFF(wk, 6, #date), 6)
SET #enddate = DATEADD(dd,6, DATEADD(wk, DATEDIFF(wk, 6, #date), 6))
SET #monthStart = CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(#date)-1),#date),101)
SET #MonthEnd =CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,#date))),DATEADD(mm,1,#date)),101)
--select #startdate wstart,#MonthStart monthstart,#enddate wend,#MonthEnd monthend
select
[date] = #date
,[week] = DATEPART(wk,#date)
,[WeekStartDate] = Case
WHEN #startdate <= #MonthStart then #MonthStart else #startdate
END
,[WeekEndDate] = Case
WHEN #enddate > #MonthEnd then #MonthEnd else #enddate
END
see here:
Fiddle

how to solve access denied into my mysql database

I don't know when I run the following code in phpMyAdmin
SET GLOBAL log_bin_trust_function_creators = 1;
-- ----------------------------
-- Function structure for `__mydiv`
-- ----------------------------
DROP FUNCTION IF EXISTS `__mydiv`;
DELIMITER ;;
CREATE FUNCTION `__mydiv`(`a` int, `b` int) RETURNS bigint(20)
BEGIN
return FLOOR(a / b);
END;;
DELIMITER ;
-- ----------------------------
-- Function structure for `__mymod`
-- ----------------------------
DROP FUNCTION IF EXISTS `__mymod`;
DELIMITER ;;
CREATE FUNCTION `__mymod`(`a` int, `b` int) RETURNS bigint(20)
BEGIN
return (a - b * FLOOR(a / b));
END;;
DELIMITER ;
-- ----------------------------
-- Function structure for `_gdmarray`
-- ----------------------------
DROP FUNCTION IF EXISTS `_gdmarray`;
DELIMITER ;;
CREATE FUNCTION `_gdmarray`(`m` smallint) RETURNS smallint(2)
BEGIN
CASE m
WHEN 0 THEN RETURN 31;
WHEN 1 THEN RETURN 28;
WHEN 2 THEN RETURN 31;
WHEN 3 THEN RETURN 30;
WHEN 4 THEN RETURN 31;
WHEN 5 THEN RETURN 30;
WHEN 6 THEN RETURN 31;
WHEN 7 THEN RETURN 31;
WHEN 8 THEN RETURN 30;
WHEN 9 THEN RETURN 31;
WHEN 10 THEN RETURN 30;
WHEN 11 THEN RETURN 31;
END CASE;
END;;
DELIMITER ;
-- ----------------------------
-- Function structure for `_jdmarray`
-- ----------------------------
DROP FUNCTION IF EXISTS `_jdmarray`;
DELIMITER ;;
CREATE FUNCTION `_jdmarray`(`m` smallint) RETURNS smallint(2)
BEGIN
CASE m
WHEN 0 THEN RETURN 31;
WHEN 1 THEN RETURN 31;
WHEN 2 THEN RETURN 31;
WHEN 3 THEN RETURN 31;
WHEN 4 THEN RETURN 31;
WHEN 5 THEN RETURN 31;
WHEN 6 THEN RETURN 30;
WHEN 7 THEN RETURN 30;
WHEN 8 THEN RETURN 30;
WHEN 9 THEN RETURN 30;
WHEN 10 THEN RETURN 30;
WHEN 11 THEN RETURN 29;
END CASE;
END;;
DELIMITER ;
-- ----------------------------
-- Function structure for `_jdmarray2`
-- ----------------------------
DROP FUNCTION IF EXISTS `_jdmarray2`;
DELIMITER ;;
CREATE FUNCTION `_jdmarray2`(`m` smallint) RETURNS smallint(2)
BEGIN
CASE m
WHEN 1 THEN RETURN 31;
WHEN 2 THEN RETURN 31;
WHEN 3 THEN RETURN 31;
WHEN 4 THEN RETURN 31;
WHEN 5 THEN RETURN 31;
WHEN 6 THEN RETURN 31;
WHEN 7 THEN RETURN 30;
WHEN 8 THEN RETURN 30;
WHEN 9 THEN RETURN 30;
WHEN 10 THEN RETURN 30;
WHEN 11 THEN RETURN 30;
WHEN 12 THEN RETURN 29;
END CASE;
END;;
DELIMITER ;
-- ----------------------------
-- Function structure for `pdate`
-- ----------------------------
DROP FUNCTION IF EXISTS `pdate`;
DELIMITER ;;
CREATE FUNCTION `pdate`(`gdate` datetime) RETURNS char(100) CHARSET utf8
BEGIN
DECLARE
i,
gy, gm, gd,
g_day_no, j_day_no, j_np,
jy, jm, jd INT DEFAULT 0; /* Can be unsigned int? */
DECLARE resout char(100);
DECLARE ttime CHAR(20);
SET gy = YEAR(gdate) - 1600;
SET gm = MONTH(gdate) - 1;
SET gd = DAY(gdate) - 1;
SET ttime = TIME(gdate);
SET g_day_no = ((365 * gy) + __mydiv(gy + 3, 4) - __mydiv(gy + 99, 100) + __mydiv (gy + 399, 400));
SET i = 0;
WHILE (i < gm) do
SET g_day_no = g_day_no + _gdmarray(i);
SET i = i + 1;
END WHILE;
IF gm > 1 and ((gy % 4 = 0 and gy % 100 <> 0)) or gy % 400 = 0 THEN
SET g_day_no = g_day_no + 1;
END IF;
SET g_day_no = g_day_no + gd;
SET j_day_no = g_day_no - 79;
SET j_np = j_day_no DIV 12053;
SET j_day_no = j_day_no % 12053;
SET jy = 979 + 33 * j_np + 4 * __mydiv(j_day_no, 1461);
SET j_day_no = j_day_no % 1461;
IF j_day_no >= 366 then
SET jy = jy + __mydiv(j_day_no - 1, 365);
SET j_day_no = (j_day_no - 1) % 365;
END IF;
SET i = 0;
WHILE (i < 11 and j_day_no >= _jdmarray(i)) do
SET j_day_no = j_day_no - _jdmarray(i);
SET i = i + 1;
END WHILE;
SET jm = i + 1;
SET jd = j_day_no + 1;
SET resout = CONCAT_WS ('-', jy, jm, jd);
IF (ttime <> '00:00:00') then
SET resout = CONCAT_WS(' ', resout, ttime);
END IF;
RETURN resout;
END;;
DELIMITER ;
-- ----------------------------
-- Function structure for `PMONTH`
-- ----------------------------
DROP FUNCTION IF EXISTS `PMONTH`;
DELIMITER ;;
CREATE FUNCTION `PMONTH`(`gdate` datetime) RETURNS char(100) CHARSET utf8
BEGIN
DECLARE
i,
gy, gm, gd,
g_day_no, j_day_no, j_np,
jy, jm, jd INT DEFAULT 0; /* Can be unsigned int? */
DECLARE resout char(100);
DECLARE ttime CHAR(20);
SET gy = YEAR(gdate) - 1600;
SET gm = MONTH(gdate) - 1;
SET gd = DAY(gdate) - 1;
SET ttime = TIME(gdate);
SET g_day_no = ((365 * gy) + __mydiv(gy + 3, 4) - __mydiv(gy + 99, 100) + __mydiv(gy + 399, 400));
SET i = 0;
WHILE (i < gm) do
SET g_day_no = g_day_no + _gdmarray(i);
SET i = i + 1;
END WHILE;
IF gm > 1 and ((gy % 4 = 0 and gy % 100 <> 0)) or gy % 400 = 0 THEN
SET g_day_no = g_day_no + 1;
END IF;
SET g_day_no = g_day_no + gd;
SET j_day_no = g_day_no - 79;
SET j_np = j_day_no DIV 12053;
set j_day_no = j_day_no % 12053;
SET jy = 979 + 33 * j_np + 4 * __mydiv(j_day_no, 1461);
SET j_day_no = j_day_no % 1461;
IF j_day_no >= 366 then
SET jy = jy + __mydiv(j_day_no - 1, 365);
SET j_day_no =(j_day_no - 1) % 365;
END IF;
SET i = 0;
WHILE (i < 11 and j_day_no >= _jdmarray(i)) do
SET j_day_no = j_day_no - _jdmarray(i);
SET i = i + 1;
END WHILE;
SET jm = i + 1;
SET jd = j_day_no + 1;
RETURN jm;
END;;
DELIMITER ;
-- ----------------------------
-- Function structure for `pmonthname`
-- ----------------------------
DROP FUNCTION IF EXISTS `pmonthname`;
DELIMITER ;;
CREATE FUNCTION `pmonthname`(`gdate` datetime) RETURNS varchar(100) CHARSET utf8
BEGIN
CASE PMONTH(gdate)
WHEN 1 THEN RETURN 'ÝÑæÑÏíä';
WHEN 2 THEN RETURN 'ÇÑÏíÈåÔÊ';
WHEN 3 THEN RETURN 'ÎÑÏÇÏ';
WHEN 4 THEN RETURN '撄';
WHEN 5 THEN RETURN 'ãÑÏÇÏ';
WHEN 6 THEN RETURN 'ÔåÑíæÑ';
WHEN 7 THEN RETURN 'ãåÑ';
WHEN 8 THEN RETURN 'ÂÈÇä';
WHEN 9 THEN RETURN 'ÂÐÑ';
WHEN 10 THEN RETURN 'Ïí';
WHEN 11 THEN RETURN 'Èåãä';
WHEN 12 THEN RETURN 'ÇÓÝäÏ';
END CASE;
END;;
DELIMITER ;
-- ----------------------------
-- Function structure for `pyear`
-- ----------------------------
DROP FUNCTION IF EXISTS `pyear`;
DELIMITER ;;
CREATE FUNCTION `pyear`(`gdate` datetime) RETURNS char(100) CHARSET utf8
BEGIN
DECLARE
i,
gy, gm, gd,
g_day_no, j_day_no, j_np,
jy, jm, jd INT DEFAULT 0; /* Can be unsigned int? */
DECLARE resout char(100);
DECLARE ttime CHAR(20);
SET gy = YEAR(gdate) - 1600;
SET gm = MONTH(gdate) - 1;
SET gd = DAY(gdate) - 1;
SET ttime = TIME(gdate);
SET g_day_no = ((365 * gy) + __mydiv(gy + 3, 4) - __mydiv(gy + 99, 100) + __mydiv(gy + 399, 400));
SET i = 0;
WHILE (i < gm) do
SET g_day_no = g_day_no + _gdmarray(i);
SET i = i + 1;
END WHILE;
IF gm > 1 and ((gy % 4 = 0 and gy % 100 <> 0)) or gy % 400 = 0 THEN
SET g_day_no = g_day_no + 1;
END IF;
SET g_day_no = g_day_no + gd;
SET j_day_no = g_day_no - 79;
SET j_np = j_day_no DIV 12053;
set j_day_no = j_day_no % 12053;
SET jy = 979 + 33 * j_np + 4 * __mydiv(j_day_no, 1461);
SET j_day_no = j_day_no % 1461;
IF j_day_no >= 366 then
SET jy = jy + __mydiv(j_day_no - 1, 365);
SET j_day_no = (j_day_no - 1) % 365;
END IF;
SET i = 0;
WHILE (i < 11 and j_day_no >= _jdmarray(i)) do
SET j_day_no = j_day_no - _jdmarray(i);
SET i = i + 1;
END WHILE;
SET jm = i + 1;
SET jd = j_day_no + 1;
RETURN jy;
END;;
DELIMITER ;
-- ----------------------------
-- Function structure for `pday`
-- ----------------------------
DROP FUNCTION IF EXISTS `pday`;
DELIMITER ;;
CREATE FUNCTION `pday`(`gdate` datetime) RETURNS char(100) CHARSET utf8
BEGIN
DECLARE
i,
gy, gm, gd,
g_day_no, j_day_no, j_np,
jy, jm, jd INT DEFAULT 0; /* Can be unsigned int? */
DECLARE resout char(100);
DECLARE ttime CHAR(20);
SET gy = YEAR(gdate) - 1600;
SET gm = MONTH(gdate) - 1;
SET gd = DAY(gdate) - 1;
SET ttime = TIME(gdate);
SET g_day_no = ((365 * gy) + __mydiv(gy + 3, 4) - __mydiv(gy + 99 , 100) + __mydiv(gy + 399, 400));
SET i = 0;
WHILE (i < gm) do
SET g_day_no = g_day_no + _gdmarray(i);
SET i = i + 1;
END WHILE;
IF gm > 1 and ((gy % 4 = 0 and gy % 100 <> 0)) or gy % 400 = 0 THEN
SET g_day_no = g_day_no + 1;
END IF;
SET g_day_no = g_day_no + gd;
SET j_day_no = g_day_no - 79;
SET j_np = j_day_no DIV 12053;
SET j_day_no = j_day_no % 12053;
SET jy = 979 + 33 * j_np + 4 * __mydiv(j_day_no, 1461);
SET j_day_no = j_day_no % 1461;
IF j_day_no >= 366 then
SET jy = jy + __mydiv(j_day_no - 1, 365);
SET j_day_no = (j_day_no-1) % 365;
END IF;
SET i = 0;
WHILE (i < 11 and j_day_no >= _jdmarray(i)) do
SET j_day_no = j_day_no - _jdmarray(i);
SET i = i + 1;
END WHILE;
SET jm = i + 1;
SET jd = j_day_no + 1;
RETURN jd;
END;;
DELIMITER ;
-- ----------------------------
-- Function structure for `_gdmarray2`
-- ----------------------------
DROP FUNCTION IF EXISTS `_gdmarray2`;
DELIMITER ;;
CREATE FUNCTION `_gdmarray2`(`m` smallint, `k` SMALLINT) RETURNS smallint(2)
BEGIN
CASE m
WHEN 0 THEN RETURN 31;
WHEN 1 THEN RETURN 28+k;
WHEN 2 THEN RETURN 31;
WHEN 3 THEN RETURN 30;
WHEN 4 THEN RETURN 31;
WHEN 5 THEN RETURN 30;
WHEN 6 THEN RETURN 31;
WHEN 7 THEN RETURN 31;
WHEN 8 THEN RETURN 30;
WHEN 9 THEN RETURN 31;
WHEN 10 THEN RETURN 30;
WHEN 11 THEN RETURN 31;
END CASE;
END;;
DELIMITER ;
-- ----------------------------
-- Function structure for `gdate`
-- ----------------------------
DROP FUNCTION IF EXISTS `gdate`;
DELIMITER ;;
CREATE FUNCTION `gdate`(`jy` smallint, `jm` smallint, `jd` smallint) RETURNS datetime
BEGIN
DECLARE
i, j, e, k, mo,
gy, gm, gd,
g_day_no, j_day_no, bkab, jmm, mday, g_day_mo, bkab1, j1
INT DEFAULT 0; /* Can be unsigned int? */
DECLARE resout char(100);
DECLARE fdate datetime;
SET bkab = __mymod(jy,33);
IF (bkab = 1 or bkab= 5 or bkab = 9 or bkab = 13 or bkab = 17 or bkab = 22 or bkab = 26 or bkab = 30) THEN
SET j=1;
end IF;
SET bkab1 = __mymod(jy+1,33);
IF (bkab1 = 1 or bkab1= 5 or bkab1 = 9 or bkab1 = 13 or bkab1 = 17 or bkab1 = 22 or bkab1 = 26 or bkab1 = 30) THEN
SET j1=1;
end IF;
CASE jm
WHEN 1 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 2 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 3 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 4 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 5 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 6 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 7 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 8 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 9 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 10 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 11 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 12 THEN IF jd > _jdmarray2(jm)+j or jd <= 0 THEN SET e=1; end IF;
END CASE;
IF jm > 12 or jm <= 0 THEN SET e=1; end IF;
IF jy <= 0 THEN SET e=1; end IF;
IF e>0 THEN
RETURN 0;
end IF;
IF (jm>=11) or (jm=10 and jd>=11 and j=0) or (jm=10 and jd>11 and j=1) THEN
SET i=1;
end IF;
SET gy = jy + 621 + i;
IF (__mymod(gy,4)=0) THEN
SET k=1;
end IF;
IF (__mymod(gy,100)=0) and (__mymod(gy,400)<>0) THEN
SET k=0;
END IF;
SET jmm=jm-1;
WHILE (jmm > 0) do
SET mday=mday+_jdmarray2(jmm);
SET jmm=jmm-1;
end WHILE;
SET j_day_no=(jy-1)*365+(__mydiv(jy,4))+mday+jd;
SET g_day_no=j_day_no+226899;
SET g_day_no=g_day_no-(__mydiv(gy-1,4));
SET g_day_mo=__mymod(g_day_no,365);
IF (k=1 and j=1) THEN
IF (g_day_mo=0) THEN
RETURN CONCAT_WS('-',gy,'12','30');
END IF;
IF (g_day_mo=1) THEN
RETURN CONCAT_WS('-',gy,'12','31');
END IF;
END IF;
IF (g_day_mo=0) THEN
RETURN CONCAT_WS('-',gy,'12','31');
END IF;
SET mo=0;
SET gm=gm+1;
while g_day_mo>_gdmarray2(mo,k) do
SET g_day_mo=g_day_mo-_gdmarray2(mo,k);
SET mo=mo+1;
SET gm=gm+1;
end WHILE;
SET gd=g_day_mo;
RETURN CONCAT_WS('-',gy,gm,gd);
END;;
DELIMITER ;
-- ----------------------------
-- Function structure for `gdatestr`
-- ----------------------------
DROP FUNCTION IF EXISTS `gdatestr`;
DELIMITER ;;
CREATE FUNCTION `gdatestr`(`jdat` char(10)) RETURNS datetime
BEGIN
DECLARE
i, j, e, k, mo,
gy, gm, gd,
g_day_no, j_day_no, bkab, jmm, mday, g_day_mo, jd, jy, jm,bkab1,j1
INT DEFAULT 0; /* ### Can't be unsigned int! ### */
DECLARE resout char(100);
DECLARE jdd, jyd, jmd, jt varchar(100);
DECLARE fdate datetime;
SET jdd = SUBSTRING_INDEX(jdat, '/', -1);
SET jt = SUBSTRING_INDEX(jdat, '/', 2);
SET jyd = SUBSTRING_INDEX(jt, '/', 1);
SET jmd = SUBSTRING_INDEX(jt, '/', -1);
SET jd = CAST(jdd as SIGNED);
SET jy = CAST(jyd as SIGNED);
SET jm = CAST(jmd as SIGNED);
SET bkab = __mymod(jy,33);
IF (bkab = 1 or bkab= 5 or bkab = 9 or bkab = 13 or bkab = 17 or bkab = 22 or bkab = 26 or bkab = 30) THEN
SET j=1;
end IF;
SET bkab1 = __mymod(jy+1,33);
IF (bkab1 = 1 or bkab1= 5 or bkab1 = 9 or bkab1 = 13 or bkab1 = 17 or bkab1 = 22 or bkab1 = 26 or bkab1 = 30) THEN
SET j1=1;
end IF;
CASE jm
WHEN 1 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 2 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 3 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 4 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 5 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 6 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 7 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 8 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 9 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 10 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 11 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 12 THEN IF jd > _jdmarray2(jm)+j or jd <= 0 THEN SET e=1; end IF;
END CASE;
IF jm > 12 or jm <= 0 THEN SET e=1; end IF;
IF jy <= 0 THEN SET e=1; end IF;
IF e>0 THEN
RETURN 0;
end IF;
IF (jm>=11) or (jm=10 and jd>=11 and j=0) or (jm=10 and jd>11 and j=1) THEN
SET i=1;
end IF;
SET gy = jy + 621 + i;
IF (__mymod(gy,4)=0) THEN
SET k=1;
end IF;
IF (__mymod(gy,100)=0) and (__mymod(gy,400)<>0) THEN
SET k=0;
END IF;
SET jmm=jm-1;
WHILE (jmm > 0) do
SET mday=mday+_jdmarray2(jmm);
SET jmm=jmm-1;
end WHILE;
SET j_day_no=(jy-1)*365+(__mydiv(jy,4))+mday+jd;
SET g_day_no=j_day_no+226899;
SET g_day_no=g_day_no-(__mydiv(gy-1,4));
SET g_day_mo=__mymod(g_day_no,365);
IF (k=1 and j=1) THEN
IF (g_day_mo=0) THEN
RETURN CONCAT_WS('-',gy,'12','30');
END IF;
IF (g_day_mo=1) THEN
RETURN CONCAT_WS('-',gy,'12','31');
END IF;
END IF;
IF (g_day_mo=0) THEN
RETURN CONCAT_WS('-',gy,'12','31');
END IF;
SET mo=0;
SET gm=gm+1;
while g_day_mo>_gdmarray2(mo,k) do
SET g_day_mo=g_day_mo-_gdmarray2(mo,k);
SET mo=mo+1;
SET gm=gm+1;
end WHILE;
SET gd=g_day_mo;
RETURN CONCAT_WS('-',gy,gm,gd);
END;;
DELIMITER ;
Then I receive the error:
#1227 - Access denied; you need the SUPER privilege for this operation
I've changed all database command such as select delete alter, ... to yes into my MySQL database manager. I am using directAdmin v1.41.1.
I would try to GRANT permissions to that user ID and client IP and see if that helps.
http://dev.mysql.com/doc/refman/5.0/en/grant.html

Error while using user defined functions MySql

Hi please help me to solve this problem thanks in advance
I defined these functions in database
CREATE FUNCTION levenshtein( s1 VARCHAR(255), s2 VARCHAR(255) )
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE s1_len, s2_len, i, j, c, c_temp, cost INT;
DECLARE s1_char CHAR;
-- max strlen=255
DECLARE cv0, cv1 VARBINARY(256);
SET s1_len = CHAR_LENGTH(s1), s2_len = CHAR_LENGTH(s2), cv1 = 0x00, j = 1, i = 1, c = 0;
IF s1 = s2 THEN
RETURN 0;
ELSEIF s1_len = 0 THEN
RETURN s2_len;
ELSEIF s2_len = 0 THEN
RETURN s1_len;
ELSE
WHILE j <= s2_len DO
SET cv1 = CONCAT(cv1, UNHEX(HEX(j))), j = j + 1;
END WHILE;
WHILE i <= s1_len DO
SET s1_char = SUBSTRING(s1, i, 1), c = i, cv0 = UNHEX(HEX(i)), j = 1;
WHILE j <= s2_len DO
SET c = c + 1;
IF s1_char = SUBSTRING(s2, j, 1) THEN
SET cost = 0; ELSE SET cost = 1;
END IF;
SET c_temp = CONV(HEX(SUBSTRING(cv1, j, 1)), 16, 10) + cost;
IF c > c_temp THEN SET c = c_temp; END IF;
SET c_temp = CONV(HEX(SUBSTRING(cv1, j+1, 1)), 16, 10) + 1;
IF c > c_temp THEN
SET c = c_temp;
END IF;
SET cv0 = CONCAT(cv0, UNHEX(HEX(c))), j = j + 1;
END WHILE;
SET cv1 = cv0, i = i + 1;
END WHILE;
END IF;
RETURN c;
END;
and helper function
CREATE FUNCTION levenshtein_ratio( s1 VARCHAR(255), s2 VARCHAR(255) )
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE s1_len, s2_len, max_len INT;
SET s1_len = LENGTH(s1), s2_len = LENGTH(s2);
IF s1_len > s2_len THEN
SET max_len = s1_len;
ELSE
SET max_len = s2_len;
END IF;
RETURN ROUND((1 - LEVENSHTEIN(s1, s2) / max_len) * 100);
END;
Now I'm trying to write query using these functions:
My Query:
SELECT cevap
FROM `chat`
WHERE levenshtein_ratio( soru,"Hello" ) >80
LIMIT 1 ;
My chat table has only two columns Varchar(250) "soru" and "cevap"
I get following error when I execute my query

Create test table in SQL Server 2008

How can I create a table with x attributes and n rows like?
For example:
x = 4 , and n = 10
att1 att2 att3 att4
----------------------
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
declare #x int = 4, #n int = 10
declare #strg nvarchar(1000) = 'create table myTable ('
declare #i int = 1
while #i<= #x
begin
set #strg = #strg + 'att' + cast(#i as varchar(5))+ ' int default ' + CAST(#i as varchar(5)) + ','
set #i = #i + 1
end
set #strg = SUBSTRING(#strg, 1, LEN(#strg)-1) + ')'
-- this creates your table
exec sp_executesql #strg
-- now lets insert rows
set #i = 0
while #i < #n
begin
INSERT INTO myTable DEFAULT VALUES;
set #i = #i + 1
end
-- lets check
select *
from myTable
Here's a script. You could put this into a stored procedure or just leave it as it is. All you have to do is update the initial col and row values.
DECLARE
#cols INT = 4,
#rows INT = 10,
#tablename VARCHAR(20) = 'TestTable'
DECLARE
#i INT = 1,
#j INT = 1,
#sql NVARCHAR(MAX) = 'create table ' + #tablename + '('
WHILE(#i <= #cols)
BEGIN
SET #sql = #sql + 'att' + CAST(#i AS VARCHAR(10)) + ' VARCHAR(10)'
IF NOT #i = #cols
BEGIN
SET #sql = #sql + ', '
END
SET #i = #i + 1
END
SET #sql = #sql + ')'
EXECUTE sp_executesql #sql
SET #sql = 'INSERT INTO ' + #tablename + ' VALUES'
WHILE(#j <= #rows)
BEGIN
SET #i = 1
SET #sql = #sql + '('
WHILE(#i <= #cols)
BEGIN
SET #sql = #sql + '''' + CAST(#i AS VARCHAR(10)) + ''''
IF NOT #i = #cols
BEGIN
SET #sql = #sql + ', '
END
SET #i = #i + 1
END
IF NOT #j = #rows
BEGIN
SET #sql = #sql + '), '
END
SET #j = #j + 1
END
SET #sql = #sql + ')'
EXECUTE sp_executesql #sql
Hope this helps.