mgmhicks
asked on
Using apostrophe in sql statements
I get a list of data similar to this CM,SL,BF
what I need to do is send to parameter in sql stored procedure.
my code looks similar to this.
@myList = '''WG'',''SM'''
a.propertyid in (@myList) ''this does not work, however I type
a.propertyid in ('WG','sm') that works and gets the results I need,
So could someone tell me what is wrong with @mylist line and what it needs to be to get this to work.
thanks
what I need to do is send to parameter in sql stored procedure.
my code looks similar to this.
@myList = '''WG'',''SM'''
a.propertyid in (@myList) ''this does not work, however I type
a.propertyid in ('WG','sm') that works and gets the results I need,
So could someone tell me what is wrong with @mylist line and what it needs to be to get this to work.
thanks
Try this:
@myList = "'WG','SM'"
~bp
@myList = "'WG','SM'"
~bp
ASKER
sorry that didn't work
declare @myList nvarchar(50)
set @mylist = "'WG','SM"'
that errors on just typing that.
declare @myList nvarchar(50)
set @mylist = "'WG','SM"'
that errors on just typing that.
ASKER
Kyle I tried your function, although it didn't error out It did not give me a results list. This is what I get when I select @mylist 'WG','SM' so that looks good but still not getting my results
here is the entire code.
declare @myList nvarchar(50)
set @mylist = '''WG'',''SM'''
select @mylist
select a.PropertyId,a.BldgId,a.Un itId,a.Res iId,rtrim( a.property id)+'-'+rt rim(a.bldg id)+'-'+rt rim(a.unit id)+'-'+rt rim(a.resi id) as fullID, rtrim(ResiFirstName)+SPACE (1)+rtrim( ResiLastNa me) as name,LeaseEndDate,resistat us,
b.Amount,b.Status from tag_esite.dbo.lease a
right outer join TAG_esite.dbo.ePaySetup b on a.Propertyid=b.PropertyId and a.bldgid=b.BldgId and a.unitid=b.UnitId and a.resiid=b.resiid
where (LeaseEndDate >='09/01/2014' and Leaseenddate <='12/31/2014' and ResiStatus<>'X') and b.Status='A' and a.PropertyId IN ( select * from dbo.fn_txt_split(@myList, ',')) and LeaseEndDate <>
(SELECT DATEADD(dd, -DAY(DATEADD(m,1,leaseendd ate)), DATEADD(m,1,LeaseEndDate))
AS LastDayOfMonth)
order by a.leaseenddate
thank you for the help
here is the entire code.
declare @myList nvarchar(50)
set @mylist = '''WG'',''SM'''
select @mylist
select a.PropertyId,a.BldgId,a.Un
b.Amount,b.Status from tag_esite.dbo.lease a
right outer join TAG_esite.dbo.ePaySetup b on a.Propertyid=b.PropertyId and a.bldgid=b.BldgId and a.unitid=b.UnitId and a.resiid=b.resiid
where (LeaseEndDate >='09/01/2014' and Leaseenddate <='12/31/2014' and ResiStatus<>'X') and b.Status='A' and a.PropertyId IN ( select * from dbo.fn_txt_split(@myList, ',')) and LeaseEndDate <>
(SELECT DATEADD(dd, -DAY(DATEADD(m,1,leaseendd
AS LastDayOfMonth)
order by a.leaseenddate
thank you for the help
You can try this with the LIKE operator, instead of splitting them:
@myList = '%WG%SM%'
a.propertyid LIKE @myList
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
when I do a select * from dbo.fn_txt_split(@myList, ',') I get this
Item
'WG'
'SM'
so that's 2 records,
Item
'WG'
'SM'
so that's 2 records,
ASKER
Awesome solution, thank you very much
You need a split function to separate them out:
where
a.propertyid in ( select * from dbo.fn_txt_split(@myList, ','))
Open in new window