Insert dash after numbers in vargraphic field - sql

I have a field in a db2 database where I need to modify the string from left column (unformatedName) to right format column (formatedName).
Here's an example:
What is the best way to find a number in a string and put a dash after the number? The column may have more then one set of numbers. I.E. There can be numbers in the start of the string, in the middle and in the end.
SELECT
locationcode AS unformatedName,
locationcode AS formatedName
FROM
location
WITH UR;
Regards

Related

Query to retrieve only columns which have last name starting with 'K'

]
The name column has both first and last name in one column.
Look at the 'rep' column. How do I filter only those rep names where the last name is starting with 'K'?
The way that table is defined won't allow you to do that query reliably--particularly if you have international names in the table. Not all names come with the given name first and the family name second. In Asia, for example, it is quite common to write names in the opposite order.
That said, assuming you have all western names, it is possible to get the information you need--but your indexes won't be able to help you. It will be slower than if your data had been broken out properly.
SELECT rep,
RTRIM(LEFT(LTRIM(RIGHT(rep, LEN(rep) - CHARINDEX(' ', rep))), CHARINDEX(' ', LTRIM(RIGHT(rep, LEN(rep) - CHARINDEX(' ', rep)))) - 1)) as family_name
WHERE family_name LIKE 'K%'
So what's going on in that query is some string manipulation. The dialect up there is SQL Server, so you'll have to refer to your vendor's string manipulation function. This picks the second word, and assumes the family name is the second word.
LEFT(str, num) takes the number of characters calculated from the left of the string
RIGHT(str, num) takes the number of characters calculated from the right of the string
CHARINDEX(char, str) finds the first index of a character
So you are getting the RIGHT side of the string where the count is the length of the string minus the first instance of a space character. Then we are getting the LEFT side of the remaining string the same way. Essentially if you had a name with 3 parts, this will always pick the second one.
You could probably do this with SUBSTRING(str, start, end), but you do need to calculate where that is precisely, using only the string itself.
Hopefully you can see where there are all kinds of edge cases where this could fail:
There are a couple records with a middle name
The family name is recorded first
Some records have a title (Mr., Lord, Dr.)
It would be better if you could separate the name into different columns and then the query would be trivial--and you have the benefit of your indexes as well.
Your other option is to create a stored procedure, and do the calculations a bit more precisely and in a way that is easier to read.
Assuming that the name is <firstname> <lastname> you can use:
where rep like '% K%'

SQL Server Search for multiple instances of same text in column

I have a SQL Server table that contains an nvarchar(max) column (MyText) containing sentences. I need to identify all instances of a particular phrase in all rows of the (MyText) column. Once identified I want to replace all instances with different text.
Thanks,
Brad
select cust_div, cust_seral
from [dbo].[lveIntake_closing_scripts]
where close_script like '%LMLSUnit%LMLSUnit.com%'
To count how many instances of the source string is contained within each row, you need to replace each instance with a string that is one character shorter, then subtract that length of the resultant string from the length of the original string. Like this:
select
cust_div
, cust_seral
, len(close_script) - len(replace(close_script, 'LMLSUnit.com','LMLSUnit.co'))
from [dbo].[lveIntake_closing_scripts]
where close_script like '%LMLSUnit%LMLSUnit.com%'

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 a field and add these fields to another table

I have a table which has a field that allows up to 120 chars. I want to split the field into three fields. If the field contains more than 40 chars and less than 80 then split the field into two. The split point should be the first space char, before the 40th character and add the two new fields to another table. and if the field is 120 char then split them in three.
Will appreciate the help!
I guess you could do something along the lines of:
SELECT
SUBSTRING(MyCol,1,40),
NULLIF(SUBSTRING(MyCol,41,40), ''),
NULLIF(SUBSTRING(MyCol,81,40), ''),
To have your 1 column broken down correctly for your INSERT statement.
The NullIf function will set whatever column needs to be NULL correctly if the SubString() function returns an empty string for that value.