5

Does anyone know how to set a null value into a datetime column in a database in sql? I tried setting the String as null: String date = null; but i would get this error:

java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]Conversion failed when converting datetime from character string.

Also I tried setting my string to no value by simply declaring it (String date;). however this would just end up setting the date as January 1, 1900.

I used 3 dropdown boxes for the user of the program to choose the month, day, and year each.

DateNew newdateprocessed;
int b, c;
String d;

//First I took the values from each drop down box.
b = Date_Processed_Month_Mod.getSelectedIndex(); 
c = Date_Processed_Day_Mod.getSelectedIndex();
d = Date_Processed_Year_Mod.getSelectedItem().toString();                
newdateprocessed = new DateNew(b, c, d);

//Then I used these values in a separate function to put them all together as just one      string
public class DateNew extends Object {
String newdate;
String monthword, newdateword;
int check=0;

public DateNew (int month, int day, String year) {

    //newdate = ""+ month + "/" + day + "/" + year;

    if (month == 0 | day == 0 | year.equals("Year")) {
        newdate=null;
        check = 1;
    }

    else {

        switch (month) {
            case 1: 
                monthword="January";
                break;
            case 2:
                monthword="February";
                break;
            case 3:
                monthword="March";
                break;
            case 4:
                monthword="April";
                break;
            case 5:
                monthword="May";
                break;
            case 6:
                monthword="June";
                break;
            case 7:
                monthword="July"; 
                break;
            case 8:
                monthword="August";
                break;
            case 9:
                monthword="September";
                break;
            case 10:
                monthword="October"; 
                break;
            case 11:
                monthword="November";
                break;
            case 12:
                monthword="December";
                break;

        }

        newdateword= monthword + " " + day + ", " + year; 
        newdate = ""+ month + "/" + day + "/" + year;
    }
    check=month;



}

public String newdate() {
    return newdate;
}

public int newdatecheck(){
    return check;
}

public String newdateword(){
    return newdateword;


}

Lastly, I would give newdateprocessed.newdate() to the column in my database. It would work fine if the user would choose a date, but if they did not choose a date that I would get an error, which is why I am trying to find a way to be able to put a null value.

2
  • Have you checked whether there is a not null constraint on the date column? Commented Jan 19, 2012 at 14:46
  • yes, i set the column to allow null values Commented Jan 19, 2012 at 14:52

4 Answers 4

22

Try to call

setNull(index,java.sql.Types.DATE)

For example

String query = "UPDATE Licenses Set Date_Processed = ?";
PreparedStatement queryStatement = (PreparedStatement)connection.prepareStatement(sql);
queryStatement.setNull(1, java.sql.Types.Date);
Sign up to request clarification or add additional context in comments.

1 Comment

perhaps it's changed, but the sqlType parameter should now be java.sql.Types.DATE
3

Why a string? If it's a datetime column, you could try:

Date date = null;

You seem to be passing a string to your data access code, which is being stored in a datetime column, so you are relying on the DBMS to interpret the string value and convert it to a datetime. There is no way this can handle a null value, so you are going to have to change the way you are doing this to actually pass a compatible data type like a Date instance.

4 Comments

i just edited my post to show more of my code. thank you, is there a certain format for a Date variable?
I've expanded on my answer as a result.
thank you, i will try to study the use of the Date as i have not used it before.
This is not strictly an answer, should have been a comment on the original question.
1

Try using the setDate method (and passing in null) instead of the setString method.

1 Comment

setDate with a null value passed through does not work for all JDBC drivers. the spec compliant way is to call setNull(index,Types.Date)
0

i figured out what to do. the query that i used for updating my database was originally this:

String query = "UPDATE Licenses Set Date_Processed = '" + newdateprocessed.newdate() + "'";

i took out the single quotation marks in the query and changed the query to this:

String query = "UPDATE Licenses Set Date_Processed =" + newdateprocessed.newdate();

and in my code for setting the value of newdate in my DateNew function, if it was null i set it to be:

newdate = "NULL";

and if it wasn't i set it to be:

newdate = "'" + month + "/" + day + "/" + year + "'";

1 Comment

This is a very bad "solution" the correct way is to use the ? parameter holder and call setNull on the prepared statement as indicated by @narek.gevorgyan

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.