[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 470
  • Last Modified:

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
0
fdsafog56
Asked:
fdsafog56
  • 4
  • 3
  • 2
  • +1
2 Solutions
 
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
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
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
 
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

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now