string concadination in sql query
-
Hi, My query -- parameter of the procedure @abc int = null; declare @txt varchar(100); set @txt = 'select * from tablename where field1 = isnull(@fld,field1)' set @txt = replace(@txt,'@fld',@abc) exec(@txt) when @abc has any value then its work fine, when @abc is null then @txt show null how can i concatenate this string and execute it Thankyou Ypki
-
Hi, My query -- parameter of the procedure @abc int = null; declare @txt varchar(100); set @txt = 'select * from tablename where field1 = isnull(@fld,field1)' set @txt = replace(@txt,'@fld',@abc) exec(@txt) when @abc has any value then its work fine, when @abc is null then @txt show null how can i concatenate this string and execute it Thankyou Ypki
Initialize variable
@abc
with 0 value, not withNULL
. e.gset @abc=0
I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.cacttus.com
modified on Wednesday, May 18, 2011 3:12 AM
-
Hi, My query -- parameter of the procedure @abc int = null; declare @txt varchar(100); set @txt = 'select * from tablename where field1 = isnull(@fld,field1)' set @txt = replace(@txt,'@fld',@abc) exec(@txt) when @abc has any value then its work fine, when @abc is null then @txt show null how can i concatenate this string and execute it Thankyou Ypki
A simple trick is to use COALESCE which returns you the first none null entry in a sequence of values, so you would replace the reference to @abc with
COALESCE(@abc, '')
.Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Hi, My query -- parameter of the procedure @abc int = null; declare @txt varchar(100); set @txt = 'select * from tablename where field1 = isnull(@fld,field1)' set @txt = replace(@txt,'@fld',@abc) exec(@txt) when @abc has any value then its work fine, when @abc is null then @txt show null how can i concatenate this string and execute it Thankyou Ypki
-
Hi, My query -- parameter of the procedure @abc int = null; declare @txt varchar(100); set @txt = 'select * from tablename where field1 = isnull(@fld,field1)' set @txt = replace(@txt,'@fld',@abc) exec(@txt) when @abc has any value then its work fine, when @abc is null then @txt show null how can i concatenate this string and execute it Thankyou Ypki