Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Inserting Null Value
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Inserting Null Value

 Post Reply Post Reply Page  12>
Author
Awangku View Drop Down
Groupie
Groupie


Joined: 15 November 2001
Location: Malaysia
Status: Offline
Points: 70
Post Options Post Options   Thanks (0) Thanks(0)   Quote Awangku Quote  Post ReplyReply Direct Link To This Post Topic: Inserting Null Value
    Posted: 04 July 2003 at 3:25am

I have this condition as part of my codes. The idea is to save a Null value if strXXX is blank.

strXXX = Request.QueryString("XXX")
If len(strXXX) <= 1 Then strXXX = Null

(or should it be If strXXX = "" Then strXXX = Null?)

strSQL = "INSERT INTO tblXXX (ColumnX) VALUES ('"& strXXX &"')"

The problem is.. instead of Null, blank was inserted into ColumnX. If strXXX is a date, instead of Null, 01-01-1900 was inserted. I know it has something to to with those inverted commas in '"& strXXX &"' but I have no idea how to fix it. Any help? Thanks heaps.

 

Back to Top
Bunce View Drop Down
Senior Member
Senior Member
Avatar

Joined: 10 April 2002
Location: Australia
Status: Offline
Points: 846
Post Options Post Options   Thanks (0) Thanks(0)   Quote Bunce Quote  Post ReplyReply Direct Link To This Post Posted: 04 July 2003 at 8:00am

First question.

Whay would you want to enter a NULL into a field in a database?  NULL means 'undefined' or 'not known'.

What is wrong with an empty string field? Most of the time, NULLS are better avoided and alternatives preferred.

There may be legit reasons for this cause, although I can't think of any at the moment.

Perhaps if you tell us the details of what you are trying to accomplish, we might be able to provide a better solution.

Cheers,
Andrew

There have been many, many posts made throughout the world...
This was one of them.
Back to Top
Awangku View Drop Down
Groupie
Groupie


Joined: 15 November 2001
Location: Malaysia
Status: Offline
Points: 70
Post Options Post Options   Thanks (0) Thanks(0)   Quote Awangku Quote  Post ReplyReply Direct Link To This Post Posted: 06 July 2003 at 6:53pm

I'm capturing some information from users using a form. Some fields are not compulsory and may be left blank, so I thought instead of leaving them empty, it's better to save them as Nulls in the database.

As you can see, I'm rather new to this database thing, so I'm still experimenting on which one is better and which one is not. And thanks to you, I'm learning more than I expect.

I don't have any issue in saving empty string fields in my table, but how do you avoid the date "01-01-1900" from being inserted when the field is empty?

 

Back to Top
Bunce View Drop Down
Senior Member
Senior Member
Avatar

Joined: 10 April 2002
Location: Australia
Status: Offline
Points: 846
Post Options Post Options   Thanks (0) Thanks(0)   Quote Bunce Quote  Post ReplyReply Direct Link To This Post Posted: 06 July 2003 at 10:18pm

Ok. 

In your database, make sure 'Allow Nulls' option is selected. (Might be something else depending on the database - which database are you using).

If you are adding new records and you want a field to contain <NULL>, then you can simply leave it out of your INSERT statement.

If you want to UPDATE an existing record so as to record <NULL> in a field then we'll have to work out something else..

What database are you using again?

Cheers,
Andrew

There have been many, many posts made throughout the world...
This was one of them.
Back to Top
Awangku View Drop Down
Groupie
Groupie


Joined: 15 November 2001
Location: Malaysia
Status: Offline
Points: 70
Post Options Post Options   Thanks (0) Thanks(0)   Quote Awangku Quote  Post ReplyReply Direct Link To This Post Posted: 07 July 2003 at 5:30am

I'm running SQL2000.

And I have already set the required columns to Allow Nulls.

Thing is, I can't leave those fields out of my INSERT statement since I don't know whether they'd be blanks in the first place. It all depends on the user input. If they don't enter anything, then it's Null.

So.. what's the correct syntax for my INSERT statement above?

Back to Top
Bunce View Drop Down
Senior Member
Senior Member
Avatar

Joined: 10 April 2002
Location: Australia
Status: Offline
Points: 846
Post Options Post Options   Thanks (0) Thanks(0)   Quote Bunce Quote  Post ReplyReply Direct Link To This Post Posted: 07 July 2003 at 6:16am

ah ok.

Its just NULL without the quotes. So you'll need to test to see if the user entered anything before constructing your SQL statement, to avoid adding the quotes.  Assume that your form value has been transferred to the variable: theValue.

If len([TheValue]) > 0 then
   strField2 = "'" & theValue & "'"
Else
   strField2 = "NULL"
End If

strSQL = "INSERT INTO myTable (Field1, Field2, Field3)"
strSQL = strSQL & " VALUES ('" & Value1 & "',"
strSQL = strSQL & strField2
strSQL = strSQL & ",'" & Value3 & "')"
conn.execute (strSQL)

So basically, the quotes are only added to the variable strField2 if a value has been entered into that form field.

Note that you could also just leave that field out using a similar method..

Hope this helps,
Andrew



Edited by Bunce
There have been many, many posts made throughout the world...
This was one of them.
Back to Top
Awangku View Drop Down
Groupie
Groupie


Joined: 15 November 2001
Location: Malaysia
Status: Offline
Points: 70
Post Options Post Options   Thanks (0) Thanks(0)   Quote Awangku Quote  Post ReplyReply Direct Link To This Post Posted: 07 July 2003 at 9:02pm
Well.. I followed the code exactly but somehow it doesn't work. Instead of inserting NULL, I got blank value. And in the case of date, I got 01-01-1900. When I tried to insert a proper date, I got this message: "Syntax error converting datetime from character string"
Back to Top
Bunce View Drop Down
Senior Member
Senior Member
Avatar

Joined: 10 April 2002
Location: Australia
Status: Offline
Points: 846
Post Options Post Options   Thanks (0) Thanks(0)   Quote Bunce Quote  Post ReplyReply Direct Link To This Post Posted: 07 July 2003 at 10:11pm

place this code before conn.execute(strSQL)

response.write strSQL
response.end

and tell us what is sent to the screen in both cases.

There have been many, many posts made throughout the world...
This was one of them.
Back to Top
 Post Reply Post Reply Page  12>

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.