Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 19th, 2007, 04:02 AM
Authorized User
 
Join Date: Sep 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Repalce Function :Urgent help

I have a table that looks like this

Table test:
Name num
tony 100,25
Dav 200.300
Emma 100 2,5.35

Then I have this statement:
Insert into Test2
select [name],Replace(Replace(Replace([num], ',', ''), '.',''), ' ', '')
From Test

The above code replaces those non- alphabetical charaters. My question is, is there any other way I can write the query to replace the non-Alphabetical characters like(!@#$%^&*()_+=-\|><,.?/)etc without nesting the Replace function.

Urgent help required.

 
Old February 19th, 2007, 06:21 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

What does the value num column mean? Why is that you have numbers with other special characters like , and . used there? Why is that you want to replace it now?

How about explaining us on that?

Cheers

_________________________
- Vijay G
Strive for Perfection
 
Old February 19th, 2007, 09:10 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

[Num] is the column name that Emeka is trying to "clean".

Emeka... this and similar functions will work. This one uses a "Tally" table to keep life simple. There are other functions that are somewhat faster, but you'll like this...

This creates the "Tally" table...
Code:
---------------------------------------------------------------------
--===== Create a tally table of numbers
 SELECT TOP 9999 IDENTITY(INT,1,1) AS N
   INTO dbo.Tally
   FROM dbo.SYSCOLUMNS sc1,
        dbo.SYSCOLUMNS sc2

--===== Give the tally table a clustered primary key for speed
  ALTER TABLE dbo.Tally
        ADD CONSTRAINT PK_Tally_N PRIMARY KEY CLUSTERED (N)

--===== Grant SELECT privs to the public
  GRANT SELECT ON dbo.Tally TO PUBLIC
GO
---------------------------------------------------------------------
This creates the function you'll need...
Code:
---------------------------------------------------------------------
--===== Create a function to clean telephone and other numbers
 CREATE FUNCTION dbo.CleanNumber (@StringNumber VARCHAR(8000))
RETURNS VARCHAR(8000)
     AS
  BEGIN
        --===== Declare local variables
        DECLARE @NewString VARCHAR(8000)
            SET @NewString = ''

        --===== Strip out ALL non-digit characters
         SELECT @NewString = @NewString + SUBSTRING(@StringNumber,N,1)
           FROM dbo.Tally WITH (NOLOCK)
          WHERE N <= LEN(@StringNumber)
            AND SUBSTRING(@StringNumber,N,1) LIKE '%[0-9]%'
          ORDER BY N

        --===== Return the cleaned string (NULL if no numbers present)
         RETURN NULLIF(@NewString,'')
    END
GO
--===== Grant EXECUTE privs to the public
  GRANT EXECUTE ON dbo.CleanNumber TO PUBLIC
GO
---------------------------------------------------------------------
And this demonstrates the funtion with some test data that I put into a test table...
Code:
---------------------------------------------------------------------
--===== If the temporary test table exists, drop it
     IF OBJECT_ID('TempDB..#MyHead') IS NOT NULL
        DROP TABLE #MyHead

--===== Create a test table full of mixed numbers
SELECT '+1-310-414-0909' AS MixedNumber
  INTO #MyHead UNION ALL
SELECT 'k3j4j5h6h7h5kj4h4khk343&%@^)#*(&$%#%$!@12' UNION ALL
SELECT '^(%$%)(DHFHSDFHOD)3(&)(F^)YFGFYP(DFY_(DYFYF' UNION ALL
SELECT '1234567890' UNION ALL
SELECT 'ABCDEF' 

--===== Demo the function
 SELECT MixedNumber AS OriginalNumber,
        dbo.CleanNumber(MixedNumber) AS CleanedNumber
   FROM #MyHead
---------------------------------------------------------------------
If you don't like the "Tally" table, you can do something similar with a loop. Either way works well...

--Jeff Moden
 
Old February 20th, 2007, 03:44 AM
Authorized User
 
Join Date: Sep 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Jeff.
 The code works perfectly.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Split function using ASp *Urgent* deean Classic ASP XML 5 June 20th, 2008 05:42 PM
Urgent:How to call the external function ivanlaw Javascript 6 October 16th, 2007 12:16 AM
XSLT Function very urgent alapati.sasi XSLT 3 May 23rd, 2007 03:45 AM
sql count function - urgent and pls help miki SQL Language 1 June 14th, 2006 09:47 AM
sql count function - pls help and urgent miki MySQL 1 May 12th, 2004 05:55 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.