1

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?

Chen Xie
  • 3,849
  • 8
  • 27
  • 46
  • 1
    Possible duplicate: http://stackoverflow.com/questions/5515310/is-there-a-standard-function-to-check-for-null-undefined-or-blank-variables-in – Lews Therin Oct 09 '12 at 22:46

3 Answers3

2

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()
}
Community
  • 1
  • 1
Justin Cave
  • 227,342
  • 24
  • 367
  • 384
  • Whoah... I kinda forgot about that when writing my answer... +1 :) – Laoujin Oct 09 '12 at 22:51
  • Not sure if it's the case. I am using Oracle SQL developer to view the data, if the 'id' is null, I can explicitly see there is a "(null)" in that grid. If the 'id' is not null, I can see nothing in that grid, which is different with the "(null)" case. – Chen Xie Oct 09 '12 at 23:15
  • @ChenXie - Then you have neither a NULL nor the empty string stored in the column. My guess is that you have a string with one or more spaces in it. What does `length(id)` and `dump(id)` return? If it is consistently returning a length of 1 and `dump` indicates that the value stored in the column is a decimal 32, then you're looking for a string that has a single space character (i.e. ' '). – Justin Cave Oct 09 '12 at 23:23
  • @JustinCave I think you are right, I made a false assumption of equaling "invisibility" and "empty". I checked the length of the "invisible" ids and they give me a consistent answer of 2. – Chen Xie Oct 09 '12 at 23:34
  • @ChenXie - OK, and what does `dump` show you? Are there two 32's in each row? If so, then you're probably looking at a string with two spaces (' '). – Justin Cave Oct 09 '12 at 23:46
  • @JustinCave correct. The problem has been solved, and thanks for the help – Chen Xie Oct 10 '12 at 22:51
1

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

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

try this jsfiddle

rlay3
  • 10,002
  • 6
  • 30
  • 22
  • `doSomething` is also executed when id is an empty string like this. Empty strings are _falsy_ in JS. – Laoujin Oct 09 '12 at 22:49
0

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... :)

Laoujin
  • 9,962
  • 7
  • 42
  • 69