Inserting Null Value in to Date Field - Oracle

Post questions here relative to DataStage Enterprise/PX Edition for such areas as Parallel job design, Parallel datasets, BuildOps, Wrappers, etc.

Moderators: chulett, rschirm, roy

Post Reply
allavivek
Premium Member
Premium Member
Posts: 211
Joined: Sat May 01, 2010 5:07 pm

Inserting Null Value in to Date Field - Oracle

Post by allavivek »

Hello,

We are using 8.7 Version, I am trying to insert null value in to a date field, in oracle,which was defined as nullable and default value as null in table.

The expression i am using is :

if Trim(lnk_tr.eff_dt) = "NULL" then SetNull() else StringtoDate(lnk_tr.eff_dt,"%yyyy-%mm-%dd")

String "NULL" is coming from input file. %yyyy-%mm-%dd is input format.

The error iam getting is :
The variable bInvalidDateTime has value 1 which is not valid in the current context.

I am sure the error is, the date value format is not valid.

But How to insert null value in to date field, which is defined as nullable in table?

Thank You
mac4rfree85
Participant
Posts: 126
Joined: Thu Jul 01, 2010 11:39 pm

Re: Inserting Null Value in to Date Field - Oracle

Post by mac4rfree85 »

I feel the error is coming from StringToDate function.. Just to confirm this, can you remove the if,,, then ..else and replace it with only "StringtoDate(lnk_tr.eff_dt,"%yyyy-%mm-%dd")" and run the job.
If the error exists, then you need to look into your input columns.
Mac4rfree
RPhani
Participant
Posts: 32
Joined: Sun Aug 26, 2012 7:03 am
Location: Hyd

Post by RPhani »

Hi,

IsValid() , If..Then..Else combination may work

----------------
Phani
ssnegi
Participant
Posts: 138
Joined: Thu Nov 15, 2007 4:17 am
Location: Sydney, Australia

Reply

Post by ssnegi »

I tested your statement with setnull() and it inserted a record into the table date field.
I got the error you are getting when I put '' instead of setnull().
So could be that some records in the file are empty instead of NULL.
Put this condition :
if Trim(lnk_tr.eff_dt) = "NULL" or Trim(lnk_tr.eff_dt) = '' then SetNull() else StringtoDate(lnk_tr.eff_dt,"%yyyy-%mm-%dd")
ssnegi
Participant
Posts: 138
Joined: Thu Nov 15, 2007 4:17 am
Location: Sydney, Australia

Post by ssnegi »

if IsValid('Date',StringToDate(DSLink.DT,"%yyyy-%mm-%dd"),"%yyyy-%mm-%dd") then DSLink.DT else SetNull()
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

ssnegi wrote:if IsValid('Date',StringToDate(DSLink.DT,"%yyyy-%mm-%dd"),"%yyyy-%mm-%dd") then DSLink.DT else SetNull()
Sorry but that's not valid "IsValid" syntax. You use it to test a string value before attempting to convert it.
-craig

"You can never have too many knives" -- Logan Nine Fingers
ssnegi
Participant
Posts: 138
Joined: Thu Nov 15, 2007 4:17 am
Location: Sydney, Australia

Post by ssnegi »

Code: Select all

if len(DSLink.DT) = 10 then if IsValid('Date',StringToDate(DSLink.DT,"%yyyy-%mm-%dd")) then StringToDate(DSLink3.DT,"%yyyy-%mm-%dd") else SetNull() else SetNull()
The earlier code gives a warning if the input string is less than length of date. So I modified the statement.
I tested this with valid (2014-12-31) and Invalid values (2014-13-32), NULL, '', ABCD,1234 and it worked correctly.
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

It's still invalid syntax and is not "working correctly", nor is there a need to check lengths. Sorry but this is pretty basic stuff, did you check the documentation for the function? Using that StringToDate function inside IsValid is the problem as it is far too early. Check validity, then convert only if it is valid:

Code: Select all

if IsValid("Date",DSLink.DT) then StringToDate(DSLink.DT,"%yyyy-%mm-%dd") else SetNull() 
The default format for date in the IsValid function is "%yyyy-%mm-%dd" so there's no need to use that format string here, use it when the date is in a non-default form. Or use it, doesn't hurt of course... just isn't needed.
-craig

"You can never have too many knives" -- Logan Nine Fingers
ssnegi
Participant
Posts: 138
Joined: Thu Nov 15, 2007 4:17 am
Location: Sydney, Australia

Post by ssnegi »

Code: Select all

if IsValid("Date",DSLink.DT) then StringToDate(DSLink.DT,"%yyyy-%mm-%dd") else SetNull()
The IsValid can check Date from a string. It works correctly on all types of Valid and Invalid strings. Thanks for that Chulett...
Post Reply