Solved

T-SQL into C#

Posted on 2011-03-08
8
225 Views
Last Modified: 2012-05-11
Need to convert the following T-SQL code into C#

ALTER FUNCTION [dbo].[charcheck]
(@hs1 nvarchar(100),@hs2  nvarchar(100))

RETURNS numeric(5,2)

AS
BEGIN
      declare @l1 as int,@l2 as int,@p1 int
      declare @r1 as nvarchar(100),@r2 as nvarchar(100)
            
      set @l1=len(@hs1)
      set @l2=len(@hs2)
      
      set @r1=@hs1
      set @r2=@hs2
      
      declare @i as int
      declare @rst as int
      set @i=1
        
      while @i <= @l2
      begin
                  set @p1=charindex(substring(@hs2,@i,1),@r1)
                  if @p1>0
                        set @r1=stuff(@r1,@p1,1,'')
            set @i=@i+1
      end
      
      set @rst=len(@r1)
      
      set @i=1
      while @i <= @l1
      begin
            set @p1=charindex(substring(@hs1,@i,1),@r2)
                  if @p1>0
                        set @r2=stuff(@r2,@p1,1,'')
            set @i=@i+1
      end
      set @rst=@rst+len(@r2)
      
      declare @retval as numeric(5,2)
      declare @som2 as numeric(5,2)
      
      
      set @som2=cast((@l1+@l2) as numeric(5,2))
      set @retval=((@l1+@l2-@rst)/@som2)*100
      return @Retval
end

Open in new window

0
Comment
Question by:EasyCode
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 1

Expert Comment

by:lalitgada
ID: 35067230
first tell me what you want to acheive through this code.
0
 

Author Comment

by:EasyCode
ID: 35067481
Check performance between C# and TSQL. This TSQL function does a lot of string-manipulation and that is a weak spot in TSQL compared to C#.

This functions matches 2 strings and returns value on a scale of 100. 100 means exact match.
0
 

Author Comment

by:EasyCode
ID: 35067805

Lalitgada,

This code is part of the complete TSQL function and does individual character matching between 2 strings.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:Gene_Cyp
ID: 35067971
Are you allowed to use LINQ (C# 3)?
0
 

Author Comment

by:EasyCode
ID: 35067976
Sure I'm
0
 
LVL 9

Expert Comment

by:gery128
ID: 35069467
Hello EasyCode:

Find C# code below to get started: Let me know the exact logic so I can post better code.
class Program
    {
        static void Main(string[] args)
        {
            Program pr = new Program();
            pr.GetValues("12", "123");
        }


        public decimal GetValues(string val1, string val2)
        {

            int l1, l2, p1,i=1,rst;
            string r1 = val1, r2 = val2, s1 = String.Empty, s2 = String.Empty;
            l1 = val1.Length; l2 = val2.Length;

            Char[]  myCharArray;

            while (i < l2)
            {
                p1 = Convert.ToInt32(r1.IndexOf(val2.Substring(i, 1)));
                if (p1 > 0)
                {
                    myCharArray = r1.ToCharArray();
                    myCharArray[p1] = ' ';
                    r1 = new string(myCharArray);
                }   
                i++;
            }
            foreach (char c in r1)
            {
                if (Convert.ToInt32(c) != 32)
                    s1 += c;
            }

            rst = s1.Length;
            i = 1;
            while (i < l1)
            {
                p1 = Convert.ToInt32(r2.IndexOf(val1.Substring(i, 1)));
                if (p1 > 0)
                {
                    myCharArray = r2.ToCharArray();
                    myCharArray[p1] = ' ';

                    r2 = new string(myCharArray);
                }                
                i++;
            }
            foreach (char c in r2)
            {
                if (Convert.ToInt32(c) != 32)
                    s2 += c;
            }

            rst = rst + s2.Length;

            decimal som2 = Convert.ToDecimal(l1 + l2);
            decimal retVal = ((l1 + l2 - rst) / som2) * 100;

            return retVal;
        }
    }

Open in new window

0
 
LVL 9

Accepted Solution

by:
gery128 earned 500 total points
ID: 35069924
Hello EasyCode,

Refer this code, I by mistake initialized counter variable i as 1 in last code. You can change following
lines and run again.

int l1, l2, p1,i=1,rst; with this line: int l1, l2, p1,i=0,rst;  and
i=1; with i=0;  

I have pasted full code for your reference.

using System;
using System.Collections.Generic;
using System.Text;

namespace SQLFunctionTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Program pr = new Program();
            pr.GetValues("12345", "123");
        }


        public decimal GetValues(string val1, string val2)
        {

            int l1, l2, p1,i=0,rst;
            string r1 = val1, r2 = val2, s1 = String.Empty, s2 = String.Empty;
            l1 = val1.Length; l2 = val2.Length;

            Char[]  myCharArray;

            while (i < l2)
            {
                p1 = Convert.ToInt32(r1.IndexOf(val2.Substring(i, 1)));
                if (p1 >= 0)
                {
                    myCharArray = r1.ToCharArray();
                    myCharArray[p1] = ' ';
                    r1 = new string(myCharArray);
                }   
                i++;
            }
            foreach (char c in r1)
            {
                if (Convert.ToInt32(c) != 32)
                    s1 += c;
            }

            rst = s1.Length;
            i = 0;
            while (i < l1)
            {
                p1 = Convert.ToInt32(r2.IndexOf(val1.Substring(i, 1)));
                if (p1 >= 0)
                {
                    myCharArray = r2.ToCharArray();
                    myCharArray[p1] = ' ';

                    r2 = new string(myCharArray);
                }                
                i++;
            }
            foreach (char c in r2)
            {
                if (Convert.ToInt32(c) != 32)
                    s2 += c;
            }

            rst = rst + s2.Length;

            decimal som2 = Convert.ToDecimal(l1 + l2);
            decimal retVal = ((l1 + l2 - rst) / som2) * 100;

            return retVal;
        }
    }
}

Open in new window

0
 

Author Comment

by:EasyCode
ID: 35107582
Thx for your code gery128
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question