Solved

How to insert the string 'NULL' using Replace

Posted on 2008-10-27
13
421 Views
Last Modified: 2012-06-27
In Visual Basic 6.0

I have a String:

Dim sVal as String

sVal = "call_proc '" & sParam1 & "','" & sParam2 & "','" & sParam3

each param is also a string passed to the function

some of the string parameters might be empty, which might produce a value like this if, for example, sParam2 was empty:

result of concatenation:

call_proc 'value1', '', 'value2'

so far so good.

I want to then replace any resulting ,'', with ,NULL, so that the output would read like this:

call_proc 'value1', NULL, 'value2'

and I tried doing so as follows:

    Dim sNew As String
    sNew = Replace(sVal, "''", "NULL")

I tested this using a dummy string:

    sNew = Replace(sVal, "''", "HELLO WORLD")

and the replace happened as expected. However, when trying to replace with the actual string that reads NULL, I end up with no conversion, and the result reads:

call_proc 'value1', '', 'value2

So how do I use VB replace() method and actually insert the string that reads NULL, that is, the word, not any kind of VB Constant?

thanks



0
Comment
Question by:PMH4514
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
13 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22815175
your code seems right, though
so, you say that sVal, after the replace line, does not have the right data?

Dim sVal as String
sVal = "call_proc '" & sParam1 & "','" & sParam2 & "','" & sParam3
sVal = replace(sVal , "''", "NULL")

Open in new window

0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 22815270
The only thing I see wrong with the code is that you have left off "'" at the end of the sVal assignment (and that way, if the third parameter is the NULL on, you don't get '' to replace).

Here's an example that is wording perfectly.  Create a test application and throw a commnad button and a Text box of the form and try the following code.
0
 

Author Comment

by:PMH4514
ID: 22815315
Yah I went and edited, incompletely, my post. That ending single quote is properly in place.  Sorry about that.

This is what I am seeing:

sParam1 = "one"
sParam2 = ""
sParam3 = "three"

Dim sVal as String
sVal = "call_proc '" & sParam1 & "','" & sParam2 & "','" & sParam3 & "'"
sVal = replace(sVal , "''", "NULL")

result:
sVal = "call_proc 'one', '', 'three'

however, if I do this instead:

sVal = replace(sVal , "''", "NULL2")

and all else is the same, I get:

sVal = "call_proc 'one', NULL2, 'three'

which is what I'd want, only NULL2 should read NULL






0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:PMH4514
ID: 22815423
if it matters, the parameters are coming across from an ASP page, where they are "" in the HTML form
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 22815647
I'm not experienced at getting data from ASP, but as a guess, perhaps you're getting some strange value that is perhaps functionally equivilent to a null character inside the single quotes and therefore the real string isn't two tick marks but two tick marks with "invisible" characters in between.

One suggestion that might work if the data coming back from ASP is effectively a NULL value should be the code snippet shown.

If the code snippet below doesn't work, try adding some debug code where you print out the ASC code number for every character in the string and see if there isn't something between the pair of ticks.
    sVal = "call_proc " & IIf(Len(sParam1), "'" & sParam1 & "'", "") & _
                          IIf(Len(sParam2), "'" & sParam2 & "'", "") & _
                          IIf(Len(sParam3), "'" & sParam3 & "'", "")

Open in new window

0
 

Author Comment

by:PMH4514
ID: 22815724
what is IIF ?
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 22815841
OK, so I'm showing examples from VB6.

IIF is a function where the 1st parameter is evaluated as true or false, if true, the 2nd parameter is returned, if false, the 3rd parameter is returned.  It's basically an IF as a function.

You can functionally do the same thing with three IF statements.
    sVal = "call_proc " 
    If Len(sParam1) then sVal = sVal & "'" & sParam1 & "'"
    If Len(sParam2) then sVal = sVal & "'" & sParam2 & "'"
    If Len(sParam3) then sVal = sVal & "'" & sParam3 & "'"

Open in new window

0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 22815924
Opps, forgot to deal with commas AND the fact you want either the parameter or the word NULL.

So let's try this one more time.
sVal = "call_proc "
 
If Len(sParam1) Then
    sVal = sVal & "'" & sParam1 & "',"
Else
    sVal = sVal & "NULL,"
End If
 
If Len(sParam2) Then
    sVal = sVal & "'" & sParam2 & "',"
Else
    sVal = sVal & "NULL,"
End If
 
If Len(sParam3) Then
    sVal = sVal & "'" & sParam3 & "'"
Else
    sVal = sVal & "NULL"
End If

Open in new window

0
 

Author Comment

by:PMH4514
ID: 22815937
When I include this:

IIf(Len(sParam1), "'" & sParam1 & "'", "test")

The word test gets replaced.

if instead
IIf(Len(sParam1), "'" & sParam1 & "'", "NULL")

the Word NULL gets replaced, however it is enclosed in single quotes

I want the word NULL with no single quotes around it.


0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 22815945
The shortened form using IIF() function would be as follows:
sVal = "call_proc " & IIf(Len(sParam1), "'" & sParam1 & "',", "NULL,") & _
                      IIf(Len(sParam2), "'" & sParam2 & "',", "NULL,") & _
                      IIf(Len(sParam3), "'" & sParam3 & "'", "NULL")

Open in new window

0
 

Author Comment

by:PMH4514
ID: 22816001
I'm using the shortened version and I end up with the word NULL enclosed in single quotes
0
 
LVL 16

Accepted Solution

by:
HooKooDooKu earned 125 total points
ID: 22816046
Make sure you are NOT using the Replace() function anymore, and only include the single quotes when appending sParam#.
The previous example I just did takes all of that, plus the need for commas, into account.
If a Parameter is NOT zero length, then the parameter is appended with single quotes.  If the Parameter IS zero length, NULL is appended withOUT single quotes.  And finally, for sParam1 and sParam2, a comma also gets appended.

The snippet below shows each piece of this broken out for you, but should be functionally the same as the previous example with the IIF to build the string in one long line of code.
sVal = "call_proc "
 
If Len(sParam1) Then
  sVal = sVal & "'" & sParam1 & "'"
Else
  sVal = sVal & "NULL"
End If
 
sVal = sVal & ","
 
If Len(sParam2) Then
  sVal = sVal & "'" & sParam2 & "'"
Else
  sVal = sVal & "NULL"
End If
 
sVal = sVal & ","
 
If Len(sParam3) Then
  sVal = sVal & "'" & sParam3 & "'"
Else
  sVal = sVal & "NULL"
End If

Open in new window

0
 

Author Comment

by:PMH4514
ID: 22816095
ok got it thanks! I forgot to pull the Replace() call
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month7 days, 1 hour left to enroll

623 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