display all column with one column masked - sql

I have two columns - name and ccNumber. I want to display both the column with one of them masked.
This query is showing only one column but I want all column to be displayed:
declare #t table (card_no varchar(20))
insert into #t
select ccNUMBER from ccinfo
select 'XXXX-XXXX-XXXX-'+ substring(card_no, 13, 4) as card_no from #t
i want name column with ccnumber column to be masked

You can try below
declare #t table (name varchar(100),card_no varchar(20))
insert into #t
select name, ccNUMBER from ccinfo
select name, 'XXXX-XXXX-XXXX-'+ substring(card_no, 13, 4) as card_no from #t

check the following example. i think you need concatenation of columns.
create table demo
(
firstName varchar(30),
secondName varchar(30)
);
insert into demo
values
('soumyajit', 'chatterjee'),
('papai', 'chatterjee'),
('virat', 'kohli');
select concat(firstName, ' ', secondName)as Name from demo;

You need to get the second column, too:
declare #t table (card_no varchar(20), name varchar(64));
insert into #t
select ccNUMBER, name from ccinfo
select 'XXXX-XXXX-XXXX-'+ substring(card_no, 13, 4) as card_no, name from #t
or just use the original table:
select 'XXXX-XXXX-XXXX-'+ substring(ccNUMBER, 13, 4) as card_no, name from ccinfo

Related

SQL server, Find all columns that are contained in a string

