Manipulating Text Field Data in a SQL Stored Procedure

Manipulating Text Field Data in a SQL Stored Procedure

The code below is functionally equivalent to a VB Replace function, but, instead, it allows you to perform an action in a Text type field in a SQL Server Stored Procedure. This functionality can be especially useful in content management type situations, allowing you, for example, to replace all carriage returns with an HTML

 CREATE Procedure sp_replaceASDECLARE	        @pointer binary(16),		@iPatternLoc Integer,		@rowId [email protected] is an arbitrary row identifier this could be passed in by-- a parameterSELECT @rowId = 1--Select Satement below retrieves first location of a searched--pattern (in this case carriege return) and assigns it [email protected] variableSELECT @iPatternLoc = PATINDEX('%' + CHAR(13) + '%',text_field)FROM someTable WHERE someTableId = @rowId--This Select statement returns a pointer variable pointed to thetext_field--column.SELECT @pointer = TEXTPTR(text_field)FROM someTableWHERE text_field = @rowIdSelect @iPatternLoc = @iPatternLoc - 1--Loop through all instances of Char(13) and replace them with--
using the UPDATETEXT functionWHILE @iPatternLoc > -1 BEGIN UPDATETEXT text_field @pointer @iPatternLoc 4 '
' SELECT @iPatternLoc = PATINDEX('%' + CHAR(13) + '%',text_field) FROM someTable WHERE someTableId = @rowId Select @iPatternLoc = @iPatternLoc - 1 END

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of