|
| Author |
Message |
Karl E. Peterson
Joined: 04 Oct 2007 Posts: 4836
|
Posted: Tue Oct 10, 2006 7:07 pm Post subject: Coerce Null to Zero? |
|
|
Just wondering... When working with database fields, it's simple to coerce
a Null to String by concatenating an empty string. IOW:
MyField & ""
Is there a similar technique for coercing it to Zero with numeric fields? I
swear I remember knowing of one at one time, but ...? All I can think of
is:
Val(MyField & "")
Sure seems klunky.
Thanks!
--
Working without a .NET?
http://classicvb.org/
Archived from group: microsoft>public>vb>syntax |
|
| Back to top |
|
 |
"Rick Rothstein \
Joined: 04 Oct 2007 Posts: 1584
|
Posted: Tue Oct 10, 2006 10:33 pm Post subject: Re: Coerce Null to Zero? |
|
|
> Just wondering... When working with database fields, it's simple to
> coerce
> a Null to String by concatenating an empty string. IOW:
>
> MyField & ""
>
> Is there a similar technique for coercing it to Zero with numeric fields?
> I
> swear I remember knowing of one at one time, but ...? All I can think of
> is:
>
> Val(MyField & "")
>
> Sure seems klunky.
No more so than this...
Format(MyField, "0;;;0")
Rick |
|
| Back to top |
|
 |
Bob Butler
Joined: 04 Oct 2007 Posts: 1325
|
Posted: Tue Oct 10, 2006 7:36 pm Post subject: Re: Coerce Null to Zero? |
|
|
"Karl E. Peterson" wrote in message@TK2MSFTNGP05.phx.gbl
> Just wondering... When working with database fields, it's simple to
> coerce a Null to String by concatenating an empty string. IOW:
>
> MyField & ""
>
> Is there a similar technique for coercing it to Zero with numeric
> fields? I swear I remember knowing of one at one time, but ...? All
> I can think of is:
>
> Val(MyField & "")
I think that's it unless you want to use IIF; at least the Val works for
numerics -- for date types I don't think I've ever found a way other than an
IsNull test of some sort
--
Reply to the group so all can participate
VB.Net: "Fool me once..." |
|
| Back to top |
|
 |
Karl E. Peterson
Joined: 04 Oct 2007 Posts: 4836
|
Posted: Tue Oct 10, 2006 7:47 pm Post subject: Re: Coerce Null to Zero? |
|
|
Bob Butler wrote:
> "Karl E. Peterson" wrote ...
>> Just wondering... When working with database fields, it's simple to
>> coerce a Null to String by concatenating an empty string. IOW:
>>
>> MyField & ""
>>
>> Is there a similar technique for coercing it to Zero with numeric
>> fields? I swear I remember knowing of one at one time, but ...? All
>> I can think of is:
>>
>> Val(MyField & "")
>
> I think that's it unless you want to use IIF; at least the Val works
> for numerics --
Okay, just wanted to be sure I wasn't forgetting some arcane slickness.
> for date types I don't think I've ever found a way
> other than an IsNull test of some sort
Hmmmm, store them as Doubles, then do a variation on above?
Thanks... Karl
--
Working without a .NET?
http://classicvb.org/ |
|
| Back to top |
|
 |
Karl E. Peterson
Joined: 04 Oct 2007 Posts: 4836
|
Posted: Tue Oct 10, 2006 7:46 pm Post subject: Re: Coerce Null to Zero? |
|
|
Rick Rothstein (MVP - VB) wrote:
>> Just wondering... When working with database fields, it's simple to
>> coerce
>> a Null to String by concatenating an empty string. IOW:
>>
>> MyField & ""
>>
>> Is there a similar technique for coercing it to Zero with numeric
>> fields? I
>> swear I remember knowing of one at one time, but ...? All I can
>> think of is:
>>
>> Val(MyField & "")
>>
>> Sure seems klunky.
>
> No more so than this...
>
> Format(MyField, "0;;;0")
Heh, true that! Especially since you can't even do *math* with that one,
yet.
--
Working without a .NET?
http://classicvb.org/ |
|
| Back to top |
|
 |
