2

My table will contain the following values. h1,h2,h3 are varchar fields with size 1

    **Register  Date        Year    Batch   h1      h2      h3**    
    1138M0321   02-08-2013  3       1       A       A       NULL
    1138M0323   02-08-2013  3       1       P       P       NULL
    1138M0324   02-08-2013  3       1       P       P       NULL    
    1138M0325   02-08-2013  3       1       P       P       NULL

I need to update one of these fields*(h1/h2/h3)* with NULL. But I can only add "" and not actually NULL How can I update the table with NULL?

6
  • Why can you only add ""? What is the rule, which rows and fields do you want to set to NULL? Commented Aug 2, 2013 at 7:50
  • depending on the condition I need to update H1 or H2 or H3. I can't make the value back to NULL which is the field's starting state when it doesn't hold any value Commented Aug 2, 2013 at 7:55
  • 1
    Depending on which condition, how should we show you the correct sql if we don't know the rule? What role plays the date field? Why can't you set the value back(?) to NULL? What have you tried, what was the result, what is the desired result? Commented Aug 2, 2013 at 7:57
  • 1
    Here is the rule.. WHERE year=" & year & " AND batch=" & batch & "" AND date= '02-08-2013' I tried DBNull.Value but still it doesn't place NULL Commented Aug 2, 2013 at 8:00
  • 1
    Please show the code that you're trying to use. Commented Aug 2, 2013 at 8:01

6 Answers 6

4

The NULL value must be SQL, not a .NET value, so instead of trying things like:

"... SET h1 = " & NULL & " ... "

Simply use this in the query:

"... SET h1 = NULL ... "

Note that:

Null values cannot be used for information that is required to distinguish one row in a table from another row in a table, such as primary keys.

See MSDN Documentation.

Sign up to request clarification or add additional context in comments.

Comments

2

Why use VB Nulls at all ...

UPDATE student_attendance_table SET h1 = NULL WHERE...

Comments

1
UPDATE student_attendance_table SET h3 = 'NULL' WHERE...

This will make your table look like this:

**Register  Date        Year    Batch   h1      h2      h3**    
    1138M0321   02-08-2013  3       1       A       A       'NULL'
    1138M0323   02-08-2013  3       1       P       P       'NULL'
    1138M0324   02-08-2013  3       1       P       P       'NULL'
    1138M0325   02-08-2013  3       1       P       P       'NULL'

And when you want to display the values of column h3. You can trim the single quotes and display NULL.

Comments

0

If you do not have a NOT NULL constraint

"UPDATE student_attendance_table SET h1 = " & System.DBNull.Value & " WHERE...

2 Comments

How can I do this in vb.net there is no NULL when I try like this "UPDATE student_attendance_table SET h1 = " & NULL & " WHERE... there is only Nullable and DBNull If I use DBNull it's showing error
Can't you use System.DBNull.Value?
0

In case you want to stick to using NULL then it must be included within statement itself without any parameter, use like the classical way:

"UPDATE student_attendance_table SET h1 = NULL WHERE...

Comments

0
"Update Table Set Column = (NULL As Column Type) where Condition"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.