| Author |
Topic |
|
cykophysh
Starting Member
24 Posts |
Posted - 2007-08-06 : 10:12:32
|
I have a column in a table in the DB called Branch, that actually contains a comma delimited string, which is the areas the Branch services.I need to create a query that will extract branches Email depending on the area is services. I thought I might be able to use PatIndex to search the Comma Delimited string for the areaThis is the Code from my Proc ALTER proc [dbo].[sfspGetBranchEmailByLocality] @locid intasDECLARE @CommaString VARCHAR(max)declare @Locality varchar(max)-- Get the Locality name set @Locality = (SELECT Locality FROM Locality WHERE (ID = @locid))-- Append commas to the locality nameSET @CommaString = ', ' + @Locality + ',' SELECT BranchEmailFROM Branchwhere PATINDEX(',' + cast(LocalitiesServed as varchar(max)) + ',', @Commastring) > 0However my idea doesn't seem to work , was wondering if you guys would have some pointers Kind Regards,Gary |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-08-06 : 10:23:10
|
| Trywhere PATINDEX('%'+@Commastring+'%',',' + cast(LocalitiesServed as varchar(max)) + ',') > 0MadhivananFailing to plan is Planning to fail |
 |
|
|
cykophysh
Starting Member
24 Posts |
Posted - 2007-08-06 : 10:35:42
|
| Thanks for thatTried that already and it doesn't seem to work either.Kind Regards,Gary |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-08-06 : 10:53:04
|
| Can you fix your database design? A poor design is making what should be a simple SELECT something much slower, less efficient, and more complicated.- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
cykophysh
Starting Member
24 Posts |
Posted - 2007-08-06 : 11:16:00
|
quote: Originally posted by jsmith8858 Can you fix your database design? A poor design is making what should be a simple SELECT something much slower, less efficient, and more complicated.- Jeffhttp://weblogs.sqlteam.com/JeffS
This is an Inherited DB designSometimes you got to work with what you've gotClients aren't always happy to engage in more work.There is an import routine that inserts data into the DB, so changing any DB structure would potentially break the importsSo No, changing the DB design is not an option. Would love to work in the Utopia where clients are happy to pay for extensive work :-)Kind Regards,Gary |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-08-06 : 11:45:53
|
quote: Originally posted by cykophysh Thanks for thatTried that already and it doesn't seem to work either.Kind Regards,Gary
this approach should work fine.can you show us the query? you probably have extra spaces that get in the way._______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenp |
 |
|
|
cykophysh
Starting Member
24 Posts |
Posted - 2007-08-06 : 12:00:52
|
quote: Originally posted by spirit1
quote: Originally posted by cykophysh Thanks for thatTried that already and it doesn't seem to work either.Kind Regards,Gary
I've been playing around with this declare @Locality varchar(max)DECLARE @SafeInClause VARCHAR(100)Declare @LocalitiesServed varchar(max)set @Locality = 'Area1'set @LocalitiesServed = 'Area1, Area2, Area3, Area4, Area5'Select @LocalitySET @SafeInClause =', ' + @Locality +', 'Select @SafeInClauseSELECT BranchEmailFROM Branchwhere PATINDEX( @SafeInClause, ', ' + cast( LocalitiesServed as varchar(max)) + ', ' ) > 0 this approach should work fine.can you show us the query? you probably have extra spaces that get in the way._______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenp
Kind Regards,Gary |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-08-06 : 12:07:53
|
| use either:WHERE charindex( @SafeInClause, ', ' + cast( @LocalitiesServed as varchar(max)) + ', ' ) > 0orWHERE patindex( '%' + @SafeInClause + '%', ', ' + cast( @LocalitiesServed as varchar(max)) + ', ' ) > 0_______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenp |
 |
|
|
cykophysh
Starting Member
24 Posts |
Posted - 2007-08-06 : 12:13:24
|
Thanks Spirit1Thats done the trick!!!quote: Originally posted by spirit1 use either:WHERE charindex( @SafeInClause, ', ' + cast( @LocalitiesServed as varchar(max)) + ', ' ) > 0orWHERE patindex( '%' + @SafeInClause + '%', ', ' + cast( @LocalitiesServed as varchar(max)) + ', ' ) > 0_______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenp
Kind Regards,Gary |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-08-07 : 02:22:20
|
<<PATINDEX( @SafeInClause, ', ' + cast( LocalitiesServed as varchar(max)) + ', ' ) > 0>>It means that you didnt look at the query I posted clearly MadhivananFailing to plan is Planning to fail |
 |
|
|
cykophysh
Starting Member
24 Posts |
Posted - 2007-08-07 : 04:14:08
|
It means I needed the '%' in the query quote: Originally posted by madhivanan <<PATINDEX( @SafeInClause, ', ' + cast( LocalitiesServed as varchar(max)) + ', ' ) > 0>>It means that you didnt look at the query I posted clearly MadhivananFailing to plan is Planning to fail
Kind Regards,Gary |
 |
|
|
|