Altering the contents of a column in SQL server 2008 - sql

I exported data from an excel file. From that file, I have a column called 'ID'.For now that column has entries like the following.
ID
1.14455,
2.48755,
3.65588,
4.25415,
and so on.
I need to get rid of that comma from the column. How do I do it?

If there are no other commas in the values, then just use replace():
select replace(col, ',', '')
If there are possibly other commas:
select (case when col like '%,' then left(col, len(col) - 1)
else col
end)

Related

Change data remove after the parens

HI I have data in one column as this:
in SQL Server 2008
Aetna (AETNA)
I need to update this so that it will be Aetna that is to remove everything after the first parens in an update mode.
You can use CHARINDEX() to find the position of the (, then select everything to the LEFT() of that:
SELECT RTRIM(LEFT('Aetna (AETNA)',CHARINDEX('(','Aetna (AETNA)')-1))
Need to subtract 1 from the length to also remove the (, and RTRIM() removes any extra blank space from the right side.
Just replace the hardcoded string with your column name.
The update would be:
UPDATE table
SET col = RTRIM(LEFT(col,CHARINDEX('(',col)-1))
WHERE col like '%(%'
If you need to do this for all records:
update t
set col = left(col, charindex(' (', col))
where col like '% (%';

Any way to change specific data in a column e.g Mr to Dr which contains a name

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".

Update statement to update part of column length

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]

SQL Server - select substring of all characters following last hyphen

I am working with a database of products, trying to extract the product color from a combined ID/color code column where the color code is always the string following the last hyphen in the column. The issue is that the number of hyphens, product ID, and color code can all be different.
Here are four examples:
ABC123-001
BCD45678-0165
S-XYZ999-M2235
A-S-ABC123-001
The color codes in this case would be 001, 0165, M2235, and 001. What would be the best way to select these into their own column?
I think the following does what you want:
select right(col, charindex('-', reverse(col)) - 1)
In the event that you might have no hyphens in the value, then use a case:
select (case when col like '%-%'
then right(col, charindex('-', reverse(col)) - 1)
else col
end)
It is great to check whether the hyphen exists or not in the string, to avoid the following error:
Invalid length parameter passed to the right function.
SELECT CASE WHEN Col like '%\%' THEN RIGHT(Col,CHARINDEX('\',REVERSE(Col))-1) ELSE '' END AS ColName

SQL split in one column into two columns based on delimiter(s)

My data in one column(col) is as follows:
Col
Accounts::Changes
Applications::Zen::Other
Server::Access
I need this data to go to two columns. I want the first string before the delimiter (:) to go into one column (col1) and the last string after the last delimiter to go into another column (col2).
The output should be:
Col1 Col2
Accounts Changes
Applications Zen
Server Access
I am using sql server 2008 R2
You should be able to do this with basic string operations:
select left(col, charindex('::', col) - 1) as col1,
right(col, charindex('::', reverse(col)) - 1) as col2
from table t;
Here is a SQL Fiddle.
I have been able to achieve this as follows:
select A.Col1,
case when CHARINDEX('::',A.Colx)>0
then SUBSTRING(A.Colx,1,CHARINDEX('::',A.Colx)-1)
else A.Colx end Col2,
CASE WHEN CHARINDEX('::',A.Colx)>0
THEN SUBSTRING(A.Colx,CHARINDEX('::',A.Colx)+2,len(A.Colx))
ELSE NULL END as Colx3
FROM (
select
case when CHARINDEX('::',Col)>0
then SUBSTRING(Col,1,CHARINDEX('::',Col)-1)
else Col end Col1,
CASE WHEN CHARINDEX('::',Col)>0
THEN SUBSTRING(Col,CHARINDEX('::',Col)+2,len(Col))
ELSE NULL END as Colx
FROM Table1 ) as A
Although I end up getting a third column with the leftover string, I won't use it.