concat with left in sql - sql

i have two columens in the same table:
Column A :abcdef
Column B :12345
I want Column A value to be replaced by :
abcdef123
therefore i want all the data from column A plus the 3 first digit from column B.
I am stuck big time. I use Microsft SQL server Mgt Studio.
any help is welcome.
thanks

try this
set ColumnA=(select CONCAT(ColumnA,LEFT(ColumnB,3)))

as simple as this:
update table set ColumnA = ColumnA + LEFT(ColumnB, 3)

Use LEFT string function :
DECLARE #colA VARCHAR(100) = 'abcdef'
DECLARE #colB VARCHAR(100) = '12345'
SELECT #colA + LEFT(#colB,3)

Update Table_name SET ColumnA = ColumnA+LEFT(ColumnB,3)

Related

SQL Replace only a part of a path

I have a table (about 160k rows) with a column called paths.
In that column there are paths like:
"\\ab.local\folder1\folder2\folder3\folder_x"
"\\ab.local\folderA\"
The length of the paths differ.
What I would like to do is to replace only the "ab" before the .local in to "cd" and leave the rest untouched.
I have been told to use a replace function, but somehow I don't get it to work the way I want to.
I am looking for the right syntax to do this.
Declare #oldval as varchar(30) = '\\ab.local\' ;
Declare #newval as varchar(30) = '\\cd.local\' ;
update yourtable set yourfield = replace(yourfield,#oldval ,#newval) where yourfield like #oldval + '%'
Solved Reference : https://stackoverflow.com/a/814551/6923146
Hope it's perfect for your solution
UPDATE my_table
SET columnName = replace(columnName, 'oldstring', 'newstring')
WHERE columnName like '%oldstring%'
For example:
UPDATE my_table
SET columnName = replace(columnName, '\ab.', '\ab.')
WHERE columnName like '%\ab.%'
If the part to replace is on a fixed position with a fixed length you could use STUFF, like so:
UPDATE yourTable SET paths = STUFF(paths, 3, 2, 'cd')
This replaces two characters in paths beginning at position 3 with cd

updating a text with additional characters in SQL

I have the following value format :
1234567890
I want to convert it to the following format : 123-45A67890
Table Name: Test
Column Name : MyCode
Note that I am using Microsoft SQL 2012.
You can use STUFF . Something like this
DECLARE #v VARCHAR(15) = '1234567890'
SELECT STUFF(STUFF(#v,4,0,'-'),7,0,'A')
Your SELECT would be
SELECT STUFF(STUFF(MyCode,4,0,'-'),7,0,'A')
FROM Test
Your UPDATE would be
UPDATE Test
SET MyCode = STUFF(STUFF(MyCode,4,0,'-'),7,0,'A')
Does SQL Server support the PASTE function?
paste(paste('1234567890',7,0,'A'),4,0,'-')
For example:
select paste(paste(column_name,7,0,'A'),4,0,'-') from table_name
or
update table_name set column_name = paste(paste(column_name,7,0,'A'),4,0,'-')
Here what tried with a variable #a:
declare #a varchar(100)='1234567890'
select STUFF(STUFF(#a,4,0,'-'),7,0,'A') --gives--> '123-45A67890'
Likely you can use it to update your table, which
Adds Hyphen (-) after first 3rd character,
Adds letter 'A' after 2 letters just after hyphen...
update Test set MyCode = STUFF(STUFF(MyCode,7,0,'A'),4,0,'-')
More about STUFF() function in SQL

UPDATE and REPLACE part of a string

I've got a table with two columns, ID and Value. I want to change a part of some strings in the second column.
Example of Table:
ID Value
---------------------------------
1 c:\temp\123\abc\111
2 c:\temp\123\abc\222
3 c:\temp\123\abc\333
4 c:\temp\123\abc\444
Now the 123\ in the Value string is not needed. I tried UPDATE and REPLACE:
UPDATE dbo.xxx
SET Value = REPLACE(Value, '%123%', '')
WHERE ID <= 4
When I execute the script SQL Server does not report an error, but it does not update anything either. Why is that?
You don't need wildcards in the REPLACE - it just finds the string you enter for the second argument, so the following should work:
UPDATE dbo.xxx
SET Value = REPLACE(Value, '123', '')
WHERE ID <=4
If the column to replace is type text or ntext you need to cast it to nvarchar
UPDATE dbo.xxx
SET Value = REPLACE(CAST(Value as nVarchar(4000)), '123', '')
WHERE ID <=4
Try to remove % chars as below
UPDATE dbo.xxx
SET Value = REPLACE(Value, '123', '')
WHERE ID <=4
To make the query run faster in big tables where not every line needs to be updated, you can also choose to only update rows that will be modified:
UPDATE dbo.xxx
SET Value = REPLACE(Value, '123', '')
WHERE ID <= 4
AND Value LIKE '%123%'
query:
UPDATE tablename
SET field_name = REPLACE(field_name , 'oldstring', 'newstring')
WHERE field_name LIKE ('oldstring%');
You have one table where you have date Code which is seven character something like
"32-1000"
Now you want to replace all
"32-"
With
"14-"
The SQL query you have to run is
Update Products Set Code = replace(Code, '32-', '14-') Where ...(Put your where statement in here)
For anyone want to replace your script.
update dbo.[TABLE_NAME] set COLUMN_NAME= replace(COLUMN_NAME, 'old_value', 'new_value') where COLUMN_NAME like %CONDITION%
CREATE TABLE tbl_PersonalDetail
(ID INT IDENTITY ,[Date] nvarchar(20), Name nvarchar(20), GenderID int);
INSERT INTO Tbl_PersonalDetail VALUES(N'18-4-2015', N'Monay', 2),
(N'31-3-2015', N'Monay', 2),
(N'28-12-2015', N'Monay', 2),
(N'19-4-2015', N'Monay', 2)
DECLARE #Date Nvarchar(200)
SET #Date = (SELECT [Date] FROM Tbl_PersonalDetail WHERE ID = 2)
Update Tbl_PersonalDetail SET [Date] = (REPLACE(#Date , '-','/')) WHERE ID = 2
you should use the below update query
UPDATE dbo.xxx SET Value=REPLACE(Value,'123\','') WHERE Id IN(1, 2, 3, 4)
UPDATE dbo.xxx SET Value=REPLACE(Value,'123\','') WHERE Id <= 4
Either of the above queries should work.
replace for persian word
UPDATE dbo.TblNews
SET keyWords = REPLACE(keyWords, '-', N'،')
help:
dbo.TblNews -- table name
keyWords -- fild name

SQL Server 2005 XML Query problem with ".exist" method

I have this XML Query in SQL Server 2005:
SElECT XmlField FROM tablename WHERE xmlField.exist('(/Root/Name[id="10")[1]') = 1
However, I want to replace the value "10" with a parameter that I pass to the Stored Procedure. How do I achieve this? I have tried using "#variablename" but it doesn't work.
Thanks in advance.
Probably, you want to have something like
SELECT XmlField FROM tablename WHERE xmlField.exist('(/Root/Name[id="{ sql:variable("#variablename") }")[1]') = 1
See http://msdn.microsoft.com/en-us/library/ms188254(v=SQL.100).aspx for how to access variables and columns in XQuery in SQL Server.
After a few minutes of hair pulling...i found an answer...
Result_XML.exist('(/Root/Name[id="{sql:variable("#myId")}"])[1]') = 1
should be written as
Result_XML.exist('(/Root/Name[id=(sql:variable("#myId"))])[1]') = 1
I replaced the "{ and }" with ( and ) to enclose the sql:variable keyword.
There is one more thing I found out about by many many trials: if your variable is a char value, if you declare it in your sql statement, it should be varchar, not char.
This sql didn't return any results:
DECLARE #myparam char(50)
SET #myparam = 'someval'
...
WHERE
t.c.exist('/root/child[text() = sql:variable("#myparam ")]') = 1
But this did:
DECLARE #myparam varchar(50)
SET #myparam = 'someval'
...
WHERE
t.c.exist('/root/child[text() = sql:variable("#myparam ")]') = 1
Maybe this is obvious, but I spent some time before I figured the reason why no records would be returned.

sql query to replace a blank cell with any text in access 2007 column

I have an access table which has some cells as blank ( no data ) in a particular column.
how i write an sql query to replace a blank cell with any text in access 2007 column
any help appreciated.
i have already tried the sql query
update tableA set colA = 'abc' where ISNULL(colA);
It updates 0 rows.
Update Table
Set [ColumnName] = "my random text"
Where Len([ColumnName]) = 0 OR [ColumnName] Is Null
This will account for situations where the cell value is an empty string or if it is null. If you are trying update one column from another you can do:
Update Table
Set [ColumnName] = [MyOtherColumnName]
Where Len([ColumnName]) = 0 OR [ColumnName] Is Null
Im pretty sure its
update tableA set colA='abc' where colA is null
Try this:
update tableA set colA = 'abc' where colA IS NULL;