Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Substring change using table valued function

$
0
0

Experts,

We have some sample records into address table as:-

CREATE TABLE Address_Temp
(
	R_Id	INT				NOT NULL,
	Add1	VARCHAR(100)	NULL,
	City	VARCHAR(30)		NULL,
	State	VARCHAR(2)		NULL,
CONSTRAINT [PKAddress_Temp] PRIMARY KEY CLUSTERED 
(
	[R_Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
)  ON [PRIMARY]

INSERT INTO Address_Temp VALUES ( 1, '188 East StReet', 'Worthington', 'MA' )
INSERT INTO Address_Temp VALUES ( 2, '1222 Main St', 'Hopkins', 'NY' )
INSERT INTO Address_Temp VALUES ( 3, '410 W St', 'Madison', 'WI' )
INSERT INTO Address_Temp VALUES ( 4, '3013 N West Avenue', 'Chicago', 'IL' )
INSERT INTO Address_Temp VALUES ( 5, '610 W Third St', 'Worthington', 'MA' )
INSERT INTO Address_Temp VALUES ( 6, '341 Western Ave', 'Lynn', 'MD' )
INSERT INTO Address_Temp VALUES ( 7, '410 West St', 'Madison', 'WI' )

Now we have conversation table as:-

DECLARE	@Conversation TABLE
(
	Str1	VARCHAR(10),
	Str2	VARCHAR(10)
)

INSERT INTO @Conversation VALUES ( 'Street', 'St' )
INSERT INTO @Conversation VALUES ( 'West', 'W' )
INSERT INTO @Conversation VALUES ( 'Avenue', 'Ave' )
INSERT INTO @Conversation VALUES ( 'Third', '3rd' )

As we don't have any type of like in statement in SQL Server, we are creating full-text search and perform below query:-

CREATE FULLTEXT CATALOG [AddrFullTextSearch] WITH ACCENT_SENSITIVITY = ON

CREATE FULLTEXT INDEX ON [dbo].[Address_Temp](
[Add1])
KEY INDEX [PKAddress_Temp] ON [AddrFullTextSearch]
WITH CHANGE_TRACKING AUTO

DECLARE	@Sql VARCHAR(500) = ''

DECLARE	@Conversation TABLE
(
	Str1	VARCHAR(10),
	Str2	VARCHAR(10)
)

INSERT INTO @Conversation VALUES ( 'Street', 'St' )
INSERT INTO @Conversation VALUES ( 'West', 'W' )
INSERT INTO @Conversation VALUES ( 'Avenue', 'Ave' )
INSERT INTO @Conversation VALUES ( 'Third', '3rd' )

SELECT	DISTINCT
		@Sql = STUFF((SELECT ' OR " *' + a.Str1 + '* "' AS [text()]
                FROM @Conversation a
                FOR XML PATH('')
                ), 1, 4, '' )
FROM	@Conversation

--SELECT	@Sql

SELECT	*
FROM	Address_Temp
WHERE	CONTAINS(Add1, @Sql)--'" *West* " OR " *Avenue* "')

--SELECT	*
--FROM	@Conversation

--DROP FULLTEXT INDEX ON Address_Temp
--DROP FULLTEXT CATALOG [AddrFullTextSearch]
--DROP TABLE Address_Temp

Upto here everything is working perfectly, as we want to update only 4 records of the Address_Temp table out of 7 records.

Now we want to create table valued function so that we can update with my expected o/p:-

SELECT	'1' AS 'R_Id', '188 East St' AS 'Add1', 'Worthington' AS 'City', 'MA' AS 'State'
UNION ALL
SELECT	'4' AS 'R_Id', '3013 N W Ave' AS 'Add1', 'Chicago' AS 'City', 'IL' AS 'State'
UNION ALL
SELECT	'5' AS 'R_Id', '610 W 3rd St' AS 'Add1', 'Worthington' AS 'City', 'MA' AS 'State'
UNION ALL
SELECT	'7' AS 'R_Id', '410 W St' AS 'Add1', 'Madison' AS 'City', 'WI' AS 'State'
Much appreciate your help.

Thank you in advance

Kumar


Please do let us know your feedback. Thank You - KG, MCTS


Viewing all articles
Browse latest Browse all 23857

Trending Articles