Bob Butler
Joined: 04 Oct 2007 Posts: 1325
|
Posted: Tue Oct 10, 2006 7:58 pm Post subject: Re: Coerce Null to Zero? |
|
|
"Karl E. Peterson" wrote in message@TK2MSFTNGP02.phx.gbl
> Bob Butler wrote:
>> "Karl E. Peterson" wrote ...
>>> Just wondering... When working with database fields, it's simple to
>>> coerce a Null to String by concatenating an empty string. IOW:
>>>
>>> MyField & ""
>>>
>>> Is there a similar technique for coercing it to Zero with numeric
>>> fields? I swear I remember knowing of one at one time, but ...?
>>> All I can think of is:
>>>
>>> Val(MyField & "")
>>
>> I think that's it unless you want to use IIF; at least the Val works
>> for numerics --
>
> Okay, just wanted to be sure I wasn't forgetting some arcane
> slickness.
>
>> for date types I don't think I've ever found a way
>> other than an IsNull test of some sort
>
> Hmmmm, store them as Doubles, then do a variation on above?
I'd do VB.Net first!
you could always coerce the value in the query...
case when myfield is null then 0 else myfield end as myfieldnz
--
Reply to the group so all can participate
VB.Net: "Fool me once..." |
|
| Back to top |
|
 |
Bob O`Bob
Joined: 04 Oct 2007 Posts: 1456
|
Posted: Tue Oct 10, 2006 8:26 pm Post subject: Re: Coerce Null to Zero? |
|
|
Karl E. Peterson wrote:
> Just wondering... When working with database fields, it's simple to coerce
> a Null to String by concatenating an empty string. IOW:
>
> MyField & ""
>
> Is there a similar technique for coercing it to Zero with numeric fields? I
> swear I remember knowing of one at one time, but ...? All I can think of
> is:
>
> Val(MyField & "")
>
> Sure seems klunky.
>
> Thanks!
I just add zero.
Bob |
|
| Back to top |
|
 |
Karl E. Peterson
Joined: 04 Oct 2007 Posts: 4836
|
Posted: Tue Oct 10, 2006 8:37 pm Post subject: Re: Coerce Null to Zero? |
|
|
Bob O`Bob wrote:
> Karl E. Peterson wrote:
>> Just wondering... When working with database fields, it's simple to
>> coerce a Null to String by concatenating an empty string. IOW:
>>
>> MyField & ""
>>
>> Is there a similar technique for coercing it to Zero with numeric
>> fields? I swear I remember knowing of one at one time, but ...?
>> All I can think of is:
>>
>> Val(MyField & "")
>>
>> Sure seems klunky.
>>
>> Thanks!
>
> I just add zero.
?null+0
Null
Confused...?
--
Working without a .NET?
http://classicvb.org/ |
|
| Back to top |
|
 |
Dmitriy Antonov
Joined: 04 Oct 2007 Posts: 431
|
Posted: Wed Oct 11, 2006 1:07 am Post subject: Re: Coerce Null to Zero? |
|
|
"Karl E. Peterson" wrote in message @TK2MSFTNGP05.phx.gbl...
> Just wondering... When working with database fields, it's simple to
> coerce
> a Null to String by concatenating an empty string. IOW:
>
> MyField & ""
>
> Is there a similar technique for coercing it to Zero with numeric fields?
> I
> swear I remember knowing of one at one time, but ...? All I can think of
> is:
>
> Val(MyField & "")
>
> Sure seems klunky.
>
> Thanks!
> --
> Working without a .NET?
> http://classicvb.org/
>
>
(Null And 0)
Try
?5 + (Null And 0)
5
But I generally prefer to use generic function. Something like this (got
idea from MS Access):
public function nz(Value as Variant, optional ValueIfNull as
Variant=vbNullString) as Variant
if isnull(Value) then nz=ValueIfNull Else nz=Value
end function
So any of the following should work with numeric (integer) value:
lRes = nz(Null, 0)
lRes = Val(nz(Null))
I prefer an empty string to zero as a default value, because it is easier to
convert to zero from an empty string then vice versa.
Should be adjusted if objects can be considered, but, generally, no need for
this.
Dmitriy. |
|
| Back to top |
|
 |
