Locate, Substring query - mysql

I am trying to pull the data from between 2 string points in mysql, my example script would be
'otherdata&p1=textneeded&otherdata'
I need to pull the "textneeded" bit, "P1=" is the start position and will only appear once within the string but the "&" sign can appear multiples times. If there is nothing between these 2 points just return a blank. Where I have put otherdata, this can be a varying number of things on either end of the points I am trying to extract the data from. I am no expert at all on mysql so any help would be appreciated.
Thanks

You may use MySQL's locate() function to get the position of p1= substring and then the position of the next & and use substr() to extract the string between the two. +3 and -3 are to compensate for the length of p1=:
set #s='otherdata&p1=textneeded&otherdata';
select substr(#s,locate('p1=',#s)+3,locate('&',#s,locate('p1=',#s))-locate('p1=',#s)-3)

Related

type error when trying to update a table field name query

I have a query where i am trying to update field by taking the left number of characters before a space.
I'm not very good with Access VBA, so I'm trying to do this via a query.
my data is a list of SKUs, where I want to update the same field (sku) with a shorter SKU number, by using the Left$ function along with the InStr function to take all characters to the left of a space in the number.
test sku
E349CAJ6 OBROBRO
E357CAJ6 OBROSID
E329CAJ6 OWHIBRO
E358CAJ6 ONO SID
Note that the space isn't always in position 9, sometimes it varies. I was trying to use the following Query update value: Left$([IMPORT - EFF ORDERS]![SKU], InStr([IMPORT - EFF ORDERS]![SKU]," ",1))
The InStr, identifies the starting position based on the space, to use for the Left function.
The SKU field is a Short Text type field.
However, when I run the query, I get a "Type Conversion" error and none of the records will update.
I have wracked my brain to try to figure this one out and would appreciate an expert's fresh eyes on it.
Thank you so much in advance !
The ,1 is in wrong argument, really don't need it. If you want to use Compare argument then also use Start argument. Without explicit Start and Compare parameters, function will use defaults.
Left([IMPORT - EFF ORDERS]![SKU], InStr(1, [IMPORT - EFF ORDERS]![SKU], " ", 1))
InStr() in this case is returning a value that will be used as a length parameter, not a starting position. Start position for Left is first character.

Format Function returns wrong value

We have a field in a query that should be left-padded with zeroes if it is too short, and we accomplish this using the Format() function. However, there are some values that produce bizarre results.
Format("14425112-8","00000000-00")
Returns the value "00019330-78"
For most inputs, the string gets formatted as expected, 8 digits, hyphen, two digits. But in a few rare cases, the value is modified. Is this repeatable for anyone else? Does anyone have an explanation?
Thanks for your help.
This is an example of access trying to be too helpful. It looks like it is interpreting these values as dates, but since you didn't use any date indicators in the format e.g: (dd,mm,yyyy), it converted 1-1 to a date, and then tried to display it in decimal form:
debug.print Format("1-1","000000-00")
returns 000427-36 which is the decimal value 42736 which, if you convert to a date, becomes 1/1/2017. This is what access interpreted "1-1" as.
it seems that access has reserved the - character as symbolizing a date format, despite what their website says. This function is only useful for formatting actual dates, or numeric values, such as prices. If you are set on using the format function, you will have to change you separator to a decimal point, which apparently is the only character that will get you what you want with the leading and trailing zeros.
Otherwise, you may have to build your own function for this.
You cannot format a string like a number this way. Try this:
PaddedNumber = Right(String(8, "0") & "14425112-8", 10)

Substring in flex3

I want to use substring in flex side like eg:- m having sm no like 0001A,0287393A,22839A,etc
i need to use substring for numeric only and chop character value. Or else if you have sm other idea beside using substring can share much appreciated
I think I understand the question. You want to convert the numeric part of the string to a Number, and drop the characters. If so, you're in luck:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/package.html#parseInt()
parseInt('0001a') should return Number(1).

Using mysql how to find 1st occurrence of a alphabet in a string

Using mysql I need to find the position of the 1st occurrence of any alphabet in a sub string.
For example if my string is like 123456A12345 then I need to find the position of A.
My main goal is to get all the digits just before the alphabet.
Thanks in advance.
If you want to get all the digits, just convert the string to an integer by adding 0:
select '12345abc'+0
returns 12345.
This does not handle leading 0s. And, it won't work for any length expression, but it may solve your problem quickly.

Are there markers for the start and end of a string in MySQL?

Basically I'm trying to remove a value (val2 for example) from a string that looks like 'val1,val2,val3'. However the values can be arranged in any order and I want to avoid ending up with stuff like ',val1,val3', 'val1,,val3' or 'val1,val3,'. I thought about replacing 'val2' with '' and then replacing ',,', ',end' and 'start,' with ','. But I don't know what the markers for end and start of a string are.
Storing multiple values in one single column is never a good idea. You should think about redesign your data model.
You can get what you want with regular expressions, see MYSQL documentation. There are two characters ^ and $ that match beginning and end of a string.