RTF to plain text in SQL with read only permissions - sql

I have a MSSQL 2005 database which also stores RTF-like text format. I'd like to strip off all formatting code and just get the plain text. Now the tricky part; I only have read access to the database. So, unfortunately I can't use any CREATE FUNCTION solutions...
For example, a column starts with something like:
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}} \viewkind4\uc1\pard\lang1043\ul\b\f0\fs22
Can this be done?

Related

Don't sanitize HTML when inserting text from MS Access Into SQL Database

My Application is basically an Access front end with a sql database in the backend.
I have a textfield in which i can write text. If i put new lines there, I can see that in the SQL database, there is a inserted for formatting.
BUT when i write a there, the is sanitized and thus not displayed correctly later on.
Example:
eins
absatz
custom <div></div>
is saved as
<div>eins</div> <div>absatz</div> <div>custom<div></div></div>
I actually don't know how to prevent that from happening, it seems like acces is doint that automatecally.
The access is version 2016 and the sql server is a mcrosoft SQL server Developer
My goal was the the user could type his own HTML for formatting for example and then this HTML is stored normally as the autogenrated html in the database, to be displayed correctly when exporting data to pdf.

Export data from SQL without the hard returns

I have a field in SQL that is called Comments I am trying to pull all comments with a "?" in them so below is a sample code like what I am using. The code works fine however the problem is when I go to copy this data out and paste it into word or excel the information comes out looking jumbled. I have figured out the reason for this is that the input side of the application where the comments are entered allows for the user to do multi-line comments so there are hard returns in the field. Is there a way I can export the data without the hard returns. I am using SSMS with a SQL 2012 database. For Office I have 2013 if that is needed.
Select C.Comment
From Patron.Comment as C
Where C.Comment like '%?%'
Yes, this is possible, you can use the REPLACE function to get rid of unwanted newline characters:
Replace a newline in TSQL

Search and Replace a a partial string / substring in mssql tables

I was tasked with moving an installation of Orchard CMS to a different server and domain. All the content (page content, menu structure, links, etc.) is stored in an MSSQL database. The good part: When moving the physical files of the Orchard installation to the new server, the database will stay the same, no need to migrate it. The bad thing: There are lots and lots of absolute URLs scattered all over the pages and menus.
I have isolated / pinned down the tables and fields in which the URLs occur, but I lack the (MS)SQL experience/knowledge to do a "search - replace". So I come here for help (I have tried exporting the tables to .sql files, doing a search-replace in a text editor, and then re-importing the .sql files to the database, but ran into several syntax errors... so i need to do this the "SQL way").
To give an example:
The table Common_BodyPartRecord has the field Text of type ntext that contains HTML content. I need to find every occurance of the partial string /oldserver.com/foo/ and replace it with /newserver.org/bar/. There can be multiple occurances of the pattern within the same table entry.
(In total I have 5 patterns that will need replacing, all partial string / substrings of urls, domains/paths, etc.)
I usually do frontend stuff and came to this assignment by chance. I have used MySQL back in the day I was playing around with PHP related stuff, but never got past eh basics of SQL - it would be helpful if you could keep your explainations more or less newbie-friendly.
The SQL server version is SQL Server 9.0.4053, I have access to the database via the Microsoft SQL Server Management Studio 12
Any help is highly appreciated!
You can't manipulate the NTEXT datatype directly, but you can CAST it to VARCHAR(MAX), then use the REPLACE function to perform the string replacement, then CAST it back to NTEXT. This can all be done in a single UPDATE statement.
update MyTable
set MyColmun = cast(replace(cast(MyColumn as nvarchar(max)), N'/oldserver.com/foo/', N'/newserver.org/bar/') as ntext)
where cast(MyColumn as nvarchar(max)) LIKE N'%/oldserver.com/foo/%'
The WHERE clause in the UPDATE statement below is used to prevent SQL Server from making non-changes, i.e. if the value does not need to be changed then there is no need to update it to itself.
The CAST function is used to change the data type of a value. NTEXT is a legacy data type used for storing large character values, NVARCHAR(MAX) is a new and more versatile data type for storing large character values. The REPLACE function can not operate on NTEXT values, hence the need to CAST it to NVARCHAR(MAX) first, do the replace, then CAST it back to NTEXT afterwards.

Unicode characters not saving with Access front end linked to sql table

I have an old access database that I have converted to Office 2010 format and then moved the one data table to SQL. There is only one form that is associated with the one linked table.
Once it was all done I compared the data from before and after and found that all the ≤ had been converted to =. I had mistakenly set the field as varchar so I updated it to nvarchar. I then inserted some corrected data via SQL Server Mgt Studio and all looked good.
The issue is that if I enter ≤ symbols via access they look fine, but once I close and reopen the front end they aren't there. They seem to be being converted to = when access writes to the sql back end table.
My research says that it may be that the ODBC connection is stuffing up the Unicode character, but then other places say it should be fine. I am not doing the update via sql so I can't try the N in front of unicode text.
Any suggestions?
The comment from Gord was right on the mark. When you make changes to the SQL table in the back end Access will not update the linked table. You need to remove it and re-add it.
Just use the same name as before and everything will work fine.

SQL (T-SQL, SQL Server Mgmnt Studio): Creating comma delimited .txt file with commas in fields

Background: I have to create a report that will be run regularly to send to an external entity. It calls for a comma delimited text file. Certain fields required for the report contain commas (I can easily parse the commas out of the name fields, but errant commas in the address and certain number fields are trickier). I have no control over the database design or input controls.
I know how to get a comma-delimited text file of query results from SQL Server Management Studio. But the commas in the fields screw everything up. I can change the delimiting character and then get the fields right in Excel, but that's just a workaround - it needs to be able to meet specifications automatically.
This report previously ran on an antiquated DBMS - I have a copy of an old report, and the fields are all enclosed in double quotes ("...."). This would work - though I don't know how the external users parse the fields (not my problem) - but I'm too dumb to figure out how to do it in t-sql.
Thanks!
You can use the Export Data task, but if you must try getting these results from Management Studio after running a query, go to Tools>Options, find the settings for Grid Output and check the box to delimit fields that contain field separators. This option will only take effect when you open a new query window.