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,'-')
Related
How do I remove the first two characters of every row in this column?
Ticket Number
J2F4T45T
J2J3J3J2
J25TGYHJ2
J2FFJ2J2
J2MG8NGJ2
If you are using MSSQL You can use STUFF like this to see how it works:
SELECT STUFF('0123456789',1,2,'')
And then with an update
UPDATE YOUR_TABLE SET YOUR_COLUMN = STUFF('YOUR_COLUMN',1,2,'')
if you were using mysql there is an similar function INSERT()
There are a few different ways to do this, you can play with SUBSTRING or even regular expressions depending on your database.
I have a numbers column in my sql table and I want to pull all the numbers that start with 3 specific digits; how would I query this?
Use the "LIKE" query:
123 is the prefix. I think you have to store the numbers as strings though, just try it out on your data set :-)
SELECT * from TableName Where ColumnName LIKE '123%'
See also this Q&A: In MySql, find strings with a given prefix
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')
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.
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.