Replacing values with wildcards (parsing text data) - sql

I have rows which contains HTML tags. e.g.
<b>Abc</b> <strong>Bca</strong>
So I need to cut it out. As I suggest I need to find something like '%<%>%' and make a REPLACE to ''.
How can I do it? Interested for both solutions - MS SQL & Oracle also.

Assuming table is called yourtable and field is called htmltag.
In SQL Server:
SELECT
SUBSTRING(substring(htmltag,charindex('>',htmltag)+1,250),0,CHARINDEX('<',
substring(htmltag,charindex('>',htmltag)+1 ,250),0))
FROM yourtable
SQL FIDDLE
In Oracle
SELECT regexp_replace(htmltag, '<[^>]+>', '') htmltag
FROM yourtable
SQL FIDDLE

Related

Using Regex to determine what kind of SQL statement a row is from a list?

I have a large list of SQL commands such as
SELECT * FROM TEST_TABLE
INSERT .....
UPDATE .....
SELECT * FROM ....
etc. My goal is to parse this list into a set of results so that I can easily determine a good count of how many of these statements are SELECT statements, how many are UPDATES, etc.
so I would be looking at a result set such as
SELECT 2
INSERT 1
UPDATE 1
...
I figured I could do this with Regex, but I'm a bit lost other than simply looking at everything string and comparing against 'SELECT' as a prefix, but this can run into multiple issues. Is there any other way to format this using REGEX?
You can add the SQL statements to a table and run them through a SQL query. If the SQL text is in a column called SQL_TEXT, you can get the SQL command type using this:
upper(regexp_substr(trim(regexp_replace(SQL_TEXT, '\\s', ' ')),
'^([\\w\\-]+)')) as COMMAND_TYPE
You'll need to do some clean up to create a column that indicates the type of statement you have. The rest is just basic aggregation
with cte as
(select *, trim(lower(split_part(regexp_replace(col, '\\s', ' '),' ',1))) as statement
from t)
select statement, count(*) as freq
from cte
group by statement;
SQL is a language and needs a parser to turn it from text into a structure. Regular expressions can only do part of the work (such as lexing).
Regular Expression Vs. String Parsing
You will have to limit your ambition if you want to restrict yourself to using regular expressions.
Still you can get some distance if you so want. A quick search found this random example of tokenizing MySQL SQL statements using regex https://swanhart.livejournal.com/130191.html

trim whitespaces in oracle where condition

I'm facing the following issues when i tried to retrieve the data from oracle database by trimming the whitespace. I'm using oracle 12G version.
Let's say the name column has some whitespaces like 'Test '.
Table name - PGM_DETAIL
ID NAME
1 Test
When I'm trying to fetch the data using following query. It's not fetching it.
SELECT * FROM PGM_DETAIL WHERE TRIM(NAME) = TRIM('Test');
Could you anyone please suggest me what the problem in my sql statement.
Thanks in advance...,
Oracle comparisons are case-sensitive by default.
So try:
WHERE UPPER(TRIM(NAME)) = TRIM('TEST');
try
SELECT * FROM PGM_DETAIL WHERE TRIM(NAME) = TRIM('Test');

How to get the value after dot in sql query

Hi i'm saving file name as a date and time with file extension like below
43201612150389.docx,
73201611843471.jpg
in my sql table, now I want after dot which extension using sql simple query any one have idea.
Use SUBSTRING_INDEX:
SELECT SUBSTRING_INDEX(mycol, '.', -1)
Demo here
In SQL Server you can use PARSENAME:
SELECT PARSENAME(col, 1)
MySql:
SELECT SUBSTRING_INDEX(field, '.', -1)
SQL:
SELECT PARSENAME(field, 1)
if you have all those values stored in table ,numbers table is best way to do it.
select substring(#string,n,len(#string))
from numbers
where substring(#string,n,1)='.'

SQl Trim problem

I am trying to pull data from table using this select statement..
SELECT ID_NO
FROM EMPLOYEE
WHERE trim(SYN_NO) ='21IT';
SYN_NO column hold data in this format
21IT / 00065421
I want to get just first four characters and drop rest of it.. i tried trim, rtrim but it didnt work. Is there a way to do this.. Thank you
Sadly for those of us coming from SQL Server, there is no LEFT in oracle. The article I linked below talks about one person's encounter with this problem, and recommends using
SUBSTR(SYN_NO,1,4)
(see article)
Oracle SUBSTR specification
Sorry, just realized this is Oracle. You need to use SUBSTR:
SELECT ID_NO
FROM EMPLOYEE
WHERE SUBSTR(SYN_NO, 1, 4) ='21IT';
In Oracle, you'd use the SUBSTR function
SELECT id_no
FROM employee
WHERE substr(syn_no,1,4) = '21IT'

Joining SQL result with string

I have a MSSQL 2005 database with a lot of records that were added since last backup. I want to make another SQL script that puts result values into string representing INSERT statement that I will save for later use.
Something like:
SELECT 'Insert INTO tabname columns VALUES("+Column1"',')' FROM XY
Or simple example:
Column A,Row1=5
SELECT A+"BLAH" FROM X
should return "BLAH5"
Thank you
I'm not sure i understand, if you want to build a script (lets say PHP) just run over the records and either print out or to file something like:
echo 'INSERT INTO tablename (field1,field2) VALUES('.$row['field1'].','.$row['field2'].');';
if you want that string in the result directly from the SQL you could use CONCAT:
SELECT CONCAT('INSERT INTO...VALUES(',field1,',',field2,')') FROM yourtable;
Hope that helps...
You should really mention what SQL database system you're using.
For MySQL, what you want is the CONCAT function.
SELECT CONCAT('INSERT INTO table (columns) VALUES ("', column1, '");') FROM xy;
what version of sql?
for ms sql, you can use + for concatenation and single quotes for strings
for mysql/oracle, use concat(column, 'string')