Link to home
Start Free TrialLog in
Avatar of fdsafog56
fdsafog56

asked on

In Oracle it's DCODE(). What is in Sql Server ?

Hi,
I try to insert a row in a table which has date fields.I use Convert function like this:
CONVERT(DATETIME,'" & dateString & "',103).
The problem is that dateString variable may has an empty string-if the user don't choose a date. In that case I get in the DataBase the date :01/01/1900 .
I want that when the dateString is empty I'll get Null in that field on the Data Base and when the variable is not empty in the db will be the date in the variable.

In Oracle there is a function that does it - DCODE() function.
I need the same function but in Sql Server.

Thank's
Avatar of roshkm
roshkm

Are you talking abt  ISNULL( ) ?  :-)

Regards,
Rosh.K.M
ISNULL ( check_expression , replacement_value )

Cheers,
Rosh.K.M
CASE
  when ISNULL(datestring, '') = '' then ''
  else  CONVERT(DATETIME,'" & dateString & "',103)
 end

hope it helps
karin
SOLUTION
Avatar of KarinLoos
KarinLoos

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hilaire,
nuliff requires 2 expressions and returns null of the 2 expressions are equivalent
sorry hilaire got confused with the "
but tried it like this
select CONVERT(DATETIME, NULLIF('' + @date  + '', ''), 103)

my apologies
>>nullif requires 2 expressions and returns null of the 2 expressions are equivalent<<
agree, so what's the problem ?

so let's take NULLIF(@a, '')
if @a is null, it keeps null
if @a is empty string '', it returns null
in any other case it returns @a

just as required .... and faster than case ... when


@Karin
sorry our posts crossed ...
Avatar of fdsafog56

ASKER

Hi,

Thanks for your help.
I prefered Hilaire's answer because it's simple and faster.