Click here to Skip to main content
15,885,754 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

Am trying to Convert C# Function into sql server stored procedure
i have a c# code, i want to convert sql stored procedure
i have tried many ways but i couldn't get the out put.

below is my c# code:

protected void Page_Load(object sender, EventArgs e)
  {
      string S = "pass";
      string str2 = "";
      int num2 = 1;
      int num3 = S.Length;
      for (int i = 0; i < num3; i++)
      {
          str2 = str2 + Convert.ToChar(Convert.ToChar(S.Substring(i, 1)) + ((i + 1) * num2));
          num2 *= -1;
      }
      return str2;

  }




My Output for the above one is =
q_vo



So what i want is how to convert the above c# code into sql stored procedure.

if you know please drop a solution for this.

Thanks in advance,

What I have tried:

how to convert c# code into sql server stored procedure?
Posted
Updated 6-May-18 11:33am
Comments
Tomas Takac 18-Jan-17 3:15am    
Looks like homework to me.

In this case it can be done as its just string manipulation. Main challenge is the conversion of text to ascii code and back. SQL 2008 has following functions that can convert character to equivalent ascii code
MSDN: ASCII (Transact-SQL)
MSDN: CHAR (Transact-SQL)

You can use the guts of this snippet to create the stored procedure
SQL
DECLARE @input VARCHAR(50)
DECLARE @output VARCHAR(50)
DECLARE @length INT
DECLARE @i INT
DECLARE @tmp INT
DECLARE @num2 INT

SET @input = 'pass'

-- set defaults for output and internal variables
SELECT @output = '', @i = 1, @num2=1, @length = LEN(@input)

-- made slight change in formula as indexes in sql string arrays start at 1
WHILE @i <= @length
BEGIN
	SET @tmp = ascii(SUBSTRING(@input,@i,1))
	SET @tmp = @tmp + (@i*@num2)
	SET @num2 = @num2 * -1
	SET @output = @output + char(@tmp)

	SET @i = @i+1
end

SELECT @output


if you need to know how to create a stored procedure you can look at this:
CREATE PROCEDURE (Transact-SQL)
 
Share this answer
 
Comments
Maciej Los 18-Jan-17 16:06pm    
5!
I would not recommend converting C# code directly if it does not have data dependency & converting it directly can also lead to unknown trouble. I guess SQL supports CLR (depending on version you are using it), Article here should be able to help you on that. But again its tradeoff with performance.
 
Share this answer
 
class ValidateSAID {

public int check(String id) {
id = id.Trim();
if (!Regex.IsMatch(id, @"[0-9]+")) {
return -1;
}
if (id.Length != 10) {
return -1;
}
int type = (int)(id[0]-'0');
if (type != 2 && type != 1) {
return -1;
}
int sum = 0;
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
String ZFOdd = ((int)(id[i]-'0') * 2).ToString().PadLeft(2, '0');
sum += (int)(ZFOdd[0]-'0') + (int)(ZFOdd[1]-'0');
} else {
sum += (int)(id[i]-'0');
}
}
return (sum % 10 != 0) ? -1 : type;

}

}
 
Share this answer
 
Comments
Dave Kreskowiak 6-May-18 18:58pm    
Asked an answered over a year ago.
CHill60 7-May-18 7:19am    
This is C#. OP wanted SQL. Completely off-topic

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