Karl E. Peterson
Joined: 04 Oct 2007 Posts: 4836
|
Posted: Tue Oct 10, 2006 10:19 pm Post subject: Re: Coerce Null to Zero? |
|
|
Dmitriy Antonov wrote:
> "Karl E. Peterson" wrote in message
> @TK2MSFTNGP05.phx.gbl...
>> Just wondering... When working with database fields, it's simple to
>> coerce
>> a Null to String by concatenating an empty string. IOW:
>>
>> MyField & ""
>>
>> Is there a similar technique for coercing it to Zero with numeric
>> fields? I
>> swear I remember knowing of one at one time, but ...? All I can
>> think of is:
>>
>> Val(MyField & "")
>>
>> Sure seems klunky.
>
> (Null And 0)
>
>
> Try
> ?5 + (Null And 0)
> 5
Yeah, that's the idea! Only problem is, if the field isn't Null, it fails.
Say, the field holds 12:
?5+(12 And 0)
5
Ideally, I'd want that to return 17. IOW, just consider the field at its
"face value" if not Null, and consider it 0 if it is Null.
> But I generally prefer to use generic function. Something like this
> (got idea from MS Access):
>
> public function nz(Value as Variant, optional ValueIfNull as
> Variant=vbNullString) as Variant
> if isnull(Value) then nz=ValueIfNull Else nz=Value
> end function
>
> So any of the following should work with numeric (integer) value:
> lRes = nz(Null, 0)
> lRes = Val(nz(Null))
>
> I prefer an empty string to zero as a default value, because it is
> easier to convert to zero from an empty string then vice versa.
>
> Should be adjusted if objects can be considered, but, generally, no
> need for this.
That's another thought, yeah. Might have to go that way, if the idea of
Val() becomes sufficiently disquieting. Thanks!
--
Working without a .NET?
http://classicvb.org/ |
|
| Back to top |
|
 |
Dmitriy Antonov
Joined: 04 Oct 2007 Posts: 431
|
Posted: Wed Oct 11, 2006 1:23 am Post subject: Re: Coerce Null to Zero? |
|
|
"Dmitriy Antonov" wrote in message %23iJGN7GHA.2384@TK2MSFTNGP04.phx.gbl...
>
> "Karl E. Peterson" wrote in message
> @TK2MSFTNGP05.phx.gbl...
>> Just wondering... When working with database fields, it's simple to
>> coerce
>> a Null to String by concatenating an empty string. IOW:
>>
>> MyField & ""
>>
>> Is there a similar technique for coercing it to Zero with numeric fields?
>> I
>> swear I remember knowing of one at one time, but ...? All I can think of
>> is:
>>
>> Val(MyField & "")
>>
>> Sure seems klunky.
>>
>> Thanks!
>> --
>> Working without a .NET?
>> http://classicvb.org/
>>
>>
>
> (Null And 0)
>
>
> Try
> ?5 + (Null And 0)
> 5
>
>
> But I generally prefer to use generic function. Something like this (got
> idea from MS Access):
>
> public function nz(Value as Variant, optional ValueIfNull as
> Variant=vbNullString) as Variant
> if isnull(Value) then nz=ValueIfNull Else nz=Value
> end function
>
> So any of the following should work with numeric (integer) value:
> lRes = nz(Null, 0)
> lRes = Val(nz(Null))
>
> I prefer an empty string to zero as a default value, because it is easier
> to convert to zero from an empty string then vice versa.
>
> Should be adjusted if objects can be considered, but, generally, no need
> for this.
>
> Dmitriy.
>
>
I probably had a bad day today.
Please disregard the first part - of course ANDing a variable with zero is
not going to work (and doing it with a constant does work but doesn't make
any sense). So I would stick to just a function.
Dmitriy. |
|
| Back to top |
|
 |
