Replacing certain character in email addresses with '*' in an SQL query - sql

From example#gmail.com
exam***#gm***.com
Can anyone mask the email using SQL query.AS show above from example#gmail.com and convert it to
exam***#gm***.com

DECLARE #String VARCHAR(100) = 'example#gmail.com'
SELECT LEFT(#String, 3) + '*****#'
+ REVERSE(LEFT(RIGHT(REVERSE(#String) , CHARINDEX('#', #String) +2), 2))
+ '******'
+ RIGHT(#String, 4)
Result
exa*****#gm******.com
Just thought of another simpler solution
SELECT LEFT(#String, 3) + '*****#'
+ SUBSTRING(#String, CHARINDEX('#',#String)+1,2)
+ '*******'
+ RIGHT(#String, 4)
This will also give you the exact same results.

while insert you can do same from c# code.
try this with other example,
Declare #input varchar(50)='example#gmail.com '
select left(#input,4)+replicate('*',len(substring(#input,5,charindex('#',#input)-5)))
+substring(#input,charindex('#',#input),3)
+replicate('*',len(substring(#input,charindex('#',#input)+3,len(#input)-charindex('.',#input))))
+substring(#input,charindex('.',#input),len(#input))

may be a multi replace works, an alternative masking the vocals:
update table set mail = replace(replace(mail,'a','*') ,'e','*') and go on

Related

Need output of substring sql without using reverse

I have a SQL query which is returning output; I need to get substring with last occurrence, I am confused and not able to find out how to do that:
SELECT
spsetuppath,
SUBSTRING(spsetuppath, 0, (LEN(spsetuppath) - CHARINDEX('\', RTRIM(LTRIM(REVERSE(spsetuppath))))) + 1)
FROM
UMRdb..sql_spversion
WHERE
bitversion = '64'
AND productversion = ' 10.50.2500.0'
Output of this command is as follows
For the last column, I need the path before exe and I need only exe file name - how to do that?
So output I am expecting should look like this
SQLServer2008R2SP1-KB2528583-x64-ENU.exe
select right(spsetuppath, charindex('\', reverse(spsetuppath) + '\') - 1) from UMRdb..sql_spversion where bitversion='64' and
productversion=' 10.50.2500.0 '
this worked awesome.
This should help you:
SELECT
spsetuppath,
SUBSTRING(spsetuppath, CHARINDEX('\', RTRIM(LTRIM(REVERSE(spsetuppath)))) - 1, 150)
FROM
UMRdb..sql_spversion
WHERE
bitversion = '64'
AND productversion = ' 10.50.2500.0'
Example:
DECLARE #string varchar(256) = '\\blablabla\dbms\MSSQL\SQL SERVER 2008\SQLServer2008R2SP1-KB2528583-x64-ENU.exe'
SELECT SUBSTRING(#string, CHARINDEX('\', RTRIM(LTRIM(REVERSE(#string)))) - 1, 150)
EDIT: If you want to avoid SUBSTRING function use this:
DECLARE #string varchar(256) = '\\blablabla\dbms\MSSQL\SQL SERVER 2008\SQLServer2008R2SP1-KB2528583-x64-ENU.exe'
SELECT REVERSE(LEFT(REVERSE(#string),CHARINDEX('\', RTRIM(LTRIM(REVERSE(#string)))) - 1))

SQL Server query to delete text from text column

I have a SQL Server database with a table feedback that contains a text column comment. In that column I have tag data, for example
This is my record <tag>Random characters are here</tag> with information.
How do I write a query to update all of these records to remove the <tag></tag> and all of the text in between?
I'd like to write this to a different 'temporary' table to first verify the changes and then update the original table.
I am running SQL Server 2014 Express.
Thank you
Here is a function to remove tags..
CREATE FUNCTION [dbo].[RemoveTag](#text NVARCHAR(MAX), #tag as nvarchar(max))
RETURNS NVARCHAR(MAX)
AS
BEGIN
declare #startTagIndex as int
declare #endTagIndex as int
set #startTagIndex = CHARINDEX('<' + #tag + '>', #text)
if(#startTagIndex > 0) BEGIN
set #endTagIndex = CHARINDEX('</' + #tag + '>', #text, #startTagIndex)
if(#endTagIndex > 0) BEGIN
return LEFT(#text, #startTagIndex - 1) + RIGHT(#text, len(#text) - len(#tag) - #endTagIndex - 2)
END
END
return #text
END
Later you can use it like:
Update table set field = dbo.RemoveTag(field, 'tag')
If you want to write fields to other table then:
CREATE TABLE dbo.OtherTable (
OtherField nvarchar(MAX) NOT NULL
)
GO
INSERT INTO OtherTable (OtherField)
SELECT dbo.RemoveTag(field, 'tag') from table
Making a lot assumptions about the format of your string. But if they're valid then this is very simple:
left(s, charindex('<tag>', s - 1)) +
substring(s, charindex('</tag>', s) + 6, len(s))
Obviously we're basically assuming that the search strings appear only once and in the correct order. There's also an assumption that there will be matches. Also, I used len(s) as an easy upper bound on the number of characters to take from the right. You could just hard-code something appropriate if you felt like it since SQL Server doesn't error for going past the end. s is just a stand in for your char column.
http://sqlfiddle.com/#!3/771a3/8
Not sure if extra whitespace is going to be an issue so you might want to trim and add a space character in the middle.
rtrim(left(s, charindex('<tag>', s) - 1)) + ' ' +
ltrim(substring(s, charindex('</tag>', s) + 6, len(s)))
You can use CHARINDEX to find where your tags start and stop, SUBSTRING to get all text between < and >, and REPLACE to swap out the substring for ''.
Select Field,
Substring(FIELD, charindex('<', Field), CHARINDEX('>', Field,
(CHARINDEX('>', FIELD)) + 1) - charindex('<', Field)+1) as ToRemove,
replace (Field, Substring(FIELD, charindex('<', Field), CHARINDEX('>',
Field, (CHARINDEX('>', FIELD)) + 1) - charindex('<', Field)+1), '')
as FinalResult
from TableName
The output will be three columns, Field, ToRemove and FinalResult, but nothing will actually be updated.
I think the only way this will fail is if you have nested tags. <b><i>sometext</i></b>
To actually make the change:
Update #TableName set Field = replace (Field, Substring(FIELD, charindex('<', Field), CHARINDEX('>', Field, (CHARINDEX('>', FIELD)) + 1) - charindex('<', Field)+1), '')
Tested on SQL Server 2012.

Delete the last part of a string SQL

I have a field named Path and it looks like this:
/{e8cfdcba-9572-4c64-828f-dea54d8a00b7}/sites/BI_Center/euroreporting/Reports/BITraining/Elena/GroupingEx.rdl
I need a parameter from where i can choose a folder name. Something like this:
/sites/BI_Center/euroreporting/Reports/BITraining/Elena
What i have done by now is to delete the first bit of the path. This is the code:
SELECT replace(reverse(substring(reverse(Path), 1, ISNULL(NullIF(charindex('}',reverse(Path)),0),len(Path))) ),'}','') AS Path2 from Catalog
Now, my path looks like this: /sites/BI_Center/euroreporting/Reports/BITraining/Elena/GroupingEx.rdl
How can i exclude the report's name? (for example GroupingEx.rdl). I tried the MID function, but it doesn't work because the report's name length is variable.
Thank you in advance.
This is one of the methods
declare #s varchar(200)
set #s='/sites/BI_Center/euroreporting/Reports/BITraining/Elena/GroupingEx.rdl'
select reverse(replace(reverse(#s),substring(reverse(#s),1,charindex('/',reverse(#s))),''))
EDIT:
This is much simpler
declare #s varchar(200)
set #s='/sites/BI_Center/euroreporting/Reports/BITraining/Elena/GroupingEx.rdl'
select substring(#s,1,len(#s)-charindex('/',reverse(#s)))
I suggest this function:
SUBSTRING([path], CHARINDEX('/', [path], 2),
LEN([path]) - CHARINDEX('/', [path], 2) - CHARINDEX('/', REVERSE([path]), 1) + 1)
for this: /sites/BI_Center/euroreporting/Reports/BITraining/Elena
declare #str varchar(250)
set #str = '/{e8cfdcba-9572-4c64-828f-dea54d8a00b7}/sites/BI_Center/euroreporting/Reports/BITraining/Elena/GroupingEx.rdl'
select substring (
#str, --expression
charindex('}', #str) + 1, --start
len(#str) - charindex('/', reverse(#str)) - charindex('}', #str) --how many characters of the expression will be returned
)
OutPut:
/sites/BI_Center/euroreporting/Reports/BITraining/Elena

Extracting text between two characters in SQL

I am trying to extract the text between two characters using t-sql. I have been able to write it where it pulls the information close to what I want, but for some reason I am not getting what i am expecting(suprise, suprise). Could really use alittle help refining it. I am trying to extract part of the table name that is located between two [ ]. An example of the column data is as follows(this is a table that records all changes made to the database so the column text is basically SQL statements):
ALTER TABLE [TABLENAME].[MYTABLE] ADD
[VIP_CUSTOMER] [int] NULL
I am trying to extract part of the table name, in this example I just want 'MYTABLE'
Right now I am using:
select SUBSTRING(db.Event_Text, CHARINDEX('.', db.Event_Text) + 2, (CHARINDEX(']', db.Event_Text)) - CHARINDEX('', db.Event_Text) + Len(']')) as OBJName
FROM DBA_AUDIT_EVENT DB
WHERE DATABASE_NAME = 'XYZ'
But when I use this, I don't always get the results needed. Sometimes I get 'MYTABLE] ADD' and sometimes I get the part of the name I want, and sometimes depending on the length of the tablename I only get part the first part of the name with part of the name cut off at the end. Is there anyway to get this right, or is there a better way of writing it? Any help would be greatly appreciated. Thanks in advance.
Long, but here's a formula using the brackets:
Declare #text varchar(200);
Select #text='ALTER TABLE [TABLENAME].[MYTABLE] ADD [VIP_CUSTOMER] [int] NULL';
Select SUBSTRING(#text,
CHARINDEX('[', #text, CHARINDEX('[', #text) + 1 ) +1,
CHARINDEX(']', #text, CHARINDEX('[', #text, CHARINDEX('[', #text) + 1 ) ) -
CHARINDEX('[', #text, CHARINDEX('[', #text) + 1 ) - 1 );
Replace #text with your column name.
Give this a shot:
select SUBSTRING(db.Event_Text, CHARINDEX('.', db.Event_Text) + 2
, CHARINDEX(']', db.Event_Text) - 2) as OBJName
FROM DBA_AUDIT_EVENT DB
WHERE DATABASE_NAME = 'XYZ'
this is a pretty ugly way to get the length, but I've used something like this before:
select SUBSTRING(db.Event_Text,
CHARINDEX('.', db.Event_Text) + 2,
charindex('] ADD',db.Event_Text) - CHARINDEX('.',db.Event_Text)-2))
Give it a try, it may work for you.

Search and Replace Serialized DB Dump

I am moving a database from one server to an other and have lots of serialized data in there. So, I am wondering:
Is it possible to use regex to replace all occurrences like the following (and similar)
s:22:\"http://somedomain.com/\"
s:26:\"http://somedomain.com/abc/\"
s:29:\"http://somedomain.com/abcdef/\"
to
s:27:\"http://someOtherdomain.com/\"
s:31:\"http://someOtherdomain.com/abc/\"
s:34:\"http://someOtherdomain.com/abcdef/\"
If that column, that holds these data, is of the same length, and these occurrences 22, 26, 29,... are at the same position from the beginning of the string. Then, for SQL Server, you can use REPLACE , SUBSTRING with CHARINDEX to do that:
DECLARE #s VARCHAR(50);
DECLARE #sub INT;
SET #s = 's:27:\"http://somedomain.com/\"';
SET #sub = CONVERT(INT, SUBSTRING(#s, CHARINDEX(':', #s) + 1, 2));
SELECT REPLACE(REPLACE(#s, 'somedomain', 'someOtherdomain'), #sub, #sub + 5);
So s:number:\"http://somedomain.com/\" will become s:number + 5:\"http://someOtherdomain.com/\".
If you want to run an UPDATE against that table you can write it this way:
UPDATE #t
SET s = REPLACE(REPLACE(s, 'somedomain', 'someOtherdomain'),
CONVERT(INT, SUBSTRING(s, CHARINDEX(':', s) + 1, 2)),
CONVERT(INT, SUBSTRING(s, CHARINDEX(':', s) + 1, 2)) + 5);
What does this query do, is that, it searches for the occurrence of somedomain and replaces it with someOtherdomain, get the number between the first two :'s, convert it to INT and replace it with the same number + 5. The following is how your data should looks like after you run the previous query:
s:27:\"http://someOtherdomain.com/\"
s:31:\"http://someOtherdomain.com/abc/\"
s:34:\"http://someOtherdomain.com/abcdef/\"
Here is a Live Demo.