How can I insert a character into a string? (VBA) - vba

I have a column of product numbers that are all formatted like this:
MK444LLA
...same number and letter pattern, same character count. I need to insert a / into each cell so they all end up like this:
MK444LL/A
I'm thinking I just need a solution for the first row, which I can then apply to the entire column.

Use the Left and Right string functions and concatenate the three parts together with &.
Left(Range("A1").Text, 7) & "/" & Right(Range("A1").Text, 1)
Left(Range("A1").Text, 7) - this returns the first seven characters.
Right(Range("A1").Text, 1) - this returns the last character.

Related

Remove numbers from beginning of strings in a numbered list with SQLite

I have a long numbered list imported into a table, the strings are in the following format:
1. fdhsglahs sdhkgs
2. urgbvdgh ndovh
3. 8yhbnxjghr nvdfo dfhioj
...
9999. vnur neeu nu
I want to remove the numbers in the beginning of the string, the "." adjacent to the number, and any number of spaces that come immediately after the "." and before the next character (that is, before the beginning of the string itself).
Can't find a method to do that in SQLite.
Please notice, some of the strings contain numbers as part of the string, which are not to be removed.
For this requirement you can use string functions like substr(), instr() and ltrim():
select ltrim(substr(col, instr(col, '.') + 1))
from tablename
Replace col with the column's name.
this code returns the part of the string after the . left trimmed of spaces.
See the demo.
If you want to update the table:
update tablename
set col = ltrim(substr(col, instr(col, '.') + 1));
See the demo.

What does the trim function mean in this context?

Database I'm using: https://uploadfiles.io/72wph
select acnum, field.fieldnum, title, descrip
from field, interest
where field.fieldnum=interest.fieldnum and trim(ID) like 'B.1._';
What will the output be from the above query?
Does trim(ID) like 'B.1._' mean that it will only select items from B.1._ column?
trim removes spaces at the beginning and end.
"_" would allow representing any character. Hence query select any row that starts with "B.1."
For eg.
'B.1.0'
'B.1.9'
'B.1.A'
'B.1.Z'
etc
Optional Wildcard characters allowed in like are % (percent) and _ (underscore).
A % matches any string with zero or more characters.
An _ matches any single character.
I don't know about the DB you are using but trim usually remove spaces around the argument you give to it.
The ID is trimmed to be sure to compare the ID without any white-space around it.
About your second question, Only the ROWS with an ID like 'B.1.' will be selected.
SQL like
SQL WHERE

Using SQL to make specific changes in a database.

I am trying to figure out some commands/code in SQL.
I have database with names, addresses IDs etc, but I have to convert firstname values ending in “jnr” to “(Jnr)” and those ending in “snr” to “(Snr)”.
How do I do this?
update table TABLE_NAME set NAMES = '*xyz*Jnr' where NAMES like '%jnr'
Update or select:
PASTE(column, CHAR_LENGTH(column)-3, 1, UPPER(SUBSTRING(column FROM CHAR_LENGTH(column)-3 FOR 1)
WHERE column LIKE '%jnr' OR column LIKE '%snr'
PASTE is used to put in one character at position 3 from end,
CHAR_LENGTH to get length of column value,
UPPER converts character to upper case,
SUBSTRING is used to pick one character here (j or s),
LIKE is used to find values ending with jnr, or snr.
All ANSI SQL (no dbms specified!)

copy part of one column to another column in the same table

I need to copy part of one column into another column. The delimiter is "-". I don't want to remove that part from the first column.
Example:
ItemDesc Part#
Glowear_black-1234
So it needs to look like this:
ItemDesc Part#
Glowear_black-1234 1234
The only SQL query I can find cuts the information from the ItemDesc column and pastes it into Part#. I still need the "1234" in the first column. Also not all of the ItemDesc have a "-" (which is fine).
In Access SQL, you can use InStr() to find the position of "-" within your field. Then you can use Mid() to return the substring which starts one character after that position.
Note you must bracket Part# in your SQL statement because of the # character in its name.
UPDATE tblIOT1
SET [Part#] =
Mid(ItemDesc, InStr(1, ItemDesc, "-") + 1)
If ItemDesc could be Null or contain a string without a "-", add a WHERE clause to ignore those rows in the UPDATE.
WHERE ItemDesc ALike '%-%'
It is not explicitly clear what to do for the case where ItemDesc lacks the hyphen symbol as a delimiter, but here is my suggestion. Use the substring function to grab everything to the right of the hyphen, where everything to the right is bound by the index of the hyphen character plus one, and the length of the string:
update user.table
set Part# = substring(ItemDesc, (CHARINDEX('-', ItemDesc)+1), LEN(ItemDesc) )
Taking your statement from the comments:
UPDATE tblIOT1
SET Part#= Trim(Mid(ItemDesc, InStr(ItemDesc, '-') +1))
where ItemDesc like "*-*"
Note that I am using the star character and the other respondent uses the percent character, either appear to be valid to MS Access as specified here

Split string and replace

I have a varchar column with Url's with data that looks like this:
http://google.mews.......http://www.somesite.com
I want to get rid of the first http and keep the second one so the row above would result in:
http://www.somesite.com
I've tried using split() but can't get it to work
Thanks
If you are trying to do this using T-SQL, you can try something in the lines of:
-- assume #v is the variable holding the URL
SELECT SUBSTRING(#v, PATINDEX('%_http://%', #v) + 1, LEN(#v))
This will return the start position of the first http:// that has before it at least one character (hence the '%_' before it and the + 1 offset).
If the first URL always starts right from the beginning of the string, you can use SUBSTRING() & CHARINDEX():
SELECT SUBSTRING(column, CHARINDEX('http://', column, 2), LEN(column))
FROM table
CHARINDEX simply searches a string for a substring and returns the substring's starting position within the string. Its third argument is optional and, if set, specifies the search starting position, in this case it's 2 so it didn't hit the first http://.