Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

URLEncode

By Robert Cary,

Call this function as you would any scalar UDF:

select dbo.URLEncode('K8%/fwO3L mEQ*.}')

This script requires a numbers tabel and assumes the following schema:

IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Numbers')
BEGIN

     CREATE TABLE dbo.Numbers
    (
        Num INT NOT NULL 
        CONSTRAINT [PKC__Numbers__Num] PRIMARY KEY CLUSTERED (Num) on [PRIMARY]
    )
    ;WITH Nbrs_3( n ) AS ( SELECT 1 UNION SELECT 0 ),
          Nbrs_2( n ) AS ( SELECT 1 FROM Nbrs_3 n1 CROSS JOIN Nbrs_3 n2 ),
          Nbrs_1( n ) AS ( SELECT 1 FROM Nbrs_2 n1 CROSS JOIN Nbrs_2 n2 ),
          Nbrs_0( n ) AS ( SELECT 1 FROM Nbrs_1 n1 CROSS JOIN Nbrs_1 n2 ),
          Nbrs  ( n ) AS ( SELECT 1 FROM Nbrs_0 n1 CROSS JOIN Nbrs_0 n2 )
     
    INSERT INTO dbo.Numbers(Num)
    SELECT n
    FROM ( SELECT ROW_NUMBER() OVER (ORDER BY n)
          FROM Nbrs ) D ( n )
         WHERE n <= 50000 ;
END

Numbers tables are an invaluable addition to your toolset. To quote Adam Machanic:

Numbers tables are truly invaluable. I use them all of the time for string manipulation, simulating window functions, populating test tables with lots of data, eliminating cursor logic, and many other tasks that would be incredibly difficult without them.

Is using a table of numbers a hack, as I've seen some people claim? No. Show me another way to efficiently do all of the things a numbers table can. Does it waste space? No. The script below will use up around 900 KB of disk space in each database. That's absolutely nothing. You'll end up getting millions, maybe billions of times the disk space investment back in terms of ease of development and time saved. http://sqlblog.com/blogs/adam_machanic/archive/2006/07/12/you-require-a-numbers-table.aspx

Enjoy!

Total article views: 3172 | Views in the last 30 days: 21
 
Related Articles
FORUM

Cross Joins

Avoiding Cross Joins

FORUM

Difference Between cross apply and cross join

Difference Between cross apply and cross join

FORUM

cross tab

create a cross from result....

FORUM

Finding duplicate values in very large table. Select Row_number() over too slow

Our developers have created a nightly process that uses select row_number() over and does a cross jo...

FORUM

Select just three octets from IP number

Select just three octets from IP number

Tags
encode    
tools    
t-sql    
url    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones