I need to update my table because a number of fields in a column have got a fullstop on the end, i need to remove this.
So, in TableA, Field1: the data looks like
1002243.
1007053.
1007403.
1104098.
1110010.
NOTE: Not all the fields are the same length.
I need to remove the full stops.
Im using SQL Server 2005, cheers :)
UPDATE TableA
SET Field1 = LEFT(Field1 ,LEN(Field1)-1)
WHERE Field1 LIKE '%.'
Then you have data after the period. Try this to see:
Print '[' + Field1 + ']' and see if you get something like this:
[101. ]
There is a space after the period.
Related
I want to create a select query that return the outcome like this :
A table that contains MatrixCode column with such values like this: 'shampo250', 'condishioner500', and 'AMO200'.
The query will check for each word in the column whether it is contained within text
SELECT *
FROM [t_MO_MatrixCodeMapping]
WHERE 'AMO200IL351972598764' LIKE MatrixCode
I tried to use contain too but it did dot worked.
I can not cut the text from 'AMO200IL351972598764' to 'AMO200'
Are you not after this?
SELECT *
FROM [t_MO_MatrixCodeMapping]
WHERE 'AMO200IL351972598764' LIKE MatrixCode + '%';
LIKE might as well be equivilent to = in your query, as it contains no wild cards. WHERE 'AMO200IL351972598764' LIKE 'AMO200' won't resolve the true, as it is semantically equivalent to 'WHERE 'AMO200IL351972598764' = 'AMO200'.
I have an nvarchar field in my database called CatCustom which contains comma-separated 5-character codes. It can contain as little as one code, or as many as 20 codes, separated by commas.
Right now, I use this query to add a new 5-character code to the field in given records (in this case the new code is LRR01):
UPDATE dbo.Sources
SET CatCustom = CONCAT_WS(', ', RTRIM(CatCustom), 'LRR01')
WHERE SourceID IN (1,2,3,4,5,8,9,44,63,45,101,102,222,344)
I need to add to this though: I need the record to be updated only if that 5-character code doesn't already exist somewhere in the CatCustom field, to ensure that code is not in there more than once.
How would I accomplish this?
EDIT: I really don't understand how this can be considered a duplicate of the suggested thread. This is a VERY specific case and has nothing to do with creating stored procedures and or variables. The alleged duplicated thread does not really help me - sorry.
Use STRING_SPLIT function to split the comma separated list and then add Not Exist condition in the WHERE clause like below
UPDATE dbo.Sources
SET CatCustom = CONCAT_WS(', ', RTRIM(CatCustom), 'LRR01')
WHERE SourceID IN (1,2,3,4,5,8,9,44,63,45,101,102,222,344)
AND NOT EXISTS (SELECT 1 FROM STRING_SPLIT(CatCustom, ',') where value = 'LRR01')
UPDATE dbo.Sources
SET
CatCustom = CONCAT_WS(', ', RTRIM(CatCustom), 'LRR01')
WHERE
SourceID IN (1,2,3,4,5,8,9,44,63,45,101,102,222,344)
AND CatCustom NOT LIKE '%LRR01%';
I am trying to do a select statement in Microsoft SQL server management studio. The data looks like this:
My select statement is:
SELECT * FROM OSUSR_W7I_INVENTORYCHANGELOG
WHERE OBJECTTYPEID = 3
and CREATEDBY is null
and TEXT = 'State changed from ''Awaiting Prep'' to ''Import Error'''
But this brings back no results, despite there being lots of records with this text.
I was able to retrieve some data by using this statement:
SELECT * FROM OSUSR_W7I_INVENTORYCHANGELOG
WHERE OBJECTTYPEID = 3
and CREATEDBY is null
and TEXT like '%Awaiting Prep%'
and TEXT like '%Import Error%'
but unfortunately it brings back too many results because it is bringing back data that states: 'State changed from 'Import Error' to 'Awaiting Prep''
where I am only looking for 'State changed from 'Awaiting Prep' to 'Import Error''
Please can anyone help. From reading other posts putting a double quote in should solve the issue of the single quotes but in this instance it doesn't work.
Many thanks
You can combine your LIKE to one using % as placeholders, instead of:
SELECT * FROM OSUSR_W7I_INVENTORYCHANGELOG
WHERE OBJECTTYPEID = 3
and CREATEDBY is null
and TEXT like '%Awaiting Prep%'
and TEXT like '%Import Error%'
do:
SELECT * FROM OSUSR_W7I_INVENTORYCHANGELOG
WHERE OBJECTTYPEID = 3
and CREATEDBY is null
and TEXT like '%Awaiting Prep%Import Error%'
You can try using LTRIM and RTRIM in sql server. If there is case of some white space. Otherwise your statement is seems ok
SELECT * FROM OSUSR_W7I_INVENTORYCHANGELOG
WHERE OBJECTTYPEID = 3
and CREATEDBY is null
and LTRIM(RTRIM(TEXT)) = 'State changed from ''Awaiting Prep'' to ''Import
Error'''
How to write sql procedure to get matching results while searching for string values.It should search by the similarity of the characters entered by user. For instance Ahmmed, Ahmad, Ahmmad or Mohammad, Mohammed, Mohamad etc should display all similar names while executing search.
Any help would be highly appreciated.
Thanks in advance.
Well I guess you need AI for that but, you can use the like keyword to do wildcards.
e.g.
$search_string = 'Ahm'; //or Moha
$query = SELECT column FROM tableName WHERE column LIKE '$search_string%'; //starts with the $search_string
or
$query = SELECT column FROM tableName WHERE column LIKE '%$search_string'; //ends with the $search_string
or
$query = SELECT column FROM tableName WHERE column LIKE '%$search_string%'; //starts and/or ends with the $search_string
take note of the %
I know there are a few about similar topics on here but I can't find one related to my issue, this is it:
I have a table with an ID Column and a QRCode column. each time an item is added the primary key auto increments. The QRCode will scan in to be like the following:
"http://somewebsite.com/12345/987654321"
i want to be able to remove the "http://somewebsite.com/" from the string, I know how to do this in C# however I am unsure of how to do this in Sql Server. any guidance would be great, thanks
Regular formats are like the following, and used in the example below.
"http://somewebsite.com/12345/456564654"
"http://somewebsite.com/12345/989886765"
"http://somewebsite.com/12346/987654321"
the query returns the following results:
SELECT
REPLACE
(
REPLACE(QRCode, 'http://somewebsite.com/', '')
,'/', ' '
) AS QRCode
FROM
QRTable
WHERE
QRCode LIKE '%http://somewebsite.com/%'
"12345 456564654"
"12345 989886765"
"12346 987654321"
Now i need to update the table with those new results however as there's 3 results, i get the error message "Subquery returned more than 1 value". is there a way to replace the selected values in the table with the ones that exist based on the primary key field?
**Removed previous example
A more complete answer based on your updated question. This removes the first portion of the URL as well as the trailing / so that you get your desired output.
DECLARE #Variable VARCHAR(50)
SET #Variable = 'http://somewebsite.com/12345/456564654'
SET #Variable =
REPLACE
(
REPLACE(#Variable, 'http://somewebsite.com/', '')
,'/', ' '
)
PRINT #Variable
Output = 12345 456564654
Looking at your SQL statement you want this:
SELECT
REPLACE
(
REPLACE(QRCode, 'http://somewebsite.com/', '')
,'/', ' '
) AS QRCode
FROM
QRTable
WHERE
QRCode LIKE '%http://somewebsite.com/%'