do you know how to remove a text between two words in notepad ++
VALUES (1, NULL, NULL,
VALUES (2, NULL, NULL,
VALUES (3, NULL, NULL,
to
VALUES (NULL, NULL,
If you just want to remove the first entry from each VALUES clause, then try the following find and replace, in regex mode:
Find: VALUES \([^,]+,\s*
Replace: VALUES (
Check the demo link below to a working example.
Demo
Ctrl+H
Find what: VALUES \(\K[^,]+, ?
Replace with: LEAVE EMPTY
check Wrap around
check Regular expression
Replace all
Explanation:
VALUES \( # literally
\K # forget all we have seen until this position
[^,]+ # 1 or more not comma
, ? # a comma followed by an optional space
Result for given example:
VALUES (NULL, NULL,
VALUES (NULL, NULL,
VALUES (NULL, NULL,
Screen capture:
resolved
VALUES.*?NULL
VALUES\(NULL
Related
File looks like:
INSERT INTO x VALUES (48394, '9-10-2007', 19);
INSERT INTO x VALUES (99981, '3-5-2008', 45);
I would like to replace each line with:
INSERT INTO x VALUES (48394, STR_TO_DATE('9-10-2007', %d-%m-%y), 19);
INSERT INTO x VALUES (99981, STR_TO_DATE('3-5-2008', %d-%m-%y), 45);
I can't seem to find how to deal with changing string names to replace
:%s/<WHAT GOES HERE>/add in STR_TO_DATE(...)/
If your data is structured exactly like that with no other strings delimited by ' and the contents are always the date you want to convert, searching for simply '.*' will work:
:%s/'.*'/STR_TO_DATE(&, %d-%m-%y)
To be more specific, i.e. if other strings appear on the same line:
:%s/'\d*-\d*-\d*'/STR_TO_DATE(&, %d-%m-%y)
Here's an example of a solution:
:%s/\(INSERT INTO x VALUES (.*,\) '\(.*\)'\(.*\)/\1 STR_TO_DATE('\2', %d-%m-%y)\3/g
Relevant reading
So, I'm using Excel as a query builder (lots of us have been there), but I have a problem. Some of the columns, which have empty data, have to be inserted as NULL, so the cleanest way to do this that occurred to me was to use the SUBSTITUTE formula to find those empty spots and change them for NULLs.
This is what I have, as an example:
INSERT INTO blah (meh1, meh2, meh3, meh4, meh5, meh6) VALUES (14, '', '', '', '', 5)
And this is the formula I use:
=SUBSTITUTE(*cell*;", ,";", NULL,")
This, in my head, should change all the appearances of ", ," to ", NULL,", resulting in something like this:
INSERT INTO blah (meh1, meh2, meh3, meh4, meh5, meh6) VALUES (14, NULL, NULL, NULL, NULL, 5)
But in the end, what I get is...
INSERT INTO blah (meh1, meh2, meh3, meh4, meh5, meh6) VALUES (14, NULL, '', NULL, '', 5)
Does any of you know why this happens and how to solve it?
I have made a workaround by using 2 SUBSTITUTEs, but it feels sloppy.
If you want to test this yourselves with something cleaner, try this:
Original cell content:
a, a, a, a, a, a, a
Substitute function:
=SUBSTITUTE(*cell*;", a,";", B,")
And this is what you will get:
a, B, a, B, a, B, a
Just in case, I got this solved by omitting the first comma in the search pattern.
This is, instead of looking for ", ," I searched for " ," and it worked fine.
In this case, there was no other way that a " ," would appear in the text, so I'm going with that.
I'm trying to pull the string to the right of the last forward slash in the string below.
/Applied Analytics/URMFG/Service Analysis/ServiceAnalysis
So basically, I would like to see ServiceAnalysis returned.
I've come across the following SQL, which is close to what I need, but it's not exact.
=MID(K19, FIND("/",K19)+1, LEN(K19))
DECLARE #test NVARCHAR(100)
SET #test = '/Applied Analytics/URMFG/Service Analysis/ServiceAnalysis'
SELECT REVERSE(LEFT(REVERSE(#test), CHARINDEX('/', REVERSE(#test)) -1))
Reverse the String and find first instance of /
Find characters to the left of /
Reverse again to get your desired result
In SQL, you could do this:
declare #string varchar(100) = '/Applied Analytics/URMFG/Service Analysis/ServiceAnalysis';
select RIGHT(#string,charindex('/',reverse(#string),1)-1)
However, still waiting to see if it's EXCEL you're referencing (since that looks like an EXCEL formula).
If it is Excel, then you can use the Reverse() function from this post and apply it like this:
Here's the formula:
=Reverse(LEFT(Reverse(A1),FIND("/",Reverse(A1),1)-1))
Regular Expressions to the rescue! you can achieve this using the RXReplace() function:
RXReplace([column],"^/.*/(.*)$","$1","")
I'll let you look up the RXReplace() documentation on your own, but just to explain the regex itself:
^/ matches the beginning of the string and the starting /
.*/ matches any characters that come next, followed by a / which is the final / before the end of the string (and preceeding the bit that we want to extract)
(.*)$ matches any characters that come next, putting them into a "capturing group" (basically a variable), followed by the end of the string
the $1 is a token which refers to the capturing group above (normally this looks like \1 in regex, but Spotfire is a bit different)
pretty much any time you need to deal with extracting bits of strings in Spotfire expressions, RXReplace() is what you want. it's a lot more sustainable than doing a ton of Left()s, Right()s, and Len()s, although the initial effort can be a bit higher.
more regex info at http://www.regular-expressions.info/.
Another additional approach using PARSENAME() function
DECLARE #String NVARCHAR(100)
SET #String = '/Applied Analytics/URMFG/Service Analysis/ServiceAnalysis'
SELECT PARSENAME(REPLACE(SUBSTRING(#String, 2, 100), '/', '.'), 1) AS [4th part],
PARSENAME(REPLACE(SUBSTRING(#String, 2, 100), '/', '.'), 2) AS [3rd part],
PARSENAME(REPLACE(SUBSTRING(#String, 2, 100), '/', '.'), 3) AS [2nd part],
PARSENAME(REPLACE(SUBSTRING(#String, 2, 100), '/', '.'), 4) AS [1st part]
output
The following SQL Statement worked for me in T-SQL 2017:
SELECT RIGHT([Filename], CHARINDEX('\', REVERSE('\' + [Filename])) - 1)
I have a column with below values,
User_Id=446^User_Input=L307-60#/25" AP^^
I am trying to get each individual value based on a specified key.
All value after User_Id= until it encounters ^
All value after User_Input= until it encounters ^
I tried for and so far I have this,
SELECT LTRIM(REGEXP_SUBSTR('User_Id=446^User_Input=L307-60#/25" AP^'
,'[0-9]+',1,1),'^') User_Id
from dual
How do I get the value for the User_Input??
P.S: User input can have anything, like ',", *,% including a ^ in the middle of the string (that is, not as a delimiter).
Any help would be greatly appreciated..
This can be easily solved using boring old INSTR to calculate the offsets of the start and end points for the KEY and VALUE strings.
The trick is to use the optional occurrence parameter to identify each the correct instance of =. Because the input can contain carets which aren't intended as delimiters we need to use a negative position to identify the last ^.
with cte as (
select kv
, instr(kv, '=', 1, 1)+1 as k_st -- first occurrence
, instr(kv, '^', 1) as k_end
, instr(kv, '=', 1, 2)+1 as v_st -- second occurrence
, instr(kv, '^', -1) as v_end -- counting from back
from t23
)
select substr(kv, k_st, k_end - k_st) as user_id
, substr(kv, v_st, v_end - v_st) as user_input
from cte
/
Here is the requisite SQL Fiddle to prove it works. I think it's much easier to understand than any regex equivalent.
If there is no particular need to use Regex, something like this returns the value.
WITH rslt AS (
SELECT 'User_Id=446^User_Input=L307-60#/25" AP^' val
FROM dual
)
SELECT LTRIM(SUBSTR(val
,INSTR(val, '=', 1, 2) + 1
,INSTR(val, '^', 1, 2) - (INSTR(val, '=', 1, 2) + 1)))
FROM rslt;
Of course, if you can't guarantee that there will not be any carets that are valid text characters, this will possibly return partial results.
Assuming that you will always have 'User_Id=' and 'User_Input=' in your string, I would use a character group approach to parsing
Use the starting anchor,^, and ending anchor, $. Look for 'User_Id=' and 'User_Input='
Associate the value you are searching for with a character group.
SCOTT#dev>
1 SELECT REGEXP_SUBSTR('User_Id=446^User_Input=L307-60#/25" AP^','^User_Id=(.*\^)User_Input=(.*\^)$',1, 1, NULL, 1) User_Id
2* FROM dual
SCOTT#dev> /
USER
====
446^
SCOTT#dev>
1 SELECT REGEXP_SUBSTR('User_Id=446^User_Input=L307-60#/25" AP^','^User_Id=(.*\^)User_Input=(.*\^)$',1, 1, NULL, 2) User_Input
2* FROM dual
SCOTT#dev> /
USER_INPUT
================
L307-60#/25" AP^
SCOTT#dev>
Got this answer from a friend of mine.. Looks simple and works great...
SELECT
regexp_replace('User_Id=446^User_Input=L307-60#/25" AP^^', '.*User_Id=([^\^]+).*', '\1') User_Id,
regexp_replace('User_Id=446^User_Input=L307-60#/25" AP^^', '.*User_Input=(.*)[\^]$', '\1') User_Input
FROM dual
Posting here just in case any of you find it interesting..
I have this strange problem. i have a table with 10 columns of type character varying.
I need to have a function that searches all records and returns the id of the record which has all strings. Lets say records:
1. a,b,c,d,e
2. a,k,l,h
3. f,t,r,e,w,q
if i call this function func(a,d) it should return 1, if i call func(e,w,q) its should return 3.
The function is
CREATE OR REPLACE FUNCTION func(ma1 character varying,ma2 character varying,ma3 character varying,ma4 character varying)
DECLARE name numeric;
BEGIN
SELECT Id INTO name from Table WHERE
ma1 IN (col1,col2,col3,col4) AND
ma2 IN (col1,col2,col3,col4) AND
ma3 IN (col1,col2,col3,col4) AND
ma4 IN (col1,col2,col3,col4);
RETURN name;
END;
It's working 90% of the time, the weird problem is that some rows are not found.
Its not uppercase or lowercase problem.
What can be wrong, its version 9.1 on 64 bit win 7. I feel its like encoding or string problem but i can't see where and what.
//Ok i found the problem, it has to do with all column, if all 24 columns are filled in then its not working ?? but why ? are there limitations becouse there are 24 columns that i must compare with//
Can someone help me plz.
thanks.
The problem is (probably) that some of your columns have nulls.
In SQL, any equality comparison with a null is always false. This extends to the list of values used with the IN (...) condition.
If any of the values in the list are null, the comparison will be false, even if the value being sought is in the list.
The work-around is to make sure no values are null. which unfortunately results in a verbose solution:
WHERE ma1 IN (COALESCE(col1, ''), COALESCE(col2, ''), ...)
I suspect Bohemian is correct that the problem is related to nulls in your IN clauses. An alternative approach is to use Postgres's array contained in operator to perform your test.
where ARRAY[ma1,ma2,ma3,ma4] <# ARRAY[col1,col2,...,colN]