Suppose I have a table:
Id int,
Name varchar(10)
with values:
(1, Nick), (2, Mike), (3, Eric)
I want to return all the names that are contained in a string. For example "Nick, Mick" would return Nick and Mike.
I've tried with LIKE but it works the other way around returning the values that contain a string.
I was hoping for somthing like this, but that it actually works.
SELECT * FROM table
WHERE Name.isContained("Nick, Mike");
Result:
1, Nick and 2, Mike
Try this:
SELECT Name
FROM mytable
WHERE PATINDEX('%'+Name+'%', 'Nick, Mike') > 0
Demo here
Try this:
select name from tbl_sound where SOUNDEX(`name`) like '%00%'
Demo. Finds exact names match
create table demo (
Id int,
Name varchar(10));
insert demo
values
(1, 'Nick'), (2, 'Mike'), (3, 'Eric');
declare #prm varchar(200) = 'Nick,Mike,Ann';
select *
from demo
where ','+ #prm +',' like '%,'+Name+',%'
;
Check This.
declare #tmp nvarchar(250)
SET #tmp = ''
select #tmp = #tmp + concat(ID,' , ',name,' and ')
from table
where Name in ('Nick','Mike')
select SUBSTRING(#tmp, -2, LEN(#tmp))
OutPut :

Remove value from string where it match another column

I need to remove a file path from the beginning of a column. I have table with 3 columns one has just the path I want to remove.
So I have a table like:
Is there any way in SQL Server 2008 that I can remove the text where it matches the path column?
Thanks
Try this
UPDATE Table x
SET x.dataconv = SUBSTRING(x.dataconv,LEN(x.path),LEN(x.dataconv))
WHERE SUBSTRING(x.dataconv,1,LEN(x.path)) == x.path
You could try using replace?
DECLARE #table TABLE (id INT, dataconv VARCHAR(500), [path] VARCHAR(500));
INSERT INTO #table SELECT 1, 'C:\windows test_data_1', 'C:\windows';
INSERT INTO #table SELECT 2, 'C:\windows\test.ini test_data_2', 'C:\windows\test.ini';
INSERT INTO #table SELECT 3, 'C:\word docs\doc.ini test_data_3', 'C:\word docs\doc.ini';
INSERT INTO #table SELECT 4, '/P/TestUser/dfg002/SinglePageCV''s/JoeBloggs.doc33216root;tuser002_admin#IEroot;D‌​omain:Users#IE431104 1330820929 1003750078 1055597658', '/P/TestUser/dfg002/SinglePageCV''s/JoeBloggs.doc';
SELECT id, LTRIM(REPLACE(dataconv, [path], '')) FROM #table;
Returns:
1 test_data_1
2 test_data_2
3 test_data_3
4 33216root;tuser002_admin#IEroot;D??omain:Users#IE431104 1330820929 1003750078 1055597658

Can a Pivot table be used with a unknown number of columns?

If I have a team table with a unknown amount of members, is there a way to make the pivot query dynamic?
create table #t (
team varchar (20), member varchar (20)
)
insert into #t values ('ERP', 'Jack')
insert into #t values ('ERP', 'John')
insert into #t values ('ERP', 'Mary')
insert into #t values ('ERP', 'Tim')
insert into #t values ('CRM', 'Robert')
insert into #t values ('CRM', 'Diana')
select * from #t
select team, [1] as teamMember1, /* 1st select */
[2] as teamMember2, [3] as teamMember3
from
(select team , member, row_number () /* 3rd select */
over (partition by team order by team) as rownum
from #t) a
pivot (max(member) for rownum in ([1], [2], [3])) as pvt
drop table #t
Why yes, yes there is. Here's a script I cooked up years ago for a similar problem that was ultimately solved by giving the user Excel and washing my hands of it. I apologize it's not configured with your example data, but hopefully it's easy to follow.
Hope that helps,
John
--------------START QUERY--------------
-- Example Table
CREATE TABLE #glbTestTable
(
ProviderID INT,
Total INT,
PaymentDate SMALLDATETIME
)
--So the dates insert properly
SET DATEFORMAT dmy
-- Populate Example Table
INSERT INTO #glbTestTable VALUES (232, 12200, '12/01/09')
INSERT INTO #glbTestTable VALUES (456, 10200, '12/01/09')
INSERT INTO #glbTestTable VALUES (563, 11899, '02/03/09')
INSERT INTO #glbTestTable VALUES (221, 5239, '13/04/09')
INSERT INTO #glbTestTable VALUES (987, 7899, '02/03/09')
INSERT INTO #glbTestTable VALUES (1, 1234, '02/08/09')
INSERT INTO #glbTestTable VALUES (2, 4321, '02/07/09')
INSERT INTO #glbTestTable VALUES (3, 5555, '02/06/09')
-- Raw Output
SELECT *
FROM #glbTestTable
-- Build Query for Pivot --
DECLARE #pvtColumns VARCHAR(MAX)
SET #pvtColumns = ''
-- Grab up to the first 1023 "Columns" that we want to use in Pivot Table.
-- Tables can only have 1024 columns at a maximum
SELECT TOP 1023 #pvtColumns = #pvtColumns + '[' + CONVERT(VARCHAR, PaymentDate, 103) + '], '
FROM (SELECT DISTINCT PaymentDate FROM #glbTestTable) t_distFP
-- Create PivotTable Query
DECLARE #myQuery VARCHAR(MAX)
SET #myQuery = '
SELECT ProviderID, ' + LEFT(#pvtColumns, LEN(#pvtColumns) - 1) + '
FROM (SELECT ProviderID, PaymentDate, Total
FROM #glbTestTable) AS SourceTable
PIVOT
(
SUM(Total)
FOR PaymentDate IN (' + LEFT(#pvtColumns, LEN(#pvtColumns) - 1) + ')
) AS PivotTable'
-- Run the Pivot Query
EXEC(#myQuery)
-- Cleanup
DROP TABLE #glbTestTable
---------------END QUERY---------------

Generate a new ID which is not taken

DROP TABLE #ABC
CREATE TABLE #ABC (ID INT, Name VARCHAR (2))
INSERT INTO #ABC (ID, NAME)
VALUES (1,'01'),(1,'F5'),(1,'05'),(2,'08'),(2,'G4'),(3,'Y7'),(3,'18')
drop table #XYZ
CREATE TABLE #XYZ (ID INT, Name char(20))
INSERT INTO #XYZ (ID,Name)
SELECT ID, SUBSTRING(REPLACE(CONVERT(VARCHAR(36), NEWID()) , '-', ''), 0, 3) from #ABC
Select * from #XYZ
I want the process to create a new name which is already not taken by the same ID. in other words, same ID (say ID =1) can not have the same "Name" columns. This is just very few records but my real data is huge, I want system to generate only new Names with 2 characters in table#XYZ which are already not taken by the same ID in #ABC. Any help is appreciated. Thanks
I added another temp table #DEF to hold the Names you're generating from NEWID(). It's a little clearer to use SUBSTRING('123456', 1, 2) to obtain the first two characters. I also added a GROUP BY ID so that it will generate only one Name per ID.
Finally, I used a LEFT JOIN and a NULL match to look for #DEF entries that were not already present in #ABC.
DROP TABLE #ABC
CREATE TABLE #ABC (ID INT, Name VARCHAR (2))
INSERT INTO #ABC (ID, NAME)
VALUES (1,'01'),(1,'F5'),(1,'05'),(2,'08'),(2,'G4'),(3,'Y7'),(3,'18')
DROP TABLE #DEF
CREATE TABLE #DEF (ID INT, Name char(20))
INSERT INTO #DEF (ID, Name)
SELECT ID, SUBSTRING(REPLACE(CONVERT(VARCHAR(36), NEWID()) , '-', ''), 1, 2) AS Name
FROM #ABC
GROUP BY ID
DROP TABLE #XYZ
CREATE TABLE #XYZ (ID INT, Name char(20))
INSERT INTO #XYZ (ID, Name)
SELECT #DEF.ID, #DEF.Name
FROM #DEF
LEFT JOIN #ABC ON #ABC.ID = #DEF.ID AND #ABC.Name = #DEF.Name
WHERE #ABC.ID IS NULL
GROUP BY #DEF.ID, #DEF.Name
SELECT * FROM #XYZ
Does this work for you?

SQL Server Simple Group by query

I have a simple problem , Although i believe its simple , am not able to figure out the same.
Consider i have the below table with exactly same data as given below :
CREATE TABLE #temp
(
link varchar(255),
number INT,
fname varchar(255)
)
insert into #temp VALUES ('abc',1,'f1')
insert into #temp VALUES ('abc',2,'f2')
insert into #temp VALUES ('abc',3,'f3')
insert into #temp VALUES ('abc',4,'f6')
insert into #temp VALUES ('abc',10,'f100')
insert into #temp VALUES ('abe',-1,'f0')
insert into #temp VALUES ('abe',1,'f1')
insert into #temp VALUES ('abe',2,'f2')
insert into #temp VALUES ('abe',3,'f3')
insert into #temp VALUES ('abe',4,'f6')
insert into #temp VALUES ('abe',20,'f200')
insert into #temp VALUES ('cbe',-1,'f0')
insert into #temp VALUES ('cbe',1,'f1')
insert into #temp VALUES ('cbe',2,'f2')
insert into #temp VALUES ('cbe',3,'f3')
Now for a given link , i need to get the max 'number' and the corresponding 'fname' which has the max 'number' for the given 'link'.
1)Ex : if link is 'abc' , output should be
abc, 10, f100
2)Ex : if link if 'abe' , Output should be
abe, 20, f200
3)Now link can be also given as a pattern , like (link like 'ab%') , so output should be
abc, 10, f100
abe, 20, f200
4)if (link like 'cb%') , so output should be
cbe, 3, f3
Any help in writing this group by query. I have a solution using CAST and string concat like below , but that seems to be in-efficient.
select link,number,fname from #temp
where link like 'ab%' and link+'_'+CAST(number AS varchar(255))
in (select link+'_'+CAST(MAX(number) AS varchar(255)) from #temp
group by link)
Thanks..
Using a self join:
SELECT x.link,
x.number,
x.fname
FROM #temp x
JOIN (SELECT t.link,
MAX(t.number) AS max_number
FROM #temp t
GROUP BY t.link) y ON y.link = x.link
AND y.max_number = x.number
Using a CTE and ROW_NUMBER (SQL Server 2005+):
WITH cte AS (
SELECT x.link,
x.number,
x.fname,
ROW_NUMBER() OVER(PARTITION BY x.link
ORDER BY x.number DESC) rank
FROM #temp x)
SELECT c.link,
c.number,
c.fname
FROM cte c
WHERE c.rank = 1