updating a text with additional characters in SQL - 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

Related

concat with left in 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)

return value that column value found matched with the variables value in MSSQL

I have the following SQL script use to select the records that current_status contains the following variables #isPending, #isInvoiced, #isRework, #isCancelled.
SELECT *
FROM vw_sale_search vss
WHERE CONTAINS (( #isPending, #isInvoiced, #isRework, #isCancelled), vss.current_status )
However, error message is:
(Incorrect syntax near '#isPending'.)
display when I tried to run the script.
The main container, in this case your current_status column should be placed in the first place. Then the item to be searched will take the second place in SQL Server Full-Text CONTAINS function
To searching for more than one word, you can use OR syntax in the CONTAINS function
I created sample fulltext catalog and text table as shown in SQL Fulltext tutorial and prepared fıllowing SQL Select script to use OR with Contains function
declare #isPending varchar(10) = 'Data', #isInvoiced varchar(10) = 'Pool', #txt varchar(100)
set #txt = #isPending + ' OR ' + #isInvoiced
SELECT * FROM TextTable WHERE CONTAINS (textColumn, #txt)

Setting a character variable which consists of this (') character

I want to set a variable to expressions like this 'word' in SQL which consists of this (') character.
How can I do that?
For SQL Server to show ' use '' into select
SELECT 'aa''a'
You can use variable with Single quotes as following :-
DECLARE #varName VARCHAR(50)
SET #varName = '''word'''
SELECT #varName as colName
Output :- 'word'
Sample Fiddle

Insert multiline in varchar

Is it possible to save a multiline varchar in SQL Server?
DECLARE #SQL VARCHAR(256)
SELECT #SQL = 'SELECT
ID, Name
FROM
Cust'
INSERT INTO
MultiLineTBL (SQL)
VALUES
(#SQL)
So this query:
SELECT SQL From MultiLineTBL
will return:
SELECT
ID, Name
FROM
Cust
Not the straight line:
SELECT ID, NAME FROM Cust
How is it possible to store a multiline varchar?
Your sql query have syntex error:
DECLARE #SQL VARCHAR(256) ;
--please update query like that
set #SQL = 'insert into MultiLineTBL(col1,col2)
SELECT ID, Name FROM Cust';
--and execure like that:
exec(#SQL);
Yes, it is possible to store a multi-line varchar in a table. In fact, the value in your example is stored correctly.
As AlexK has correctly commented, multi-line values are not displayed correctly in SQL Server Management Studio when it is configured to return the results in a grid, and that (viewing the results in a grid) must be what gave you the wrong impression. As per my observation, SSMS replaces every control character, including CR (CHAR(13)) and LF (CHAR(10)), with a space in this display mode.
Try switching to Results to Text (Ctrl+T) before running the query and you will see that line delimiters are actually preserved.

SQL query to replace commas by pipe symbol

How to write an SQL query to replace commas by pipe symbol in a string, for example: abc, def.
use following query
update DATABASE_NAME.TABLE_NAME
set FIELD_NAME = replace(
FIELD_NAME,
‘find this string’,
‘replace found string with this string’
);
also u can use for select only
SELECT REPLACE(‘www.mysql.com’, ‘w’, ‘Ww’);
There is no such command in the SQL standard, but most vendors implement this function as "replace():
SQL Server: replace()
http://msdn.microsoft.com/en-us/library/ms181984.aspx
Oracle: replace()
http://www.oradev.com/oracle_string_functions.jsp
DB2: replace ()
http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2z10.doc.sqlref%2Fsrc%2Ftpc%2Fdb2z_scalarfunctionsintro.htm
mySQL: replace ()
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html
Here are some SQL Server examples:
http://www.sqlteam.com/article/using-replace-in-an-update-statement
SELECT Replace('SQLTeam.com Rocks!', 'Rocks', 'is cool') -- returns literal
Update dbo.authors
Set city = replace(city, 'Salt', 'Olympic'); -- Updates table
Declare #str varchar(100)='abc,def'
SELECT REPLACE(#str,',','|')