Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Howto udpdate text using Replace html font in textfield Expand / Collapse
Author
Message
Posted Thursday, November 8, 2012 4:09 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, November 8, 2012 5:57 AM
Points: 2, Visits: 4
I have a textfield SectionText in column in my table Section. The sectionText field contains multiple instances of html

with font references. e.g.

<span style="font-family: Times Roman;"> <span> </span></span><div><span><span style="font-family: Times Roman;">Below is a list of industry regulators that need to be notified when an incident occurs. The regulators may make arrangements to visit the site to assess any potential problems in the short, medium and long term.<br /><br /><strong> </strong></span></span><span><span style="font-size: 18pt;"><span style="color: rgb(54, 95, 145);"><span><span style="font-size: 12pt;"><span style="font-family: Arial;">

I want to write an update statement that replaces all occurrences of font-family: with "font-family: Arial;" i.e. all the html has the same arial font.

I thought I could use the REPLACE function



select replace(SectionText, '%"font-family:%;"%', '"font-family: Arial;"')

from tSection



But you can’t use wildcard characters.



I found the below bit of code on the web, which does work, but it only replaces the first occurrence of the word “font-family”, there could be multiple instances in the same field.



select

SUBSTRING(SectionText,1,CHARINDEX('"font-family:',SectionText))

+ '"font-family: Arial'+

SUBSTRING(SectionText,CHARINDEX(';"',SectionText,CHARINDEX('"font-family:',SectionText)),

LEN(SectionText)

)

from tSection

where SectionText like '%"font-family:%;"%'



If you can help that would be great!
Post #1382389
Posted Thursday, November 8, 2012 5:20 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 7:27 AM
Points: 2,836, Visits: 5,067
Your best friend here will be CLR function with RegEx.Replace.


_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1382430
Posted Thursday, November 8, 2012 5:22 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, November 8, 2012 5:57 AM
Points: 2, Visits: 4
Could you please perhaps give a little more detail I am new to SQL
Post #1382432
Posted Thursday, November 8, 2012 5:45 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 7:27 AM
Points: 2,836, Visits: 5,067
andrea.mayo (11/8/2012)
Could you please perhaps give a little more detail I am new to SQL


You will need to develop this function in .NET (c# or vb.net).
If you don't know any of .NET language, you will need help from .net developer


_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1382438
Posted Thursday, November 8, 2012 5:54 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 12:42 PM
Points: 1,787, Visits: 5,696
Extract the data, send it to a web developer, get them to change it, import it back in ?

MM


  • MMGrid Addin
  • MMNose Addin


  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • Post #1382442
    Posted Thursday, November 8, 2012 6:02 AM


    SSChampion

    SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

    Group: General Forum Members
    Last Login: Yesterday @ 7:44 AM
    Points: 12,910, Visits: 32,015
    here's a TSQL method, which generates the actual REPLACE statements you would want to use to remove the css style assignment, if it exists.

    Notice how i'm generating a lsit of "DesiredString", which is found within the data, and then use that as the basis for creating a suite of REPLACE commands:

    --Results:
    UPDATE #Inventory_Trans SET Description = REPLACE(Description,'font-family: Arial;','')
    UPDATE #Inventory_Trans SET Description = REPLACE(Description,'font-family: Courier New ;','')
    UPDATE #Inventory_Trans SET Description = REPLACE(Description,'font-family: Times Roman;','')
    UPDATE #Inventory_Trans SET Description = REPLACE(Description,'font-family: Verdana;','')


    Create table #Inventory_Trans (OtherId int,Description varchar(max) )
    insert into #Inventory_Trans
    SELECT 1,'<span style="font-family: Times Roman;"> <span> </span></span><div><span><span style="font-family: Times Roman;">Below is a list of industry regulators that need to be notified when an incident occurs. The regulators may make arrangements to visit the site to assess any potential problems in the short, medium and long term.<br /><br /><strong> </strong></span></span><span><span style="font-size: 18pt;"><span style="color: rgb(54, 95, 145);"><span><span style="font-size: 12pt;"><span style="font-family: Arial;"><span style="font-family: Verdana;">'
    UNION ALL
    SELECT 2,'<span style="font-family: Times Roman;"> <span> </span></span><div><span><span style="font-family: Times Roman;">Below is a list of industry regulators that need to be notified when an incident occurs. The regulators may make arrangements to visit the site to assess any potential problems in the short, medium and long term.<br /><br /><strong> </strong></span></span><span><span style="font-size: 18pt;"><span style="color: rgb(54, 95, 145);"><span><span style="font-size: 12pt;"><span style="font-family: Arial;"><span style="font-family: Times Roman;"> <span> </span></span><div><span><span style="font-family: Times Roman;">Below is a list of <span style="font-family: Courier New ;">industry regulators that need to be notified when an incident occurs. The regulators may make arrangements to visit the site to assess any potential problems in the short, medium and long term.<br /><br /><strong> </strong></span></span><span><span style="font-size: 18pt;"><span style="color: rgb(54, 95, 145);"><span><span style="font-size: 12pt;"><span style="font-family: Arial;">'


    --now get the ID's using a tally table
    declare
    @pre varchar(100),
    @post varchar(100),
    @pr int,
    @po int,
    @st int

    set @pre = 'font-family:'
    set @post = ';'
    set @pr = len(@pre)
    set @po = len(@post)
    set @st = 1

    ;WITH tally (N) as
    (SELECT TOP 10000 row_number() OVER (ORDER BY sc1.id)
    FROM Master.dbo.SysColumns sc1
    CROSS JOIN Master.dbo.SysColumns sc2)
    SELECT top 30
    T1.N,
    T2.*,
    @pre +
    SUBSTRING( T2.Description,
    ( CASE T1.N WHEN 1 THEN 1 ELSE T1.N + @pr END ),
    ( CASE CHARINDEX( @post, T2.Description, T1.N + @pr ) WHEN 0 THEN LEN( T2.Description ) - T1.N + @pr
    ELSE CHARINDEX( @post, T2.Description, T1.N + @pr ) - ( T1.N + @pr ) END ) )
    + @post AS DesiredString,
    Description as Description2
    Into #Results --capture into an intemediary table
    FROM Tally T1
    CROSS APPLY #Inventory_Trans T2
    WHERE N <= LEN( T2.Description )
    AND SUBSTRING( T2.Description, T1.N, @pr ) = @pre


    SELECT * from #Results


    select distinct 'UPDATE #Inventory_Trans SET Description = REPLACE(Description,''' + DesiredString + ''','''') '
    from #Results
    drop table #Inventory_Trans
    drop table #Results



    Lowell

    --There is no spoon, and there's no default ORDER BY in sql server either.
    Actually, Common Sense is so rare, it should be considered a Superpower. --my son
    Post #1382443
    « Prev Topic | Next Topic »

    Add to briefcase

    Permissions Expand / Collapse