Extract only numbers or characters from a set of characters (string)

Introduction

Hello, I hope that all of you guys doing well. Anyway let’s focus what i am trying to say? As a programmer frequently i need to do extract only numbers from an alphanumeric set of characters (string). Even sometimes need to do exactly the opposite for example extract only characters (string) from set of an alphanumeric characters (string). So i would like to share a very very simple but very efficient and strong way to do this..

Using the code

A sample code is given below:

 private String foo 
       (
             String p_inputValue
       )
       {
           string rectVal = string.Empty;

           for (int i = 0; i < p_inputValue.Length; i++)
           {
               // validating the current character is a number or not
               if (!(Char.IsDigit(p_inputValue[i])))
                   rectVal += p_inputValue[i];
           }

           return rectVal;
        
       } // end function foo

Input: A123B333C45D66E6F7GH

output: ABCDEFGH

 

if you want to do exactly the opposite you just need to change the “if” condition just like below:

 if (Char.IsDigit(p_inputValue[i]))

Input: A123B333C45D66E6F7GH

output:123333456667

History

  • 28th August, 2017: Initial post
Advertisements

A simple and another way to find rows on n’th maximum number from a table.

Introduction
It’s a very simple trick to get to get rows on n’th maximum number from a table. In this tip i used common table expression (CTE) and DENSE_RANK() function. To know more about CTE click this link and for DENSE_RANK() click this link.

Using the code
A simple code snippet is given below.
–START TO GET N’TH HIGHEST NUMBER/SALARY

DECLARE @g_query AS VARCHAR(256)
DECLARE @g_nth_highest AS INT
DECLARE @g_nth_row AS INT

SET @g_nth_height = 7;
SET @g_nth_row = 3;

SET @g_query =
‘WITH CTE_RESULT AS
(
SELECT ct_salary, DENSE_RANK() OVER
(
ORDER BY ct_salary DESC
) AS DENSERANK_RESULT FROM T_EMPLOYEE
)
SELECT TOP ‘+ CONVERT(VARCHAR, @g_nth_row) + ‘ ct_salary AS SALARY FROM CTE_RESULT WHERE CTE_RESULT.DENSERANK_RESULT =’ + CONVERT(VARCHAR, @g_nth_highest);

EXEC(@g_query)
— END
Conclusion
I hope you guys get the scenario and this might be helpful to you. Enjoy!

History
Saturday, December 22th, 2015: Initial post

A simple example of MS SQL Server Pad String

Introduction
An easy way to Pad String using MS SQL Server.

Using the code
A sample code snippet is given below:
//
CREATE FUNCTION [dbo].[FUN_pad_string]

(
@p_seq VARCHAR(16),
@p_pad_with CHAR(1),
@p_pad_length INT
)

RETURNS VARCHAR(16) AS

BEGIN

DECLARE @g_current_seq varchar(16)

SELECT @g_current_seq = ISNULL(REPLICATE(@p_pad_with, @p_pad_length – LEN(ISNULL(@p_seq ,0))), ”) + @p_seq

RETURN @g_current_seq

END
//
Conclusion

I hope you guys get the scenario and this might be helpful to you, enjoy.

History
Saturday, December 19, 2015: Initial post.

How to split MS SQL Server table row data

Introduction

An easy way to split MS SQL Server table row data.

Using the code

A sample code snippet is given below:

-- SELECT @tableColumnID, Data  FROM dbo.[FUN_split_row_data](@p_row_string_data,',')
CREATE FUNCTION [dbo].[FUN_split_row_data]
(
	@p_row_data NVARCHAR(2000),
	@p_split_on NVARCHAR(256)
)  
RETURNS @g_ret_value TABLE 
(
	id INT idENTITY(1,1),
	data nvarchar(100)
) 
AS  
BEGIN 
	DECLARE @g_counter INT
	SET @g_counter = 1

	WHILE (CHARINDEX(@p_split_on,@p_row_data)>0)
	BEGIN
		INSERT INTO @g_ret_value (data)
		SELECT data = LTRIM(RTRIM(SUBSTRING(@p_row_data,1,CHARINDEX(@p_split_on,@p_row_data)-1)))

		SET @p_row_data = SUBSTRING(@p_row_data,CHARINDEX(@p_split_on,@p_row_data)+1,LEN(@p_row_data))
		SET @g_counter = @g_counter + 1
	END
	
	INSERT INTO @g_ret_value (data)
	SELECT data = LTRIM(RTRIM(@p_row_data))

	RETURN

END

Input

SELECT  1234 AS id, data  FROM dbo.[FUN_split_row_data]('Hello,world,!!!,you are, most, welcome, at, codeproject.com',',')

Output

id    data

——————
314    Hello
314    world
314    !!!
314    you are
314    most
314    welcome
314    at
314    codeproject.com

Conclusion

I hope you guys get the scenario and this might be helpful to you, enjoy.

History

  • Saturday, December 19, 2015: Initial post.