How to write NULL to a Sql table

User avatar
Rick Lipkin
Posts: 2677
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA
Been thanked: 2 times

How to write NULL to a Sql table

Post by Rick Lipkin »

To All

How do I write NULL to a numeric or a nvarchar field on a Sql Server table .. when I do this :

oRsFac:Fields("permnum"):Value := if(i->permnum = 0, '', i->permnum )

"permnum" is a nvarchar(255) and i->permnum is numeric 10,0 .. The if() statement writes a zero to the SQL table and not the '' or NULL value ..

Same problem when I test for a char field and want to write NULL to the SQL table ..

Any Ideas ??

Thanks
Rick Lipkin
User avatar
Armando
Posts: 3279
Joined: Fri Oct 07, 2005 8:20 pm
Location: Toluca, México
Been thanked: 4 times
Contact:

Re: How to write NULL to a Sql table

Post by Armando »

Rick:

A silly question

Do you have defined the field to accept NULL?

Regards
SOI, s.a. de c.v.
estbucarm@gmail.com
http://www.soisa.mex.tl/
http://sqlcmd.blogspot.com/
Tel. (722) 174 44 45
Carpe diem quam minimum credula postero
User avatar
Armando
Posts: 3279
Joined: Fri Oct 07, 2005 8:20 pm
Location: Toluca, México
Been thanked: 4 times
Contact:

Re: How to write NULL to a Sql table

Post by Armando »

Rick:

With this sample I have no problem

Code: Select all | Expand


LOCAL nValor    := 10.50
// The field "UNI_USU" is VARCHAR(10) NOT NULL Type
oRsUni:Fields("UNI_USU"):Value := IIF(nValor = 0.00,'',nValor)
oRsUni:UpDate()
 


Neither with this an other example I have no problem neither

Code: Select all | Expand


LOCAL nValor    := 10.50
oRsUni:Fields("UNI_USU"):Value := IIF(nValor > 0.00,'',nValor)
oRsUni:UpDate()
 


Regards
SOI, s.a. de c.v.
estbucarm@gmail.com
http://www.soisa.mex.tl/
http://sqlcmd.blogspot.com/
Tel. (722) 174 44 45
Carpe diem quam minimum credula postero
User avatar
Rick Lipkin
Posts: 2677
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA
Been thanked: 2 times

Re: How to write NULL to a Sql table

Post by Rick Lipkin »

To All

YES .. all the fields are marked to accept 'null' .. for some reason on SQL Server the in line if() does not seem to want to accept '' as null ( for me ) ..

I have in the mean time used a regular if statement and only append a field if it is NOT null .. leaving the field NULL where it needs to be .. a lot of extra code :(

Rick Lipkin
User avatar
Enrico Maria Giordano
Posts: 8770
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Has thanked: 1 time
Been thanked: 7 times
Contact:

Re: How to write NULL to a Sql table

Post by Enrico Maria Giordano »

Rick Lipkin wrote:oRsFac:Fields("permnum"):Value := if(i->permnum = 0, '', i->permnum )


Try

Code: Select all | Expand

oRsFac:Fields("permnum"):Value   := if(i->permnum = 0, , i->permnum )


EMG
User avatar
gkuhnert
Posts: 274
Joined: Fri Apr 04, 2008 1:25 pm
Location: Aachen - Germany // Kerkrade - Netherlands
Contact:

Re: How to write NULL to a Sql table

Post by gkuhnert »

Rick,

as SQL Statement you could write something like

Code: Select all | Expand

UPDATE fac SET permnum = null WHERE permnum = 0


On the recordset it might then look like:

Code: Select all | Expand

oRsFac:Fields("permnum"):Value := if(i->permnum = 0, nil, i->permnum )
   or
oRsFac:Fields("permnum"):Value := if(i->permnum = 0, null, i->permnum )
 

but I don't know if it works on the recordset
Best Regards,

Gilbert Kuhnert
CTO Software GmbH
http://www.ctosoftware.de
User avatar
Rick Lipkin
Posts: 2677
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA
Been thanked: 2 times

Re: How to write NULL to a Sql table

Post by Rick Lipkin »

To All

oRsRel:Fields("qualified_ind"):Value := if(i->qualify = " ", , )
oRsRel:Fields("qualified_ind"):Value := if(i->qualify = " ", nil ,nil )

Both expressions should return NULL and what happends in MS Sql server the field is just appends blank spaces ..

Rick Lipkin
User avatar
Enrico Maria Giordano
Posts: 8770
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Has thanked: 1 time
Been thanked: 7 times
Contact:

Re: How to write NULL to a Sql table

Post by Enrico Maria Giordano »

Works fine here using latest xHarbour from CVS.

EMG
User avatar
Rick Lipkin
Posts: 2677
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA
Been thanked: 2 times

Re: How to write NULL to a Sql table

Post by Rick Lipkin »

Enrico

Using FWH 811 and the xHarbour build 1.1.0 rev 6195 .. the database is MS Sql Server .. not any other sql flavor.

Rick Lipkin
User avatar
Enrico Maria Giordano
Posts: 8770
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Has thanked: 1 time
Been thanked: 7 times
Contact:

Re: How to write NULL to a Sql table

Post by Enrico Maria Giordano »

Perhaps you should try with latest xHarbour from CVS.

EMG
User avatar
nageswaragunupudi
Posts: 10733
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 11 times
Contact:

Re: How to write NULL to a Sql table

Post by nageswaragunupudi »

The solution I found long time back and working for me for assigning NULL to fields or parameter values.

Code: Select all | Expand


#xtranslate NULL =>   VTWrapper( 1, nil )
...
...
// usage:
oRs:Fields(n):Value := NULL
oCmd:Parameters( n ):Value := NULL
 

I have not checked with the latest version of xHarbour, if assigning NIL has the same effect. If this works then its a lot better.
Regards

G. N. Rao.
Hyderabad, India
User avatar
Rick Lipkin
Posts: 2677
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA
Been thanked: 2 times

Re: How to write NULL to a Sql table

Post by Rick Lipkin »

Rao

YES ..

#xtranslate NULL => VTWrapper( 1, nil )

Was the answer !!

Thanks
Rick Lipkin
User avatar
Enrico Maria Giordano
Posts: 8770
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Has thanked: 1 time
Been thanked: 7 times
Contact:

Re: How to write NULL to a Sql table

Post by Enrico Maria Giordano »

It seems you are using an old version of win32ole.prg.

EMG
User avatar
Rick Lipkin
Posts: 2677
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA
Been thanked: 2 times

Re: How to write NULL to a Sql table

Post by Rick Lipkin »

Enrico

Using the official binaries from FTDN for xHarbour 409 .. not linking in anything else .. and it appears the FTDN version has a newer build revision than xHarbour.org :(

Rick
Post Reply