I'm working on SQL Server 2008. Assume the following column.
column1
cjd.001 306.1
cjd.001 306.2
cjd.001 306.R
Now i want to replace all '.' with '-'. But just from first string i.e. before space or below 8 characters. The remaining field should remain same.
SampleOutput
cjd-001 306.1
cjd-001 306.2
cjd-001 306.R
I tried below query but it replaces all '.'.
UPDATE dbo.table
SET column1 = REPLACE(column1, '.', '-')
I want to know what can i use in where clause?
Tried using substring function but it didn't work.
I think you need a SQL like this:
UPDATE yourTable
SET column1 = REPLACE(SUBSTRING(column1, 1, CHARINDEX(' ', column1)), '.', '-')
+ SUBSTRING(column1, CHARINDEX(' ', column1) + 1, LEN(column1));
[SQL Fiddle Demo]
Related
SQL: What are phone number formatting options?
I query my SQL Database for a phone number:
816-123-4567
I want to output it is as:
816 123-4567.
What steps do I take to achieve this result in an SQL environment?
A standard SQL solution would be:
select substring(phone, 1, 3) || ' ' || substring(phone, 5, 8)
There may be simpler solutions in other databases. For instance, in SQL Server:
select stuff(phone, 4, 1, ' ')
And in MySQL:
select insert(phone, 4, 1, ' ')
Note: These are specific the the format you have provided in your question. If you have other formats, then you might need more complicated logic.
Use SUBSTRING and CHARINDEX functions.
SUBSTRING gets a part of the string according to given indexes.
CHARINDEX gets the index of the character that triggers your String separation.
Below is an MSSQL Server query.
According to your DBMS, the function names can vary, but the logic will is the same.
Query :
SELECT
SUBSTRING('816-123-4567', 0, CHARINDEX('-', '816-123-4567') ) +
' ' +
SUBSTRING('816-123-4567', CHARINDEX('-', '816-123-4567') + 1 , LEN('816-123-4567') -
CHARINDEX('-', '816-123-4567') )
And the result :
816 123-4567
When we put your field and table name instead of static values :
SELECT
SUBSTRING(YourPhoneField, 0, CHARINDEX('-', YourPhoneField) ) +
' ' +
SUBSTRING(YourPhoneField, CHARINDEX('-', YourPhoneField) + 1 , LEN(YourPhoneField) -
CHARINDEX('-', YourPhoneField) )
FROM YourTableName
I need help to replace funny character in a column in SQL Server,
I have data like this:
id itemDesc
----------------------------------------------
1 Ball lock pins/ spring typeáááááá
2 Res 1.5k Ohm û R0805 1%
If itemDesc contains á, then replace it with " "; if it contains (û), replace it with -. I used charindex but not change at all especially with many funny characters like id = 1, so if i used charindex, "Ball lock pins/ spring typeáááááá" => "Ball lock pins/ spring type ááááá"
Any approach?
thanks for help
You can use REPLACE to replace the characters on the string:
SELECT REPLACE(REPLACE(itemDesc, 'á', ' '), 'û', '-') FROM table_name
In case you want to UPDATE the value on column itemDesc you can use the following:
UPDATE table_name SET itemDesc = REPLACE(REPLACE(itemDesc, 'á', ' '), 'û', '-')
The function CHARINDEX can't be used to replace the characters, but to find them. So you can UPDATE or SELECT only the rows with these characters using CHARINDEX:
SELECT REPLACE(REPLACE(itemDesc, 'á', ' '), 'û', '-')
FROM table_name
WHERE CHARINDEX('á', itemDesc) > 0 OR CHARINDEX('û', itemDesc) > 0
UPDATE table_name SET itemDesc = REPLACE(REPLACE(itemDesc, 'á', ' '), 'û', '-')
WHERE CHARINDEX('á', itemDesc) > 0 OR CHARINDEX('û', itemDesc) > 0
demo: http://www.sqlfiddle.com/#!18/6e241/1/0
If you're using SQL Server 2017, you can use TRANSLATE:
SELECT TRANSLATE(itemDesc, 'áû',' -') AS itemDescTidy
FROM table_name;
This is a little more succinct than a nested REPLACE (but is actually identical, as it's a "short-hand" function).
I have table like this
Column1
-------------------------------------------------------
nn=APPLE IPod 44454,o=0006,o=TOP8,nn=USA,nn=TOP8+
nn=SAMSUNG G5 487894,o=04786,o=T418,nn=JPN,nn=TO478+
And I need update that table and get result like this
Column1 Column2
---------------------------------------------------------------
nn=APPLE IPod 44454,o=0006,o=TOP8,nn=USA,nn=TOP8+ 44454
nn=SAMSUNG G5 487894,o=04786,o=T418,nn=JPN,nn=TO478+ 487894
My idea is but I can not fit with first character:
update tablename
set [column2] = SUBSTRING(column1, 1, CHARINDEX(' ', column1) - 1 (column1, CHARINDEX(' ', column1) + 1, LEN(column1))
This query can help you.
UPDATE tablename SET [column2] =
SUBSTRING((LEFT(column1,CHARINDEX(',',column1)-1)), CHARINDEX(' ', column1, CHARINDEX(' ',column1) +1 ) +1 ,LEN(column1))
Okay, given the second sample record, it looks like what you need is the last element of the space-delimited string in the first position of the comma-delimited string. So write yourself (or find) a string-splitter function that accepts a string and a delimiter, and then your parsing logic is:
split the field at the commas
take the first element
split that element at the spaces
take the last element
Does that make sense?
The following answer is based only on the two records you actually showed us. If we were to derive a rule from this, it might be that we want to extract the previous word (a product number) occurring immediately before the first comma in the string. If so, then we can try the following logic:
isolate the substring before the comma (e.g. nn=APPLE Ipod 44454)
reverse that string (45444 dopI ELPPA=nn)
then take the substring of that before the first space (45444)
finally reverse this substring to yield the product number we want (44454)
Consider the following query, with data imported via a CTE:
WITH yourTable AS (
SELECT 'nn=APPLE IPod 44454,o=0006,o=TOP8,nn=USA,nn=TOP8+' AS column1 UNION ALL
SELECT 'nn=SAMSUNG G5 487894,o=04786,o=T418,nn=JPN,nn=TO478+'
),
update_cte AS (
SELECT
column1,
column2,
REVERSE(
SUBSTRING(REVERSE(SUBSTRING(column1, 1, CHARINDEX(',', column1)-1)),
1,
CHARINDEX(' ', REVERSE(SUBSTRING(column1, 1, CHARINDEX(',', column1)-1))) - 1)
) AS col2
FROM yourTable
)
SELECT column1, col2 FROM update_cte;
Output:
Demo here:
Rextester
If you wanted to update your table to bring in these column2 product IDs, then you can use the above CTE fairly easily:
UPDATE update_cte
SET column2 = col2;
I am trying to change the Titles of 'doctors' in a database and was just wondering was there a SQL query which I could run to change them.
The column im trying to change
What I am asking is that there is any way I can update the column to add a 'Dr' infront of the names to replace the 'Miss','Mr' etc.
I'm thinking about using a SQL Statement containing the wildcard function to update it but not sure it would change the specifics.
Thanks,
Karl
Use REPLACE option
UPDATE table_name
SET column_name = REPLACE(column_name, 'Mr.', 'Dr.')
WHERE column_name LIKE 'Mr.%'
try this
Update myTable
set name = replace(replace(name,'Miss ','Dr '),'Mr ','Dr ')
I might suggest doing:
update t
set col = stuff(col, 1, charindex(' ', col + ' '), 'Dr');
This only replaces the first word in the string. You might want to be extra careful and add a where clause.
update t
set col = stuff(col, 1, charindex(' ', col + ' '), 'Dr')
where col like 'Miss %' or col like 'Mr %' or
col like 'Mrs %' or col like 'Ms %';
The problem with replace() is that it replaces all occurrences in the string. Although the honorifics are unlikely to be in a name, you could have names like "Missy Elliott".
I'm having a problem when running the below query - it seems to ONLY affect the very first record. The query removes all trailing and beginning double quotes. The first query is the one that does this; the second query is just to demonstrate that there are multiple records that have beginning double quotes that I need removed.
QUESTION: As you can see the first record resulting from the top query is fine - it has its double quotes removed from the beginning. But all subsequent queries appear to be untouched. Why?
If quotes are always assumed to exist at both the beginning and the end, adjust your CASE statement to look for instances where both cases exist:
CASE
WHEN ([Message] LIKE '"%' AND [Message] LIKE '%"') THEN LEFT(RIGHT([Message], LEN([Message])-1),LEN([Message]-2)
ELSE [Message]
EDIT
If assumption is not valid, combine above syntax with your existing CASE logic:
CASE
WHEN ([Message] LIKE '"%' AND [Message] LIKE '%"') THEN LEFT(RIGHT([Message],LEN([Message])-1),LEN([Message]-2)
WHEN ([Message] LIKE '"%') THEN RIGHT([Message],LEN([Message]-1)
WHEN ([Message] LIKE '%"') THEN LEFT([Message],LEN([Message]-1)
ELSE [Message]
Because your CASE statement is only evaluating the first condition met, it will only ever remove one of the statements.
Try something like the following:
SELECT REPLACE(SUBSTRING(Message, 1, 1), '"', '') + SUBSTRING(Message, 2, LEN(Message) - 2) + REPLACE(SUBSTRING(Message, LEN(Message), 1), '"', '')
EDIT: As Martin Smith pointed out, my original code wouldn't work if a string was under two characters, so ...
CREATE TABLE #Message (Message VARCHAR(20))
INSERT INTO #Message (Message)
SELECT '"SomeText"'
UNION
SELECT '"SomeText'
UNION
SELECT 'SomeText"'
UNION
SELECT 'S'
SELECT
CASE
WHEN LEN(Message) >=2
THEN REPLACE(SUBSTRING(Message, 1, 1), '"', '') + SUBSTRING(Message, 2, LEN(Message) - 2) + REPLACE(SUBSTRING(Message, LEN(Message), 1), '"', '')
ELSE Message
END AS Message
FROM #Message
DROP TABLE #Message
Try this:
SELECT REPLACE([Message], '"', '') AS [Message] FROM SomeTable