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

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

Related

How to remove all letters from the beginning of the string in SQL server?

I'm trying to remove the letters from the beginning of the string only from the dbo.ProductCodes table.
I have:
ProductCode
XXX8361229BB
XY0060482AB
CR0058882A1
CPR777093219
CPCODE0002835
I want:
ProductCode
8361229BB
0060482AB
0058882A1
777093219
0002835
If the letters were only at the beginning of the string, I could remove all letters using regex [^a-zA-z]. The problem is that letters appear not only at the beginning of the string.
EDIT: Also, I'd like to apply some exclusions to this logic. For instance, if the prefix is 'AA' or 'Q' or 'QA', I don't want to remove letters from the beginning of the string. Examples: Q12345, AA1234S, QA12345
Updated for changed requirements:
SELECT ProductCode,
Adjusted = SUBSTRING(ProductCode,
CASE WHEN ProductCode NOT LIKE 'Q%'
AND ProductCode NOT LIKE 'QA%'
AND ProductCode NOT LIKE 'AA%' THEN
PATINDEX('%[0-9]%', ProductCode+'0')
ELSE 1 END, 255)
FROM dbo.ProductCodes;
Example db<>fiddle also deals with no letters and all letters (empty result).
Rather than SUBSTRING, I would personally use STUFF, as then you don't have to define a length of how many characters you want to retain, meaning that this will work for any length string. I also switch to looking for the first non-alpha character, rather than the first number, just to show the difference.
SELECT ProductCode,
STUFF(ProductCode, 1, ISNULL(NULLIF(PATINDEX('%[^A-z]%', ProductCode),0)-1,0),'')
FROM dbo.ProductCodes;

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!)

SQL -- SELECT statement -- concatenate strings to

I have an SQL question. Everything works fine in the below SELECT statement except the portion I have highlighted in bold. What I'm trying to do is allow the user to search for a specific Rule within the database. Unfortunately, I do not actually have a Rule column, and so I need to concatenate certain field values to create a string with which to compare to the user's searchtext.
Any idea why the part in bold does not work? In theory, I would like this statement to check for whether the string "Rule " + part_num (where part_num is the value contained in the part_num field) equals the value of searchtext (the value of searchtext is obtained from my PHP script).
I did some research on concatenating strings for SQL purposes, but none seem to fit the bill. Does someone out there have any suggestions?
SELECT id,
part_num,
part_title,
rule_num,
rule_title,
sub_heading_num,
sub_heading,
contents
FROM rules
WHERE part_title LIKE "%'.$searchtext.'%"
OR rule_title LIKE "%'.$searchtext.'%"
OR sub_heading LIKE "%'.$searchtext.'%"
OR contents LIKE "%'.$searchtext.'%"
OR "rule" + part_num LIKE "%'.$searchtext.'%" --RULE PLUS PART_NUM DOESN'T WORK
ORDER BY id;
Since you didn't specify which DB your using, I'm going to assume SQL Sever.
Strings are specified in SQL Server with single quotes 'I'm a string', not double quotes.
See + (String Concatenation) on MSDN for examples.
Another possibility is that part_num is a numeric. If so, cast the number to a string (varchar) before concatenating.

Postgresql query to update fields using a regular expression

I have the following data in my "Street_Address_1" column:
123 Main Street
Using Postgresql, how would I write a query to update the "Street_Name" column in my Address table? In other words, "Street_Name" is blank and I'd like to populate it with the street name value contained in the "Street_Address_1" column.
From what I can tell, I would want to use the "regexp_matches" string method. Unfortunately, I haven't had much luck.
NOTE: You can assume that all addresses are in a "StreetNumber StreetName StreetType" format.
If you just want to take Street_Address_1 and strip out any leading numbers, you can do this:
UPDATE table
SET street_name = regexp_replace(street_address_1, '^[0-9]* ','','');
This takes the value in street_address_1 and replaces any leading string of numbers (plus a single space) with an empty string (the fourth parameter is for optional regex flags like "g" (global) and "i" (case-insensitive)).
This version allows things like "1212 15th Street" to work properly.
Something like...:
UPDATE table
SET Street_Name = substring(Street_Address_1 FROM '^[0-9]+ ([a-zAZ]+) ')
See relevant section from PGSQL 8.3.7 docs, the substring form is detailed shortly after the start of the section.