Chen Xie
Chen Xie

Reputation: 4281

javascript: How to judge if a field value is empty string or null based on database value

I have got one field column called 'id' in the Oracle database, the value of 'id' can be null or 2-space(i.e. " ") string. That field value is mapped by a third party application (Serena Business Manager). I am supposed to write a javascript to tell whether the field is null or with 2 spaces.

I tried so by the javascript statement:

if(id === '')
{
     doSomething()
}

This will allow those 'id's with a null value in the database pass. So here is the question: how to let string with spaces(not necessarily two) in the database pass a javascript 'if' statement?

Upvotes: 1

Views: 854

Answers (3)

Justin Cave
Justin Cave

Reputation: 231821

In Oracle, there is no difference between a NULL and an empty string. The empty string is NULL. You can't differentiate between the two in code because you can't differentiate between them in the database.

There is another thread on StackOverflow where we discuss why Oracle treats the empty string as NULL despite the fact that the ANSI standard specifies that the two should be treated differently.

Since it sounds like you're actually not looking for empty strings but for strings with two spaces

if(id === '' || id == '  ')
{
     doSomething()
}

Upvotes: 2

rlay3
rlay3

Reputation: 10552

if you want to do something when a string is null or empty:

if (!id || id === '') {
    doSomething()
}

try this jsfiddle

Upvotes: 1

Laoujin
Laoujin

Reputation: 10249

You could use NVL(id, 'this was really null') AS id. That would make your current JavaScript work.

How does your query look like? How does it get to JS? Lots of things could have happened to your null or "empty" strings before they got to the code you pasted...

PS: An 'id' database column that is either empty or null is kinda weird... :)

Upvotes: 0

Related Questions