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
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
ISNULL ( check_expression , replacement_value )
Cheers,
Rosh.K.M
Cheers,
Rosh.K.M
CASE
when ISNULL(datestring, '') = '' then ''
else CONVERT(DATETIME,'" & dateString & "',103)
end
hope it helps
karin
when ISNULL(datestring, '') = '' then ''
else CONVERT(DATETIME,'" & dateString & "',103)
end
hope it helps
karin
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
hilaire,
nuliff requires 2 expressions and returns null of the 2 expressions are equivalent
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
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
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 ...
sorry our posts crossed ...
ASKER
Hi,
Thanks for your help.
I prefered Hilaire's answer because it's simple and faster.
Thanks for your help.
I prefered Hilaire's answer because it's simple and faster.
Regards,
Rosh.K.M