SQL Server Query Output with sql scenario replace values with 'x' [duplicate] - sql

This question already has answers here:
Replacing certain character in email addresses with '*' in an SQL query
(3 answers)
Closed 6 years ago.
I have one col in my table like:
COL
Kiyara#ymail.com
Akira#zmail.com
I want my output something like this:
COL
Kxxxxx#ymail.com
Axxxx#zmail.com
iyara replaced with 5x's(xxxxx) and 'kira' with 4x's(xxxx)

There's probably a better way to do it that this, but this achieves what you're after:
DECLARE #email NVARCHAR(50) = 'Kiyara#ymail.com';
SELECT LEFT(#email, 1) + REPLICATE('X',
LEN(SUBSTRING(#email, 2,
CHARINDEX('#', #email) - 1)))
+ SUBSTRING(#email, CHARINDEX('#', #email), LEN(#email));
-- Result: KXXXXXX#ymail.com
This uses the REPLICATE() method, which according to the docs online starts with SQL Server 2008.
REPLICATE (Transact-SQL)
Repeats a string value a specified number of times.
REPLICATE ( string_expression ,integer_expression )

SELECT LEFT(col, 1) + RIGHT('xxxxxxxxxxxxxxxx', CHARINDEX('#', col) - 2) +
SUBSTRING(col, CHARINDEX('#', col), LEN(col) - CHARINDEX('#', col) + 1);
Explanation:
Take Kiyara#ymail.com as an example, and each piece of my query is shown here.
K LEFT(col, 1)
xxxxx RIGHT('xxxxxxxxxxxxxxxx', CHARINDEX('#', col) - 2)
#ymail.com SUBSTRING(col, CHARINDEX('#', col), LEN(col) - CHARINDEX('#', col) + 1)
Note that you can replace the call to RIGHT() with a string of x's long enough to match your longest expected email name.

Try it's
select substring('Akira#zmail.com',1,1) + REPLICATE('x',charindex('#','Akira#zmail.com')) + SUBSTRING('Akira#zmail.com',charindex('#','Akira#zmail.com'),200)
Change the email by column or whatever you want

declare #n nvarchar(max)
set #n='Kiyara#ymail.com'
select concat(substring(#n,1,1),replicate('X',len( substring(#n,2,charindex('#',#n,1)-2))),SUBSTRING(#n,charindex('#',#n),200))
In this with the help o substring you have found first letter ans then rest of he letter til # i.e iyara is searched using charindex ans substring,which will be converted into 'x' using replicate function and then rest id printed.

Related

How trim() works in SQL Server

I have a column SysTraNo with some specific data like
HO/20-21/DRP/0001
215/21-22/AGP/0003
I want to trim that whole column and only take 20-21 or 21-22 of that data. How can I do this?
Using the base string functions we can try:
SELECT val,
SUBSTRING(val,
CHARINDEX('/', val) + 1,
CHARINDEX('/', val, CHARINDEX('/', val) + 1) -
CHARINDEX('/', val) - 1) AS nums
FROM yourTable;
Demo
The logic here is to take a substring starting from the character after the first / until the character before the second /. On other databases, we could have used regular expressions, but SQL Server has no native support for this.
In sql server trim works like below.
As one example is above Here I have given another example using substring on email to extract the domain part.
SELECT
email,
SUBSTRING(
email,
CHARINDEX('#', email)+1,
LEN(email)-CHARINDEX('#', email)
) domain
FROM
practice
ORDER BY
email;

SQL: What are phone number formatting options?

SQL: What are phone number formatting options?
I query my SQL Database for a phone number:
816-123-4567
I want to output it is as:
816 123-4567.
What steps do I take to achieve this result in an SQL environment?
A standard SQL solution would be:
select substring(phone, 1, 3) || ' ' || substring(phone, 5, 8)
There may be simpler solutions in other databases. For instance, in SQL Server:
select stuff(phone, 4, 1, ' ')
And in MySQL:
select insert(phone, 4, 1, ' ')
Note: These are specific the the format you have provided in your question. If you have other formats, then you might need more complicated logic.
Use SUBSTRING and CHARINDEX functions.
SUBSTRING gets a part of the string according to given indexes.
CHARINDEX gets the index of the character that triggers your String separation.
Below is an MSSQL Server query.
According to your DBMS, the function names can vary, but the logic will is the same.
Query :
SELECT
SUBSTRING('816-123-4567', 0, CHARINDEX('-', '816-123-4567') ) +
' ' +
SUBSTRING('816-123-4567', CHARINDEX('-', '816-123-4567') + 1 , LEN('816-123-4567') -
CHARINDEX('-', '816-123-4567') )
And the result :
816 123-4567
When we put your field and table name instead of static values :
SELECT
SUBSTRING(YourPhoneField, 0, CHARINDEX('-', YourPhoneField) ) +
' ' +
SUBSTRING(YourPhoneField, CHARINDEX('-', YourPhoneField) + 1 , LEN(YourPhoneField) -
CHARINDEX('-', YourPhoneField) )
FROM YourTableName

Get substring after and before '-' character

I have this kind of strings in a table:
AM-65-800
AM-75/86-650
D-27-600
What I'm trying to do is to get only the middle part, for example:
65
75/86
27
Basically the substring after and before the '-' Char
I tried this (from an example in here):
SELECT SUBSTRING(Product.Name, LEN(LEFT(Product.[Name], CHARINDEX ('-', Product.[Name]))) + 1, LEN(Product.[Name]) - LEN(LEFT(Product.[Name], CHARINDEX ('-', Product.[Name]))) - LEN(RIGHT(Product.[Name], LEN(Producto.[Name]) - CHARINDEX ('-', Product.[Name]))) - 1)
FROM Product
But it gives me this error:
[42000] [Microsoft][SQL Server Native Client 10.0][SQL Server]Invalid length parameter passed to the LEFT or SUBSTRING function. (537)
Being honest, I don't know how to solve the error because I don't understand the solution from the example. what can I do?
Thanks.
One option is ParseName()
Example
Declare #S varchar(max)='AM-65-800'
Select parsename(replace(#S,'-','.'),2)
Returns
65
Use SUBSTRING with start and lengths calculated based on position of the '-':
SELECT SUBSTRING('ABC-65-DEF',
CHARINDEX('-', 'ABC-65-DEF') + 1,
CHARINDEX('-', 'ABC-65-DEF', CHARINDEX('-', 'ABC-65-DEF') + 1) - CHARINDEX('-', 'ABC-65-DEF') - 1)
Basically, it finds the first instance of the '-' (CHARINDEX('-', 'ABC-65-DEF')) and then the second instance of '-' (CHARINDEX('-', 'ABC-65-DEF', CHARINDEX('-', 'ABC-65-DEF') + 1)) and grabs the substring inbetween.
This will work to find the middle part on older SQL Servers up to and including 2008.
DECLARE #SomeText VARCHAR(100) = 'AM-75/86-650'
SELECT LEFT(RIGHT(#SomeText, LEN(#SomeText) - CHARINDEX('-', #SomeText)),
CHARINDEX('-', RIGHT(#SomeText, LEN(#SomeText) - CHARINDEX('-', #SomeText)))-1)
Returns: '75/86'

How to get a id from a url using SQL QUERY. The ID changes dynamically. the database is Sql server 2008

I have url like:
http://mysites.xyz.com/_layouts/ng/ActivityStream.aspx/id/2624D92223261D370D7287C9E83CAEEA/Activity%20Stream%20Post.
I need to get the 2624D92223261D370D7287C9E83CAEEA. But not able to do so. my current query is
SUBSTRING(#URL2,LEN('http://mysites.xyz.com/_layouts/ng/ActivityStream.aspx/id/'),LEN(#URL2)-LEN('http://mysites.xyz.com/_layouts/ng/ActivityStream.aspx/id/')- CHARINDEX('/',REVERSE(#URL2))))
Please suggest.
Try this:
SELECT SUBSTRING(
#URL2,
CHARINDEX('/id/', #URL2)+4,
CHARINDEX('/', #URL2, CHARINDEX('/id/', #URL2)+5)
- (CHARINDEX('/id/', #URL2)+4))
Note: This assumes that the id is always followed by at least one more slash.
Breakdown:
The first argument of the substring is the string that contains the full expression.
The second one is the first index after /id/.
The third one is the desired length - calculated by the first index of / after /id/ - the first index after /id/.
update
To cope with strings that does not contain a slash after the id value, use case:
SELECT SUBSTRING(
#URL,
CHARINDEX('/id/', #URL)+4,
CASE WHEN CHARINDEX('/', #URL, CHARINDEX('/id/', #URL)+5) > 0 THEN
CHARINDEX('/', #URL, CHARINDEX('/id/', #URL)+5)
- (CHARINDEX('/id/', #URL)+4)
ELSE
LEN(#URL)
END
)
If the structure is always the same then you can try:
SELECT REPLACE(RIGHT(#s, LEN(#s) - 58), '/Activity%20Stream%20Post', '')
or:
SELECT REPLACE(REPLACE(#s,'/Activity%20Stream%20Post', ''), 'http://mysites.xyz.com/_layouts/ng/ActivityStream.aspx/id/', '')

Substring with conditional statement

tl;dr
I don't understand how to conditionally change the length parameter of SUBSTRING(..)
Short enough, did read
I've got a text field in a sql table that I want to retrieve a substring from
There is a specific part of text I am having trouble retrieving a substring from, because I cannot guarantee the next string.
For example, I have:
... Tracking Code : /a/delimited/string AttributeW : ValueW ...
And
... Tracking Code : /a/different/delimited/string A random string ...
From both of those i want /a/delimited/string and /a/different/delimited/string respectively
My current sql looks something like:
DECLARE #TrackingStartStr VARCHAR(50), #TrackingEndStr VARCHAR(50)
SET #TrackingStartStr = 'Tracking Code :'
SET #TrackingEndStr = 'Some string that indicates the text is about to end'
SELECT
AField
,RTRIM(LTRIM(Substring(CAST([Body] AS VARCHAR(MAX))
,Charindex(#TrackingStartStr,CAST([Body] AS VARCHAR(MAX))) + LEN(#TrackingStartStr)
,charindex(#TrackingEndStr,CAST([Body] AS VARCHAR(MAX))) - (Charindex(#TrackingStartStr,CAST([Body] AS VARCHAR(MAX))) + LEN(#TrackingStartStr))
))) AS TrackingCode
From tbl_stupidTextTable
I don't know how to conditionally change what #TrackingEndStr is for each row.
Try:
select substring(stringfield,
charindex('/', stringfield, 1),
charindex(' ',
stringfield,
charindex('/', stringfield, 1)) -
charindex('/', stringfield, 1)) as val
from tbl
SQL Fiddle demo: http://sqlfiddle.com/#!6/cebab/16/0