Right. So I've created a stored procedure in a MySQL DB which happens to use SUBSTRING.
Running the procedure via a query gives me:
SQL Error 1630: Function mydatabase.SUBSTRING does not exist
Beg your pardon?
Is there a space after the method call to Substring before the first parenthesis?
It appears on Line 40:
IF i > 1 AND j > 1 AND (s1_char = SUBSTRING (s2, j - 1, 1))
i.e. Ensure
select substring(CustomerName, 1, 4) AS CustName from MyTable;
instead of:
select substring (CustomerName, 1, 4) AS CustName from MyTable;
Related
I have this filename AAAA_BBBBB_CC_HDDD_HGGG.csv and I'm trying to keep the values after the second underscore starting from the right.
So i want to keep any values just before _HDDD_HGGG.csv
This is my code:
SET #NFileN = REVERSE(SUBSTRING(REVERSE(#source_filename),1,CHARINDEX('_',REVERSE(#source_filename), CHARINDEX('_', REVERSE (#source_filename), 0) + 1)))
And this is the returned value:
(6 rows affected)
_HDDD_HGGG.csv
Instead of being AAAA_BBBBB_CC.
Does anyone has a clue for this?
You are taking a SUBSTRING from 1 till your CHARINDEX while your string is reversed. Either reverse your string again or use LEN to find the length of your string like so:
REVERSE(
SUBSTRING(
REVERSE(#source_filename),
CHARINDEX('_',
REVERSE(#source_filename),
CHARINDEX('_',
REVERSE (#source_filename),
0)+1)+1,
LEN(#source_filename)
)
)
p.s.: Added a second +1 to remove the "_" between CC and HDDD
p.p.s: CHARINDEX is a SQL Server function which I assume is what you are actually using. The MySQL equivalent would be POSITION, the equivalent for LEN would be LENGTH
E:g
Case 1: delimeter not exist
string = "abcdefg"
mysql> select substring_index(string,'z',1) from table;
output: abcdefg
expected output : blank string ""
Case 2: delimeter exist
string = "abczdefg"
mysql> select substring_index(string,'z',1) from table;
output: abc
expected output : abc (same as substring returns)
I want this select query should return me blank string if 'z'(delimeter) is not exist
and if delimeter exist in string then whatever substring_index is regular functionality
what modification in Query i can do for my expected output ?
Substring_index does exactly what it should :)
For your task you can use substring
set #str = "abcdef";
select substring(#str, 1, LOCATE('z', #str) - 1) from dual;
That will do exactly what you want
I'm attempting to select a part of a strint between 2 values, i've managed to get it working to about 90% but then get an error -
SUBSTRING(TranText, CHARINDEX('x', TranText) + 1, LEN(TranText) - CHARINDEX('x', TranText) - CHARINDEX('/', REVERSE(TranText)))
The field it is querying is like so
Start Date : 01/02/2013 50 x 156.00/MX + 207.64
with the desired result being
156.00
Now I think the issue is because sometimes the X can have a space before or after it, or no space at all. It gets through about 114,000 rows before throwing
Invalid length parameter passed to the LEFT or SUBSTRING function.
But am struggling to resolve.
The main root cause could be because LEN(#QRY) - CHARINDEX('x', #QRY)-CHARINDEX('/', REVERSE(#QRY)) is less than zero ie, negative value. This in turn will throw error in SUBSTRING since the minimum index for SUBSTRING is zero. Therefore we check that condition in a case statement.
SELECT CASE WHEN LEN(TranText) - CHARINDEX('x',TranText)-CHARINDEX('/', REVERSE(TranText)) >= 0 THEN
SUBSTRING(TranText, CHARINDEX('x', TranText) + 1, LEN(TranText) - CHARINDEX('x', TranText) - CHARINDEX('/', REVERSE(TranText)))
ELSE NULL END
FROM YOURTABLE
Try this. Used REPLACE function and then parsed.
DECLARE #string AS VARCHAR(100), #result AS VARCHAR(100)
DECLARE #nStart AS int
SET #string = 'Start Date : 01/02/2013 50 x 156.00/MX + 207.64'
SET #result = REPLACE(#string, '/', '')
SET #nStart = CHARINDEX('x', #result) + 1
SET #result = SUBSTRING(#result, #nStart, CHARINDEX('M',#result) - #nStart)
SELECT #result
I found this sql query online , i did work but am not quite able to parse it .
i haven't used any queries with '#' or " := "
if some one could explain me what it means and which topic it comes under , it would help me a lot ..
select (select (#) from (select(#:=0x00),(select (#) from (information_schema.columns) where (table_schema>=#) and (#)in (#:=concat(#,0x3C,0x62,0x72,0x3E,' [ ',table_schema,' ] > ',table_name,' > ',column_name))))a)#
First of all i would make the query a litte bit more readable by reformatting it:
1) SELECT (SELECT (#)
2) FROM (SELECT (#:=0x00),
3) (SELECT (#)
4) FROM (information_schema.columns)
5) WHERE (table_schema >= #)
6) AND (#) IN (#:=CONCAT(#,0x3C,0x62,0x72,0x3E,' [ ',table_schema,' ] > ',table_name,' > ',column_name))
7) )
8) )
9) a);
The assignment of # is as follows:
In Line 3 it gets the value 0x00 (Decimal: 0)
In line 5 this value is used for the greater than (table_schema >= 0)
Line 6 is a way to concat each schema, table and column name into #
# is returned in line 1 and contains a concatenated list of your structures
In line 6 an additional <br> (0x3C,0x62,0x72,0x3E) is added to the variable to make the output more readable
SELECT (0,14285714285714285714285714285714*(5 - 2) + 2)
What does this mean?
The actual line giving me trouble is:
SET #a13 = (0,14285714285714285714285714285714*(#a - #tmpv) + #tmpv)
Both #a and #tmpv have been calculated previously in the trigger
Any insight would be helpful
There is a syntax error in your query: You use the comma as a decimal separator. You have to use a dot instead. This works just fine:
SELECT ( 0.14285714285714285714285714285714 * ( 5 -2 ) +2 )