A SQL Query to select a string between two html tags - sql-server

I need to get the text between html tags in SQL.
< em> " **Test,Extract Me** " < /em>
This is just a small part of html body where the unique identifier is < em>"
Please help

SELECT SUBSTRING(ColumnName,CHARINDEX('html_tag',ColumnName)+LEN('html_tag'),CHARINDEX('html_close_tag',ColumnName)-LEN('html_close_tag')) FROM TableName
please replace html_tag,html_close_tag with your tags.

I think you want to take a substring of the column value.
You can try something like this:
SELECT SUBSTRING(ColumnName, 5, LEN(ColumnName) - 5) AS TrimmedValue FROM TABLE;

SELECT SUBSTRING(ColumnName,CHARINDEX('html_tag',ColumnName)+LEN('html_tag'), CHARINDEX('html_close_tag',ColumnName) - CHARINDEX('html_tag',ColumnName) - LEN('html_close_tag') +1) FROM TableName
In case the marked answer query doesn't work.

With using JQUERY you can find easily with simple one line of code :
$('em').text();
You can do the same with simple JS as well
document.getElementsByTagName('em')[0].innerText

Related

Remove all letters from a Code in SSIS in Derived column transformation editor

I have these codes in SSIS:
102022A
1035C
102074A
102074D
174Z
I would like to remove the letters (last string) of my codes to return the data like this:
102022
1035
102074
102074
174
How I can do this in SSIS? Which proper function should I use?
Assuming we can rely on there only being one letter, which also always occurs at the end of the code, we can use SUBSTRING here:
SELECT code, SUBSTRING(code, 1, LEN(code) - 1) AS code_out
FROM yourTable;
If you actually want to modify the codes in your table, then use an update with similar logic:
UPDATE yourTable
SET code = SUBSTRING(code, 1, LEN(code) - 1)
WHERE code LIKE '%[A-Z]';

How can I add a dash to separate the string when is too long in SQL Server? I need to write the script, not hardcoding it...thanks

I need to rename my records..so the data the I have today is like:
user.apple
user.applebanana
user.applegrapes
user.applegrapeskiwi
I want:
user.apple,
user.apple-banana,
user.apple-grapes,
user.apple-grapes-kiwi
and so on...
I need to add a dash when the second string is too big...
I'm newbie in SQL, I have tried a substring but is not this. It would be easier if I could use a replace() function, but I'm not supposed to hardcode... :(
If the second word is always 'apple' and is always prefixed with . then you could use STUFF to inject the character:
SELECT YourColumn,
CASE LEN(YourColumn) WHEN CHARINDEX('.',YourColumn) + 5 THEN YourColumn
ELSE STUFF(YourColumn, CHARINDEX('.',YourColumn)+6,0,'-') END
FROM (VALUES('user.apple'),
('user.applebanana'),
('user.applegrape'),
('operator.apple'),
('operator.applekiwi'))V(YourColumn);
The CASE expression is there, as otherwise you'd have 'user.apple-'.
DB<>Fiddle

Custom ms sql query

Is it posible that an ms sql query to return only a portion of what is stored in a field?
For example, I got this data stored in the field:
row NGV1="" NGV10="*" NGV6=" " NGV5=" " NGV4=" " NGV3=" " NGV2=" " _tipprodus="NGV" lotuloptim="20" /
I only need to display the value that is between the quotation marks from this part lotuloptim="20". The result should be 20.
Thank you in advance.
Best regards
Yes, you can use SUBSTRING, but using substring you need to know the start index
SELECT SUBSTRING(column_name,start,length) AS some_name FROM table_name;
and then you can use CHARINDEX to find the index for example to the lotuloptim word
DECLARE #document varchar(64);
SELECT #document = 'Reflectors are vital safety' +
' components of your bicycle.';
SELECT CHARINDEX('bicycle', #document);
GO

How to remove a full stop from the end of data

I need to update my table because a number of fields in a column have got a fullstop on the end, i need to remove this.
So, in TableA, Field1: the data looks like
1002243.
1007053.
1007403.
1104098.
1110010.
NOTE: Not all the fields are the same length.
I need to remove the full stops.
Im using SQL Server 2005, cheers :)
UPDATE TableA
SET Field1 = LEFT(Field1 ,LEN(Field1)-1)
WHERE Field1 LIKE '%.'
Then you have data after the period. Try this to see:
Print '[' + Field1 + ']' and see if you get something like this:
[101. ]
There is a space after the period.

Can't Convert unicode Data into XML column in sql server 2008

I already have a table with 3 column (Id bigint, Title nvarchar(450), Description nvarchar(MAX)) in sql 2008
I decide convert Title and Description column into one XML column. but when trying to update get many error like "illegal name character" or "illegal qualified name character" and etc.
to solve this problem i just create windows application with subsonic 2.1 with below code
MyTable tbl = new MyTable(1111);
tbl.myXMLcol = "<Person><Title><![CDATA[ " + HttpUtility.HtmlEncode(tbl.Title) + " ]]></Title><Description><![CDATA[ " + HttpUtility.HtmlEncode(tbl.Description) + " ]]></Description></Person>";
tbl.Save();
then try to add "<?xml version=\"1.0\" encoding=\"utf-16\"?>" into first above string that get error "unable to switch the encoding".
Note: Also i using this method to remove illegal character, but not solve my problem
Note2: I trying to update Japanese record that get this error, but for English work properly.
Could you please help me.
Thanks.
I think you should be using UTF-8 encoding.
You can find out more about the encoding here:
http://www.ibm.com/developerworks/xml/library/x-utf8/
Also, you will find some more information here:
http://msdn.microsoft.com/en-us/library/ms131375.aspx
Why not do all of this directly in SQL Server ?? Could you try this:
UPDATE YourTable
SET XmlCol = CAST(N'<Person><Title>' + Title +
N'</Title><Description>' + Description +
N'</Description></Person>' AS XML)
WHERE ID = 12345
Does that work? If not, then maybe this one here?
UPDATE YourTable
SET XmlCol = CONVERT(XML, N'<Person><Title>' + Title +
N'</Title><Description>' + Description +
N'</Description></Person>', 2)
WHERE ID = 12345
The "2" for style will strip out things SQL Server doesn't like about XML - things like DTD and so forth. Maybe that'll help in your case, too?
I've had various troubles with SQL Server's XML support when importing XML from outside. Usually, one way or another, it ends up working, though :-)
Marc
After many research, I found article about my problem.
Please see Tarun Ghosh post

Resources