Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 UDF to URLEncode String

Author  Topic 

GTIMANiac
Starting Member

4 Posts

Posted - 2004-01-23 : 12:33:28
Has some one written a UDF to URLEncode a string? I have a web-based app and would like to have the data ready without any processing in my front end.

TIA
-Scott

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2004-01-23 : 12:59:16
Can you explain a case where you would need to do this.
Go to Top of Page

GTIMANiac
Starting Member

4 Posts

Posted - 2004-01-23 : 13:06:18
I can.

The application pulls a URL from the db. It may contain characters that do not comply with RFC 1738.

I would just rather have it encoded prior to having a PHP, ColdFusion or ASP front end do it.
Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2004-01-23 : 14:05:45
Here's a copy of an VB6 URLEncode Function.
You might nbe able to follow this and port it to a UDF.


Public Function URLEncode(strData As String) As String

Dim I As Integer
Dim strTemp As String
Dim strChar As String
Dim strOut As String
Dim intAsc As Integer

strTemp = Trim(strData)
For I = 1 To Len(strTemp)
strChar = Mid(strTemp, I, 1)
intAsc = Asc(strChar)
If (intAsc >= 48 And intAsc <= 57) Or _
(intAsc >= 97 And intAsc <= 122) Or _
(intAsc >= 65 And intAsc <= 90) Then
strOut = strOut & strChar
Else
strOut = strOut & "%" & Hex(intAsc)
End If
Next I

URLEncode = strOut

End Function



Michael

<Yoda>Use the Search page you must. Find the answer you will.</Yoda>
Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2004-01-23 : 15:44:54
Got a Decode to post??
Go to Top of Page

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2004-01-23 : 15:58:28
URLDecode
Public Function URLDecode(sEncodedURL As String) As String
On Error Goto Catch
Dim iLoop As Integer
Dim sRtn As String
Dim sTmp As String


If Len(sEncodedURL) > 0 Then
' Loop through each char


For iLoop = 1 To Len(sEncodedURL)
sTmp = Mid(sEncodedURL, iLoop, 1)
sTmp = Replace(sTmp, "+", " ")
' If char is % then get next two chars
' and convert from HEX to decimal


If sTmp = "%" Then
sTmp = Mid(sEncodedURL, iLoop + 1, 2)
sTmp = Chr(CDec("&H" & sTmp))
' Increment loop by 2
iLoop = iLoop + 2
End If
sRtn = sRtn & sTmp
Next iLoop
URLDecode = sRtn
End If
Finally:
Exit Function
Catch:
URLDecode = ""
Resume Finally
End Function
Go to Top of Page
   

- Advertisement -