Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
ddombadoh
Starting Member
7 Posts |
Posted - 2013-06-03 : 10:08:26
|
Hell All,I have a table with the following structure;Column Datatypeid inttitle nvarchar(500)visibility nvarchar(50)newsDate datetimeThe table has sample data below;id title visibilty newsDate2 Test title 0; 2013-05-303 Test title 2 12;5; 2013-05-304 Test title 3 5; 2013-05-30Now i wrote the stored procedure below;ALTER PROCEDURE [dbo].[selectUserNewsByRoles] @var varchar(50)ASBEGIN SELECT * from tbl_news where ( PATINDEX('%'+left(@var,1)+'%', visibility) > 0 or PATINDEX('%'+right(@var,1)+'%', visibility) > 0 ) ORDER BY newsDateEND When i execute this procedure with '5' as parameter, i get id title visibilty newsDate3 Test title 2 12;5; 2013-05-304 Test title 3 5; 2013-05-30 which is correct. However when i pass '1', i getid title visibilty newsDate3 Test title 2 12;5; 2013-05-30which i don't expect, because the visibility does not contain 1, but rather 12.Any help on this will be appreciated. |
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2013-06-03 : 11:01:35
|
Hope this helps( PATINDEX('%'+left(@var,1)+';%', visibility) > 0 or PATINDEX('%'+right(@var,1)+';%', visibility) > 0 )Note: this depends on data and will work only if every number in the visibility column has a simicolon.CheersMIK |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-04 : 00:06:28
|
You just need this i reckonALTER PROCEDURE [dbo].[selectUserNewsByRoles] @var varchar(50)ASBEGIN SELECT * from tbl_news where ';' + visibilty + ';' LIKE '%;' + @var + ';%' ORDER BY newsDateEND ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
ddombadoh
Starting Member
7 Posts |
Posted - 2013-09-13 : 12:22:48
|
Thanks visakh16.Your solution seem to be working until I hit another challenge. When I pass a string like '5;12', I don't get any result, as I was expectingid title visibilty newsDate3 Test title 2 12;5; 2013-05-304 Test title 3 5; 2013-05-30Thanks |
|
|
JaybeeSQL
Posting Yak Master
112 Posts |
Posted - 2013-09-20 : 07:50:11
|
'Visibility', not 'Visibilty' ! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-09-22 : 04:18:26
|
quote: Originally posted by ddombadoh Thanks visakh16.Your solution seem to be working until I hit another challenge. When I pass a string like '5;12', I don't get any result, as I was expectingid title visibilty newsDate3 Test title 2 12;5; 2013-05-304 Test title 3 5; 2013-05-30Thanks
sorry i had it in wrong orderit should beALTER PROCEDURE [dbo].[selectUserNewsByRoles] @var varchar(50)ASBEGIN SELECT * from tbl_news where ';' + @var + ';' LIKE '%;' + visibilty + ';%' ORDER BY newsDateEND ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|