Replacing / with - in SQL Server using Select Command - sql

How can I replace one char from a SQL Server Select command without changing the value in the table itself?
Example:
Select Col_Name
from TABLE
returns
**RANDOM/ANSWER**
How do I modify the Select query to get **RANDOM-ANSWER** instead? So the / is replaced by -. But the data in the table remains unchanged

The replace function should do the trick:
SELECT REPLACE(my_column, '/', '-')
FROM my_table

Select Col_Name, REPLACE(Col_Name, '/', '-') AS New_Column
from TABLE

To replace all the sepecific character/string found in the data with the new character/string.
DECLARE #myValue VARCHAR(50) = 'RANDOM/ANSWER';
SELECT REPLACE(#myValue, '/', '-');
-- o/p:
-- RANDOM-ANSWER

Related

Multiple occurrences of a character match using regular expression in sql server 2014

In SQL Server 2014, i want to select a row which contains a word that is not present inside any of the angled brackets <>.
Sample Data:
Row 1 --> <div class="highlight"><b>Maddy</b></div>
Row 2 --> <div><b>This is highlighting an feature.</b></div>"
Here i want to filter only second row. So i used a query like
select * from table where column like '%<%>[a-zA-z0-9]*'+'highlight'+'%<%>%'"
I believe this is what you need:
LIKE 'T%[a-z]%[a-z]%'
Now would be a good time to familiarize yourself with what you can and cannot do with the LIKE operator.
Attempt:
select * from table_name where column_name like 'T[a-z]%'
select * from table_name where column_name like '[T]%'
You've nearly had the answer yourself Mathan.
The only problem that I can see with your code is that you were treating the LIKE expression as a dynamic expression.
If you are searching for a specific value in the middle of a substring then you need to wrap it in wildcards, even if you are joining search expressions together, e.g. LIKE '%highlight% + '%<%>%'
DECLARE #table table ( [column] varchar(100));
insert into #table ([column])
SELECT * FROM
(
VALUES
('<div class="highlight"><b>Maddy</b></div>'),
('<div><b>This is highlighting an feature.</b></div>')
) as [table] ([column]);
--SELECT * FROM #table;
select
[text_value] = PATINDEX('%<div>%', [column]) + LEN('<div>'),
SUBSTRING(
[column] -- what you're searching
, PATINDEX('%<div>%', [column]) + LEN('<div>') -- after the '<div>',
-- need to add the length of the 'div' as PATINDEX returns the starting location
, PATINDEX('%</div>%', [column]) - LEN('</div>') -- until the '</div>'
)
, *
from #table
where [column] like
'%<%>[a-zA-z0-9]%' -- you need to end these with the wildcard
+'%highlight%' -- or SQL-Server thinks it's the end of the sentence
+'%<%>%';
EDIT:
Adding in the PATINDEX can be used to remove the '<>' from the string. Example above removes the <div></div> but you can use that to remove any others as necessary e.g. <b></b>

Replace ; with blank in SQL

I have a table like this:
DECLARE #T TABLE
(note VARCHAR (50))
INSERT #T
SELECT 'Amplifier'
UNION ALL SELECT ';'
UNION ALL SELECT 'Regulator'
How can I replace the semicolon (';') with blank ('').
Expected Output:
Amplifier
'' -- here semicolon replace with blank
Regulator
If you want to replace ALL semicolons from any outputted cell you can use REPLACE like this:
SELECT REPLACE(note,';','') AS [note] FROM #T
Fetching from the given table, use a CASE statement:
SELECT CASE WHEN note = ';' THEN '' ELSE note END AS note FROM #T;
replace() would replace all occurrences of the character. Doesn't seem like you'd want that. This expression only replaces exact matches of the whole string.
It looks like you need to REPLACE all your semicolons:
DECLARE #T TABLE
(note VARCHAR (50))
INSERT INTO #T
SELECT REPLACE(SourceColumn, ';', '')
FROM SourceTable
SQL Server 2017 introduced the function TRANSLATE where you can specifiy a list of characters to be replaced
SELECT TRANSLATE('MAX(0,MIN(h-36,8))', '(,-)', ' ') -->'MAX 0 MIN h 36 8 '

How do I get the just the number from a SQL table where it is stored like this <PersonNumber>013870</PersonNumber>

I have a table that stores the information like this, ABC, and I just want the text in between.
simple demo
declare #test table
(
xmlElement varchar(1000)
)
insert into #test
values ('<PersonNumber>013870</PersonNumber>')
-- select numbers from xml in the given format
select
cast(SUBSTRING(xmlElement, 15 , CHARINDEX('</PersonNumber>',xmlElement)-15) as int) -- 15 is position where number starts, because 14 is legth of <PersonNumber>
from #test
-- result is 13870
In Oracle there is *REGEXP_SUBSTR* you can use it if you are oracle to get the number you want ans this is an example for you:
SELECT
REGEXP_SUBSTR('500 Oracle Parkway, Redwood Shores, CA',
',[^,]+,') "REGEXPR_SUBSTR"
FROM DUAL;
REGEXPR_SUBSTR
-----------------
, Redwood Shores,
If PersonNumber is fixed length you can simply use substring function.
Otherwise use
select Substring(PersonNumber, Patindex('%>%',PersonNumber) + 1, Patindex('%< /%',PersonNumber) - Patindex('%>%',PersonNumber)-1)
Try this as Sample:
DECLARE #z VARCHAR(32) = ',ukasd10,';
SELECT REPLACE(SUBSTRING(#z, CHARINDEX(',', #z), LEN(#z)), ',', '') AS Sample
and use this logic wherever you want...thanks!
With SQL Server, you can use an XML method if the column contains well-formed XML:
SELECT CAST(YourColumn AS xml).value('/Request[1]/Schedule[1]/Employees[1]/PersonIdentity[1]/PersonNumber[1]', 'int') AS PersonNumber
FROM dbo.YourTable;

Insert a period before the last two characters of a sql column

Hello I have a column in sql and I need to insert a "." before the last two characters. The column is not fixed length. Can someone help me go about this?
You can do it with an UPDATE and SUBSTRING.
UPDATE table
SET column = (SELECT SUBSTRING(column, 1, LEN(column)-2)
+ '.'
+ SUBSTRING(column, LEN(column)-1, LEN(column))
If you would like to check what the query will do to your column, just use:
SELECT
SUBSTRING(column, 1, LEN(column)-2)
+ '.'
+ SUBSTRING(column, LEN(column)-1, LEN(column))
FROM table
It looks messy but this should do it:
SELECT LEFT(COL_NAME, LEN(COL_NAME)-1)+'.'+RIGHT(COL_NAME,1)
FROM Table
Or if you want to update the value in the database rather than just the output
Update Table
SET COL_NAME = LEFT(COL_NAME, LEN(COL_NAME)-1)+'.'+RIGHT(COL_NAME,1)
You can use sp_rename like this:
EXEC sp_rename 'dbo.DatabaseName.ColumnName', 'ColumnNa.me', 'COLUMN';
If you need to apply this to multiple columns, then I would suggest using the substring method. No real use to do it for just one UPDATE on a column.
Question is not very clear. If you are trying to insert . before last 2 characters into the column data, you could use STUFF() function.
For ex:
Declare #s varchar(50) = '12345678'
Select Stuff(#s, Len(#s)-1, 0, '.')
--> 123456.78
Applying to your table query:
Select Stuff(yourCol, Len(yourCol)-1, 0, '.')
From yourTable
Please note that If Len(yourCol) is less than 2, a null string will be returned.

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,',','|')