String regex on sql - sql

I have "X1lNtWp4_e" type of string on SQL code.
I wanna convert like X1lNtWp4_e without the ( " ).
My DB Engine codes like MySQL I think.
I'm using http://chartio.com so i'm not sure which db engine.

Chartio is just a front end but assuming your DB is MySQL simply use:
Use the TRIM function with the BOTH argument:
SELECT TRIM(BOTH '"' FROM '"X1lNtWp4_e"')
FROM yourtable
Use the REPLACE function if the middle content contains " also and you want them removed:
SELECT REPLACE('"X1lNtWp4_e"', '"', '')
FROM yourtable

Related

Postgresql: Extracting substring after first instance of delimiter

I'm trying to extract everything after the first instance of a delimiter.
For example:
01443-30413 -> 30413
1221-935-5801 -> 935-5801
I have tried the following queries:
select regexp_replace(car_id, E'-.*', '') from schema.table_name;
select reverse(split_part(reverse(car_id), '-', 1)) from schema.table_name;
However both of them return:
01443-30413 -> 30413
1221-935-5801 -> 5801
So it's not working if delimiter appears multiple times.
I'm using Postgresql 11. I come from a MySQL background where you can do:
select SUBSTRING(car_id FROM (LOCATE('-',car_id)+1)) from table_name
Why not just do the PG equivalent of your MySQL approach and substring it?
SELECT SUBSTRING('abcdef-ghi' FROM POSITION('-' in 'abcdef-ghi') + 1)
If you don't like the "from" and "in" way of writing arguments, PG also has "normal" comma separated functions:
SELECT SUBSTR('abcdef-ghi', STRPOS('abcdef-ghi', '-') + 1)
I think that regexp_replace is appropriate, but using the correct pattern:
select regexp_replace('1221-935-5801', E'^[^-]+-', '');
935-5801
The regex pattern ^[^-]+- matches, from the start of the string, one or more non dash characters, ending with a dash. It then replaces with empty string, effectively removing this content.
Note that this approach also works if the input has no dashes at all, in which case it would just return the original input.
Use this regexp pattern :
select regexp_replace('1221-935-5801', E'^[^-]+-', '') from schema.table_name
Regexp explanation :
^ is the beginning of the string
[^-]+ means at least one character different than -
...until the - character is met
I tried it in a conventional way in general what we do (found
something similar to instr as strpos in postgrsql .) Can try the below
SELECT
SUBSTR(car_id,strpos(car_id,'-')+1,
length(car_id) ) from table ;

SQL LIKE '%/_Description' returns unexpected results

I use the LIKE statement like '%/_Description' but got the following results:
Description
Something/_Description
Using '%/[_]Description', "Description" is returned.
How can I restrict the result only to something like "Something/_Description"
Please note this is a standard SQL so supposed to be ran at both SQL Server/Oracle
Thanks,
You can escape the wildchar characters with the character defined in the ESCAPE clause:
match_expression [ NOT ] LIKE pattern [ ESCAPE escape_character ]
In your case (untested):
WHERE ... LIKE '%/\_Description' ESCAPE '\'
This syntax is shared by SQL Server and Oracle.
you need to understand how the LIKE works:
// return rows like text111, text222, text333 etc
select * from users where username like 'text%';
// return rows like 111text, 222text, 333text etc
select * from users where username like '%text';
// return rows like 111text111, 222text222, 333text333 etc
select * from users where username like '%text%';
You have to escape the _ properly as it's a placeholder for like too. Based on your DBMS it should be ! so your statement could look like %/!_Description
the _ char is a special jolly character in Oracle. That's why the rows returned are not as you expected. If this query is generated by a program you can have a substitute the _ with [_].
Alternatively, you can use the REPLACE function in oracle:
REPLACE( string1, string_to_replace [, replacement_string] )
in your case
REPLACE( string_var, '_', [_] )
be aware that this may be a bottleneck if your function is called too many times, so I suggest to use an escape character.
example:
select REPLACE( 'aaaa_bbb', '_', '[_]' ) from dual

Oracle db query - data format issue

I'm wondering if there is a way to query the oracle db against formatted field value.
Example:
I have a table of postcodes stored in the format of "part1 part2". I want to be able to find a postcode either by searching it using the above format or "part1part2" format.
What I was thinking is to format the entered postcode by removing the spaces and then query the database like:
SELECT *
FROM POSTCODES_TBL t
WHERE t.postcode.**Format(remove spaces)** = 'part1part2'
The Format(remove spaces would convert the postcode from "part1 part2" to "part1part".
My question is, is it possible?
You can use regexp_replace
SELECT *
FROM POSTCODES_TBL t
WHERE regexp_replace(t.postcode,'\s', '') = 'part1part2'
This will remove any whitespace (space, tab, newlines etc)
or if you only want to get rid of spaces, replace will work just as well:
SELECT *
FROM POSTCODES_TBL t
WHERE replace(t.postcode,' ', '') = 'part1part2'
More details in the manual:
replace
regexp_replace
You could use like
SELECT *
FROM POSTCODES_TBL t
WHERE t.postcode like 'part1%part2'

Using regexp_replace in update

I have a db column report_name which will have values similar to this
000007091_PaymentRegisterReport _D x3A 975844_2012-12-26.XLS
I need to delete the space before _D in all PaymentRegisterReport with XLS extension.
Could somebody help me with the regex to use inside regexp_replace function in the update statement?
Do you really need a regex to update the data? Please check the query.
update TableName
set report_name=REPLACE(report_name, ' _D' , '_D')
WHERE report_name LIKE '%PaymentRegisterReport %' AND
report_name LIKE '%.XLS';
The expression that you need to use is
REGEXP_REPLACE(f1, '(.*)(_PaymentRegisterReport) _D (.*)(\.XLS)$', '\1\2_D\3\4')
I am assuming that you can identify the report type by 'PaymentRegisterReport' and the file extensions will be in uppercase
You can replace the " _D" bi "_D" with a select as said techdo.
But I wrote a regex_replace as you was asking :
select regexp_replace('000007091_PaymentRegisterReport _D x3A 975844_2012-12-26.XLS','^(.*) _D(.*).XLS$','\1_D\2.XLS') from dual;

SQL Query: data with comma

Im using sql server 2005. i want data with comma. for example ['5000'],['5001'],..
but the last record should not include comma. Pls help me.
Query:
select '['''+convert(varchar,parcelid)+'''],' from sampletable
Try the COALESCE function
SELECT #groupedText = COALESCE(#groupedText, '') + [Text] + ','
FROM Requirement
WHERE CampaignId = #campaignId
ORDER BY [Text]
Then you could try one of the string functions to kill the end comma
T-SQL string functions
You can use regular expressions to remove the last comma or do it using your programming language (ASP etc. like a chop function or something).
http://weblogs.sqlteam.com/jeffs/archive/2007/04/27/SQL-2005-Regular-Expression-Replace.aspx
Consider using XML for this purpose. The "aggregate concatenation" solution may not be reliable, because it is not clearly documented and supported. You can get rid of the final comma with SUBSTRING, as boon suggested.
See this thread.