Bob O`Bob
Joined: 04 Oct 2007 Posts: 1456
|
Posted: Wed Oct 11, 2006 4:52 am Post subject: Re: Coerce Null to Zero? |
|
|
Karl E. Peterson wrote:
> Bob O`Bob wrote:
>> Karl E. Peterson wrote:
>>> Just wondering... When working with database fields, it's simple to
>>> coerce a Null to String by concatenating an empty string. IOW:
>>>
>>> MyField & ""
>>>
>>> Is there a similar technique for coercing it to Zero with numeric
>>> fields? I swear I remember knowing of one at one time, but ...?
>>> All I can think of is:
>>>
>>> Val(MyField & "")
>>>
>>> Sure seems klunky.
>>>
>>> Thanks!
>> I just add zero.
>
> ?null+0
> Null
>
> Confused...?
I don't think I am.
I never tried to /use/ such values directly, butI always assign them to a numeric variable
numericvar = field.value + 0
It's been quite a while, but as I recall, it always worked.
Bob
-- |
|
| Back to top |
|
 |
"Rick Rothstein \
Joined: 04 Oct 2007 Posts: 1584
|
Posted: Wed Oct 11, 2006 7:59 am Post subject: Re: Coerce Null to Zero? |
|
|
>>> I just add zero.
>>
>> ?null+0
>> Null
>>
>> Confused...?
>
>
> I don't think I am.
> I never tried to /use/ such values directly, butI always assign them to a
> numeric variable
>
> numericvar = field.value + 0
>
> It's been quite a while, but as I recall, it always worked.
The following
Dim IntVal As Integer
IntVal = Null + 0
errors out on the 2nd statement with Run-time error '94', "Invalid use of
Null"
Rick |
|
| Back to top |
|
 |
"Rick Rothstein \
Joined: 04 Oct 2007 Posts: 1584
|
Posted: Wed Oct 11, 2006 8:04 am Post subject: Re: Coerce Null to Zero? |
|
|
>>>> I just add zero.
>>>
>>> ?null+0
>>> Null
>>>
>>> Confused...?
>>
>> I don't think I am.
>> I never tried to /use/ such values directly, butI always assign them to a
>> numeric variable
>>
>> numericvar = field.value + 0
>>
>> It's been quite a while, but as I recall, it always worked.
>
> The following
>
> Dim IntVal As Integer
> IntVal = Null + 0
>
> errors out on the 2nd statement with Run-time error '94', "Invalid use of
> Null"
However, this does seem to work (maybe it is what you meant)...
Dim IntVal As Integer
IntVal = "0" & Field.Value
Debug.Print CStr(IntVal)
Rick |
|
| Back to top |
|
 |
Bob Butler
Joined: 04 Oct 2007 Posts: 1325
|
Posted: Wed Oct 11, 2006 9:44 am Post subject: Re: Coerce Null to Zero? |
|
|
"Rick Rothstein (MVP - VB)" wrote in
message @TK2MSFTNGP05.phx.gbl
> However, this does seem to work (maybe it is what you meant)...
>
> Dim IntVal As Integer
> IntVal = "0" & Field.Value
> Debug.Print CStr(IntVal)
Try that where Field.Value is a negative number
IntVal = "0-1" ' type mismatch
--
Reply to the group so all can participate
VB.Net: "Fool me once..."
|
|
| Back to top |
|
 |
|