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
fdsafog56Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

roshkmCommented:
Are you talking abt  ISNULL( ) ?  :-)

Regards,
Rosh.K.M
0
roshkmCommented:
ISNULL ( check_expression , replacement_value )

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

hope it helps
karin
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

KarinLoosCommented:
oops sorry you wanted to return a null when datestring was empty
so
( CASE
  when ISNULL(datestring, '') = '' then NULL
  else  CONVERT(DATETIME,'" & dateString & "',103)
 end )  as somefieldname
0
HilaireCommented:
instead of
CONVERT(DATETIME,'" & dateString & "',103)

use
CONVERT(DATETIME, NULLIF('" & dateString & "', ''), 103)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KarinLoosCommented:
hilaire,
nuliff requires 2 expressions and returns null of the 2 expressions are equivalent
0
KarinLoosCommented:
sorry hilaire got confused with the "
but tried it like this
select CONVERT(DATETIME, NULLIF('' + @date  + '', ''), 103)

my apologies
0
HilaireCommented:
>>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


0
HilaireCommented:
@Karin
sorry our posts crossed ...
0
fdsafog56Author Commented:
Hi,

Thanks for your help.
I prefered Hilaire's answer because it's simple and faster.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.