how to clean unwanted data in a column in sql server 2008 - sql

I have a column name Newspath varchar(max). The data in the column is like this
Newspath
423.jpg64265
789.jpg41546
546.jpg7894
I want to remove all the words after .jpg word like this
Newspath
423.jpg
789.jpg
546.jpg
Please help

I am assuming you need to update all the rows of your table. You can update all the rows of your table like
BEGIN TRAN
UPDATE yourtable
SET newpath = SUBSTR(newpath,0,CHARINDEX('jpg',newpath)+2)
You can look at more for Substring and Charindex.

Related

Update Script in SQL Server to add a hyphen after 5 characters

I have a varchar column which has data like ABCDE1, the first five characters will be alphanumeric.
I would like to add a hyphen for all records after fifth character
ABCDE-1
How can I achieve using an update script in SQL Server?
Use STUFF function
Select STUFF(varchar_col,5+1,0,'-')
From yourtable
To Update
Update yourtable Set varchar_col = STUFF(varchar_col,5+1,0,'-')

How to update a part of the column data using replace function in SQL?

I am trying to update a table coulmn, the table has thousands of records.
Currently I am updating the table by running the following query manually for some of the records.
UPDATE MyTable
SET column = REPLACE(column, 'ABC', 'ABC9')
WHERE where column like ‘ABC%’
Now I am trying to generate a generic query to update the table by adding a letter '9' after the alphabets. Thanks for your help
Use PATINDEX and STUFF
Patindex - Helps you to identify the first occurrence of numeric character in the string
Stuff - Helps you to insert 9 before the first occurrence of numeric character in the string
UPDATE MyTable
SET column = stuff(column,patindex('%[0-9]%',column),0,'9')

Add additional text to an existing string in SQL?

I have a SQL table with a column named "FileLink" and I need to add the domain name at the end of the server name for all the existing records in the table. So it would be like this:
Before:
\\ServerName\SharedFolder\Test.PDF
After:
\\ServerName.domain.net\SharedFolder\Test.PDF
So I need to add ".domain.net" to the link. Is there a sql statement to do this?
TIA
If you need to modify only one domain and this value is unique you can use REPLACE:
update Table
set Column = REPLACE(Column, 'ServerName', 'ServerName.domain.net')
If you don't want to use the replace statement you can do it like this:
Declare
#SrvName as varchar(50)
Set #SrvName = '\\ServerName'
Select
'\\ServerName.domain.net'+Substring(FileLink,Len(#SrvName)+1,Len(FileLink)-Len(#SrvName))
If the servername is the same for each record you could do it with the replace statement. Otherwise you might want to use patindex to find the first occurence of a '\' starting from position 3 to determin the place you need to insert the extra text.
If you want to prevent any issue if you run the query against old and new records, you should use
REPLACE(FileLink, '\\ServerName\', '\\ServerName.domain.net\')
Which gives:
SELECT
SELECT REPLACE(FileLink, '\\ServerName\', '\\ServerName.domain.net\') AS FileLinkUpdated
FROM MyTable
UPDATE
UPDATE MyTable
SET FileLink = REPLACE(FileLink, '\\ServerName\', '\\ServerName.domain.net\')
Note that this is assuming you don't have a link with only \\ServerName

How to store part of a field value in another column in SQL

So I'm trying to get a part of a value from a column and insert that part into another column - new column. BOTH columns are in the same table. So what i want should look something like this:
id newColumn oldColumn
1 12 123 some text
2 24 246 some text
....
I know how to get 12 and 24 using SUBSTR, but how do i enter the data for each row in the table. Should i be using self-join or something else?
First you have to add new col using following command:-
ALTER TABLE TAB_NAME
ADD COLUMN COL_NAME(VARCHAR(10));
After that execute this command:-
UPDAET TAB_NAME
SET COL_NAME = SUBSTRING(OLDCOLUMN, 1, 2);
I think this might help you.
No need to join, it's just a plain UPDATE:
update tablename set newColumn = substring(oldColumn from 1 for 2)
substring is ANSI SQL, some dbms have substr and other versions.
The question is why you are doing this? What do you expect to find in newColumn if someone later updates oldColumn to another value? Maybe you should have a view instead, where newColumn always has up to date values?
Please get into the habit of ALWAYS specifying the DB engine you are using... It helps us to help you - we can provide more relevant answers.
You might want to consider using a calculated column as opposed to storing the information again.
In SQL Server you could do something like this
ALTER TABLE YourTable
ADD new_column as SUBSTRING(old_column, 1, 2);
This way you don't need to insert or update this column it is always consistent with the original column. and you just use it in your select statement in the usual way.
select new_column from YourTable

SQL - UPDATE / SET statement query

I wanted to update 1 column from a table present in a database maintained in SQL 2008 R2.
The entire column contains same value of data type int and I want to change to different value of same int data type.
When do we use single apostrophe in SET statement & when to avoid or not use it?
UPDATE TABLENAME
SET COLUMNNAME = X
OR
UPDATE TABLENAME
SET COLUMNNAME = 'X'
Appreciate any suggestions/advise. Thanks.
In SQL, single quotes are used around string values. Other "values", like integers, references to other columns etc, should never be single quoted.