Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

T-SQL into C#

Posted on 2011-03-08
8
Medium Priority
?
227 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
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
Independent Software Vendors: 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

972 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