Solved

T-SQL into C#

Posted on 2011-03-08
8
224 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

816 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now