I am trying to join a string of database values but in this statement:
SET @sql = ('select' + @var1 + '+' + convert(varchar,@var2) +
'from [VoucherType] where [DeletedBy] is null and [AutoID] = 1')
I get an error saying
The data types nvarchar and bit are incompatible in the add operator.
@var2
which is [IsBirthdayVoucher]
in my database is a bit
type but I have already applied convert(varchar, @var2)
on it. Please kindly advice what I am doing wrong. Thanks.
SPROC:
declare @Str as varchar(max)
declare @var1 as varchar(30)=null
declare @var2 as varchar(30)=null
declare @var3 as varchar(30)=null
declare @var4 as varchar(30)=null
declare @var5 as varchar(30)=null
declare @sql as varchar(355)
set @Str = '[Code][IsBirthdayVoucher][IsReusable]VouT'
Set @var1= (select LEFT(@Str, CHARINDEX(']', @Str)))
set @Str = SUBSTRING(@Str, CHARINDEX(']', @Str) + 1, LEN(@Str))
Set @var2 =(select LEFT(@Str, CHARINDEX(']', @Str)))
set @Str = SUBSTRING(@Str, CHARINDEX(']', @Str) + 1, LEN(@Str))
Set @var3 =(select LEFT(@Str, CHARINDEX(']', @Str)))
set @Str = SUBSTRING(@Str, CHARINDEX(']', @Str) + 1, LEN(@Str))
SET @sql = ('select' + @var1 + '+' + convert(varchar,@var2) + 'from [VoucherType] where [DeletedBy] is null and [AutoID] = 1')
EXEC(@sql)
You're not converting [IsBirthdayVoucher]
to VARCHAR
, you're converting @var2
to VARCHAR
.
Try:
SET @sql = ('Select ' + @var1 + ' + Convert(varchar, ' + @var2 + ')
From [VoucherType]
Where [DeletedBy] is Null AND [AutoID] = 1')
try this:
SET @sql = ('Select CONCAT(' + @var1 + ',' + @var2 + ')
From VoucherType
Where DeletedBy is Null and [AutoID] = 1')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With