Organizational Research By

Surprising Reserch Topic

oracle considers empty strings to be null while sql server does not how is thi

oracle considers empty strings to be null while sql server does not how is thi  using -'sql-server,oracle'

I have to write a component that re-creates SQL Server tables (structure and data) in an Oracle database.  This component also has to take new data entered into the Oracle database and copy it back into SQL Server.

Translating the data types from SQL Server to Oracle is not a problem.  However, a critical difference between Oracle and SQL Server is causing a major headache.  SQL Server considers a blank string ("") to be different from a NULL value, so a char column can be defined as NOT NULL and yet still include blank strings in the data.

Oracle considers a blank string to be the same as a NULL value, so if a char column is defined as NOT NULL, you cannot insert a blank string.  This is causing my component to break whenever a NOT NULL char column contains a blank string in the original SQL Server data.

So far my solution has been to not use NOT NULL in any of my mirror Oracle table definitions, but I need a more robust solution.  This has to be a code solution, so the answer can't be "use so-and-so's SQL2Oracle product".

How would you solve this problem?

Edit:  here is the only solution I've come up with so far, and it may help to illustrate the problem.  Because Oracle doesn't allow "" in a NOT NULL column, my component could intercept any such value coming from SQL Server and replace it with "@" (just for example).

When I add a new record to my Oracle table, my code has to write "@" if I really want to insert a "", and when my code copies the new row back to SQL Server, it has to intercept the "@" and instead write "".

I'm hoping there's a more elegant way.

Edit 2:  Is it possible that there's a simpler solution, like some setting in Oracle that gets it to treat blank strings the same as all the other major database?  And would this setting also be available in Oracle Lite?

asked Oct 7, 2015 by vijaygupta1980
0 votes

Related Hot Questions

6 Answers

0 votes

I don't see an easy solution for this.

Maybe you can store your values as one or more blanks -> ' ', which aren't NULLS in Oracle, or keep track of this special case through extra fields/tables, and an adapter layer.

answered Oct 7, 2015 by sujata naik
0 votes

My typical solution would be to add a constraint in SQL Server forcing all string values in the affected columns to have a length greater than 0:

CREATE TABLE Example (StringColumn VARCHAR(10) NOT NULL)

ADD CONSTRAINT CK_Example_StringColumn CHECK (LEN(StringColumn) > 0)

However, as you have stated, you have no control over the SQL Database. As such you really have four choices (as I see it):

  1. Treat empty string values as invalid, skip those records, alert an operator and log the records in some manner that makes it easy to manually correct / re-enter.
  2. Convert empty string values to spaces.
  3. Convert empty string values to a code (i.e. "LEGACY" or "EMPTY").
  4. Rollback transfers that encounter empty string values in these columns, then put pressure on the SQL Server database owner to correct their data.

Number four would be my preference, but isn't always possible. The action you take will really depend on what the oracle users need. Ultimately, if nothing can be done about the SQL database, I would explain the issue to the oracle business system owners, explain the options and consequences and make them make the decision :)

NOTE: I believe in this case SQL Server actually exhibits the "correct" behaviour.

answered Oct 7, 2015 by mannar kande
0 votes

Its nasty and could have unexpected side effects.. but you could just insert "chr(0)" rather than ''.

drop table x

drop table x succeeded.
create table x ( id number, my_varchar varchar2(10))

create table succeeded.
insert into x values (1, chr(0))

1 rows inserted
insert into x values (2, null)

1 rows inserted
select id,length(my_varchar) from x

ID                     LENGTH(MY_VARCHAR)     
---------------------- ---------------------- 
1                      1                      

2 rows selected

select * from x where my_varchar is not null

ID                     MY_VARCHAR 
---------------------- ---------- 
answered Oct 7, 2015 by mannar kande
0 votes

Do you have to permit empty strings in the SQL Server system? If you can add a constraint to the SQL Server system that disallows empty strings, that is probably the easiest solution.

answered Oct 7, 2015 by deepak gupta
0 votes

NOT NULL is a database constraint used to stop putting invalid data into your database. This is not serving any purpose in your Oracle database and so I would not have it.

I think you should just continue to allow NULLS in any Oracle column that mirrors a SqlServer column that is known to contain empty strings.

If there is a logical difference in the SqlServer database between NULL and empty string, then you would need something extra to model this difference in Oracle.

answered Oct 7, 2015 by patelnikul321
0 votes

I'd go with an additional column on the oracle side. Have your column allow nulls and have a second column that identifies whether the SQL-Server side should get a null-value or empty-string for the row.

answered Oct 7, 2015 by deven.bendale