What's wrong with this VB.NET Substring call? - vb.net

dim dataType as String
toolTip="Marks And Number[String]"
I want to get the [String] alone.
dataType = toolTipText.Substring(toolTipText.IndexOf("[") + 1, toolTipText.IndexOf("]") - 1)
it shows an error. Regarding the length of the string.
What's wrong with my code.I dont know ,
Some times I have these type of problems . Standing with simple loops or conditions .

The second parameter is length, not ending index. You need to subtract your starting index from it.

Not that it's great code but
dataType = toolTip.Substring(toolTip.IndexOf("[") + 1, toolTip.Length - toolTip.IndexOf("[") - 2)
would sort you out.
The second parameter is the length of the substring - not the end index.
Might be better to take a look at regex.

Related

Problem with using SUBSTRING and CHARINDEX

I have a column (RCV1.ECCValue) in a table which 99% of the time has a constant string format- example being:
T0-11.86-273
the middle part of the two hyphens is a percentage. I'm using the below sql to obtain this figure which is working fine and returns 11.86 on the above example. when the data in that table is in above format
'Percentage' = round(SUBSTRING(RCV1.ECCValue,CHARINDEX('-',RCV1.ECCValue)+1, CHARINDEX('-',RCV1.ECCValue,CHARINDEX('-',RCV1.ECCValue)+1) -CHARINDEX('-',RCV1.ECCValue)-1),2) ,
However...this table is updated from an external source and very occasionally the separators differ, for example:
T0-11.86_273
when this occurs I get the error:
Invalid length parameter passed to the LEFT or SUBSTRING function.
I'm very new to SQL and have got myself out of many challenges but this one has got me stuck. Any help would be mostly appreciated. Is there a better way to extract this percentage value?
Replace '_' with '-' to string in CHARINDEX while specifying length to the substring
'Percentage' = round(SUBSTRING(RCV1.ECCValue,CHARINDEX('-',RCV1.ECCValue)+1, CHARINDEX('-',replace(RCV1.ECCValue,'_','-'),CHARINDEX('-',RCV1.ECCValue)+1) -CHARINDEX('-',RCV1.ECCValue)-1),2) ,
If you can guarantee the structure of these strings, you can try parsename
select round(parsename(translate(replace('T0-11.86_273','.',''),'-_','..'),2), 2)/100
Breakdown of steps
Replace . character in the percentage value with empty string using replace.
Replace - or _, whichever is present, with . using translate.
Parse the second element using parsename.
Round it up to 2 digits, which will also
automatically cast it to the desired numeric type.
Divide by 100
to restore the number as percentage.
Documentation & Gotchas
Use NULLIF to null out such values
round(
SUBSTRING(
RCV1.ECCValue,
NULLIF(CHARINDEX('-', RCV1.ECCValue), 0) + 1,
NULLIF(CHARINDEX('-',
RCV1.ECCValue,
NULLIF(CHARINDEX('-', RCV1.ECCValue), 0) + 1
), 0)
- NULLIF(CHARINDEX('-', RCV1.ECCValue), 0) - 1
),
2)
I strongly recommend that you place the repeated values in CROSS APPLY (VALUES to avoid having to repeat yourself. And do use whitespace, it's free.

How to search a string in list in sql?

I have been using sql for quite a time but unable to figure out below query logic.
I'm extracting two values
First_name i.e abc
FIRST_NAMES_LIST (list containing first names) i.e ['abc','abc','cba','dba'] (this may contain junk values also in between strings)
I trying to search first_name in first_name_list and return 1 or 0, using below logic
CASE FIRST_NAME in FIRST_NAMES_LIST then 1
else 0
but this isn't giving correct result
Can somebody please help.
Thanks,
Naseer
Look this information:
https://www.techonthenet.com/oracle/functions/instr.php
INSTR is a function which return <> 0 if your parameter match. If not it return 0.
I no have any clear example in your ennunciate to give you the correct answer. See the functionalities.
Regards!
Ideally you would parse out your json into a table object ( I don't know how to do that in Oracle) and then search your table object where the object contains the value, but that is pretty expensive. It would be more robust and would be able to handle special characters/corner cases better.
On the other hand, if the names are going to stay simple (ie, no quote marks ' or commas), you could use a LIKE expression and search the string.
CASE WHEN FIRST_NAMES_LIST LIKE '%''' + FIRST_NAME + '''%' THEN 1 ELSE 0 END
Yes im currently using INSTR but query is taking bit of time. hope resolves this issue. thanks.

Operator does not exist: text + integer [Postgres 9.5]

i have an issue with this piece of code coming from SQL :
UPDATE resultats_du_jour
SET Heure_debut = CONCAT(SUBSTRING(Heure_debut,1,2) +
12,SUBSTRING(Heure_debut,3,3))
WHERE Heure_debut LIKE '%PM';
This gives me the following output :
sql:53: ER ROR: operator does not exist: text + integer LINE 1: ...ET Heure_debut = CONCAT(SUBSTRING(Heure_debut,1,2)+12,SUBSTR...
I understand that you cannot add text+integer, but how can i proceed to do that?
Many thanks.
If you want to add 12 to the numerical equivalent of the first substring you take, and then concatenate it again back to text, then you may use casts:
UPDATE resultats_du_jour
SET Heure_debut = CONCAT((SUBSTRING(Heure_debut, 1, 2)::int + 12)::text,
SUBSTRING(Heure_debut, 3, 3))
WHERE Heure_debut LIKE '%PM';
This feels a bit hackish, and in general as a matter of good design you should decide whether a certain type of data is text or a number. Here, if you had things stored as numbers, you might not need to cast at all.

SQL Query - Greater Than with Text Data Type

I've searched around and couldn't find an answer anywhere.
I'm querying a database that has stored numbers as a VARCHAR2 data type. I'm trying to find numbers that are greater than 1450000 (where BI_SO_NBR > '1450000'), but this doesn't bring back the results I'm expecting.
I'm assuming it's because the value is stored as text and I don't know any way to get around it.
Is there some way to convert the field to a number in my query or some other trick that would work?Hopefully this makes sense.
I'm fairly new to SQL.
Thanks in advance.
If the number is too long to be converted correctly to a number, and it is always an integer with no left padding of zeroes, then you can also do:
where length(BI_SO_NBR) > length('1450000') or
(length(BI_SO_NBR) = length('1450000') and
BI_SO_NBR > '1450000'
)
You can try to use like this:
where to_number(BI_SO_NBR) > 1450000
Assuming you are using Oracle database. Also check To_Number function
EDIT:-
You can try this(after OP commented that it worked):
where COALESCE(TO_NUMBER(REGEXP_SUBSTR(BI_SO_NBR, '^\d+(\.\d+)?')), 0) > 1450000
If you are talking about Oracle, then:
where to_number(bi_so_nbr) > 1450000
However, there are 2 issues with this:
1. if there is any value in bi_so_nbr that cannot be converted to a number, this can result in an error
2. the query will not use an index on bi_so_nbr, if there is one. You could solve this by creating a function based index, but converting the varchar2 to number would be a better solution.

How can I return a substring based on CharIndex for starting and end points from a field in a table?

I've been working on this problem most of the morning and I think I have the solution mostly but I must have something wrong somewhere.
I have a table that keeps as a column a long html file.
I need to search that column and return a part of that html file in mssql but I do not have index numbers to use.
My query so far:
SELECT SUBSTRING(ColName, CHARINDEX('TxTStart', ColName), CHARINDEX('TxTEnd', ColName))
FROM htmlTable ht
WHERE ht.Date_ = '2009-01-01'
My thinking is that the length in this query will not be correct to obtain the whole substring from txtStart to TxtEnd.
Should I possibly use RTRIM, just take all the string and then cut off what I need after ?
Should the above actually work and I just have something else wrong?
Or am I completely off base here?
Thanks in advance for any help or hints.
The 3rd parameter is length, so you need to subtract the end and start to get the length. try this
SELECT SUBSTRING(ColName, CHARINDEX('TxTStart', ColName), CHARINDEX('TxTEnd', ColName) - CHARINDEX('TxTStart', ColName))
FROM htmlTable ht
WHERE ht.Date_ = '2009-01-01'