SQL: split a variable

Status
Not open for further replies.

mr_b0m

Ars Scholae Palatinae
654
Hello

I'm sending an INSERT that looks something like this, today
Code:
INSERT INTO table_name (ID, JobNo, Volume) VALUES (@'%1', '@%2', '@%3')
@%1-3 are variables that's replaced by our system. So far so good.

Here's the tricky part. A new requirement is to send a part of the JobNo (@%2), to another field as well, that we might call LineNo. For example, our system sends '12345.01' as variable @%2, and I want the '12345' part of it to go into JobNo and the '01' to go into LineNo. Does that make any sense? I haven't touched SQL before so I'm a bit lost. I've googled and some kind of string split function exists but I'm not sure on how to apply it here, if it's even possible. Mind you, I'm just trying the easy way here, if this doesn't work we'll go the long way around and make sure we have two different inputs for JobNo and LineNo.
 

C0rinthian

Ars Tribunus Angusticlavius
6,858
assuming worst case scenario where you don't know how long each section of the value is, you'll need to use instr to find positions and then substr to pull out portions.

So to pull out the first one, you use instr to find the position of the '.' and then use that to determine the length of the substr to extract:
Code:
substr( '12345.01' , 1 , instr( '12345.01' , '.' )- 1 )

And then for the second half, you do something similar, but working from the '.' to the end.

Code:
substr( '12345.01' ,  instr( '12345.01' , '.') - length( '12345.01' ))

instr syntax: instr('String_to_search', 'String_to_find', Start_position, occurrence)
substr syntax: substr('String', start_position, length)

Using a negative value for the start positions starts from the end and counts back. (So a start position of -4 starts 4 characters from the end of the string)
 

Fristie Blade

Ars Tribunus Militum
1,722
Subscriptor++
And to maximize the fun in your change request (and guarantee correct behavior in the old situation) I'd wrap the 2 formulas that C0rinthian wrote in a CASE block that checks if there is at least one occurrence of "." in the value you have inbound.
No occurrence: Entire string to jobNo and null into lineNo
Otherwise: First part to JobNo and second part to lineNo

One remark: The formula where you calculate the second part as counting from the right will only work correctly if there is exactly one occurrence of ".". With "123.45.67" as inbound data, it will split into "12" and "67". I'd change the first index of the substr function from "1" to the position of the "." occurrence + 1 and work to the end of the inbound value. That way a value like "123.45.67" would be split into "123" and "45.67".

Can sound like nitpicking, but since you implement the splitting logic (and not the team that supplies you the values), you'd better check for boundary cases in your logic.
In the best case when some wrong data is supplied like the "123.45.67", you have not lost any data and if they observe the unexpected "45.67" in the lineNo, then you can point them to incorrect inbound data.
Otherwhise, the invalid data might even go unnoticed.

Probably overkill, but I've been integrating data flows between different systems for too long and have developed an aversion for BIBO systems ;)
 

C0rinthian

Ars Tribunus Angusticlavius
6,858
Good points.

Also realized my second bit of code was inaccurate. (Corrected now) Regardless of where you start in the string, it then counts toward the end of the string. So if you do something like:
Code:
instr ( 'ThisIsAString', -4, 4)
it would return 'ring'

If you omit the length, it goes from the start point to the end of the string. So in your example, we can do:

Code:
substr( '12345.01' ,  instr('12345.01', '.') - length('12345.01'))

Which finds the position of the first '.' (6) and subtracts the total length of the string from that (8) giving you a starting position of -2, and then everything from that position to the end. '01'

This also addresses what Fristie mentions with the possibility of there being more than one '.' in the string, as everything works from the first occurrence.
 

mr_b0m

Ars Scholae Palatinae
654
Interesting. I do know that there will be only one '.' occurrence, and I know that the string lengths won't be changing.

However, I'm not sure how to use the instr command, can I nest it in together with the INSERT like this?

Code:
INSERT INTO table_name (ID, JobNo, LineNo, Volume) VALUES ('@%1', 'substr( '@%02' , 1 , instr( '@%02' , '.' )- 1 )',  substr( '@%02' ,  'instr( '@%02' , '.') - length( '@%02' ))', '@%3')

Feel free to smack me if it's a stupid question. :eek:
 

Fristie Blade

Ars Tribunus Militum
1,722
Subscriptor++
Sure you can do that. But don't place single quotes around the second value you insert. It is not a literal value, but a formula, so no quote before the "substr" and no quote at the end of it. Otherwhise you'd be trying to insert literally the "substr" string instead of calculating something.
Also, this statement will fail if %1, %2 or %3 contain single quotes inside their value.
From what environment do you execute that statement? If you can generate the statement and have it use bound parameters instead, you'd be off much safer.
 

mr_b0m

Ars Scholae Palatinae
654
The environment is fairly simple. This is for a computer managed fluid dispensing system and everytime someone makes a dispense, it gets sent to our DB, but in addition to that it also gets sent to the customer's DB, configured with an INI file containing the INSERT string (ForwardTransaction="INSERT blah blah"). So there's not any real coding involved at this stage, just modification of the string.
 
Status
Not open for further replies.