1

I use jdbc to retrieve data from SQL Server, and ANSI_NULLS is off . So if I run

select * from cj_log where evt = null

I can get the result.

But when I put it in a statement like this

Statement st = DBConnection.getConnection().createStatement();
String sql = "select * from CJ_LOG where EVT=null";
ResultSet rs = st.executeQuery(sql);

The result set is empty. What is the problem here?

2
  • 2
    What happens when you use select * from CJ_LOG where EVT IS NULL ? Commented Mar 2, 2011 at 23:07
  • 1
    ANSI_NULLS is a per-connection setting. If DBConnection.getConnection() is not ensuring that ANSI_NULLS is OFF, it is likely that ANSI_NULLS is in fact ON (the default). Commented Mar 3, 2011 at 2:50

1 Answer 1

2

You need to say

where EVT is null

Nothing is ever equal to null, even null.

Think of it like this: Null means "don't know".

You are asking "is something I don't know equal to something I don't know"

The answer is "I don't know". So you don't get any rows.

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

3 Comments

While you are absolutely right in terms of the SQL standard, if, as the question indicated, the SQL Server is actually running with ANSI_NULLs off, select * from CJ_LOG where EVT = NULL is actually equivalent to select * from CJ_LOG where EVT IS NULL. See here.
Point. Bottom line is "turn ANSI_NULLS on and leave it on". :-)
Sorry for the late response. Thank you all for the help.

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.