MySQL-Masking credit card numbers but keeping the hyphens - masking

I need to return a column of credit card numbers from DB table 'orders' column is 'card_number'. They are in this format on the 'orders' table:
1234-5678-9012-3456
I need the SELECT statement to return with this:
xxxx-xxxx-xxxx-3456
I've found examples without hyphens and adding hyphens where there are none showing this. I'm stumped.

Try this:
SELECT CONCAT('xxxx-xxxx-xxxx-', RIGHT(card_number,4)) FROM orders
RIGHT(card_number,4) gets the last 4 characters of card_number. It is concatenated to the end of the string 'xxxx-xxxx-xxxx-'
See RIGHT and CONCAT

Related

Consider a query to find details of research fields where the first two parts of the ID are D and 2 and the last part is one character (digit)

The ID of research fields have three parts, each part separated by a period.
Consider a query to find the details of research fields where the first two parts of the ID are D and 2, and the last part is a single character (digit).
IDs like D.2.1 and D.2.3 are in the query result whereas IDs like D.2.12 or D.2.15 are not.
The SQL query given below does not return the correct result. Explain the reason why it does not return the correct result and give the correct SQL query.
select *
from field
where ID like 'B.1._';
I have no idea why it doesnt work.
Anyone can help on this? Many thanks
D.2.1 and D.2.3 are in the query result whereas IDs like D.2.12 or D.2.15 are not.
An underscore matches any single character in a LIKE filter so B.1._ is looking for the start of the string followed by a B character followed by a . character then a 1 character then a . character then any single character then the end of the string.
You could use:
SELECT *
FROM field
WHERE ID like 'B.1._%';
The % will match any number of characters (including zero) until the end of the string and the preceding underscore will enforce that there is at least one character after the period.

Comma inside like query fails to return any result

Using Oracle db,
Select name from name_table where name like 'abc%';
returns one row with value "abc, cd" but when I do a select query with a comma before % in my like query, it fails to return any value.
Select name from name_table where name like 'abc,%';
returns no row. How can I handle a comma before % in the like query?
Example:
Database has "Sam, Smith" in the name column when the like has "Sam%" it returns one row, when i do "Sam,%" it doesn't return any row
NOT AN ANSWER but posting it as one since I can't format in a comment.
Look at this and use DUMP() on your own machine... see if this helps.
SQL> select dump('Smith, Stan') from dual;
DUMP('SMITH,STAN')
-----------------------------------------------------
Typ=96 Len=11: 83,109,105,116,104,44,32,83,116,97,110
If you count, the string is 11 characters (including the comma and the space). The comma is character 44, and the space is character 32. If you look at YOUR string and you don't see 44 where the comma should be, you will know that's the problem. You could then let us know what you see there (just for that character, I understand posting "Leno, Jay" would be a violation of privacy).
Also, make sure you don't have any extra characters (perhaps non-printable ones!) right before the comma. Just compare the two strings you are using as inputs and see where the differences may be.

Remove unnecessary Characters by using SQL query

Do you know how to remove below kind of Characters at once on a query ?
Note : .I'm retrieving this data from the Access app and put only the valid data into the SQL.
select DISTINCT ltrim(rtrim(a.Company)) from [Legacy].[dbo].[Attorney] as a
This column is company name column.I need to keep string characters only.But I need to remove numbers only rows,numbers and characters rows,NULL,Empty and all other +,-.
Based on your extremely vague "rules" I am going to make a guess.
Maybe something like this will be somewhere close.
select DISTINCT ltrim(rtrim(a.Company))
from [Legacy].[dbo].[Attorney] as a
where LEN(ltrim(rtrim(a.Company))) > 1
and IsNumeric(a.Company) = 0
This will exclude entries that are not at least 2 characters and can't be converted to a number.
This should select the rows you want to delete:
where company not like '%[a-zA-Z]%' and -- has at least one vowel
company like '%[^ a-zA-Z0-9.&]%' -- has a not-allowed character
The list of allowed characters in the second expression may not be complete.
If this works, then you can easily adapt it for a delete statement.

remove dashes from a column in a select statment without altering table data

I have a query that grabs a list of 'job numbers' from a table. However, the job number displays numbers with a dash in the middle.(e.g. 645-123)
How do I select this field and only retrieve the number up to the dash (e.g.'645') and not the '-123'?
I do not want the data in the table to be 'replaced' or edited; I just need to select the data but without the dash and remaining digits after the dash.
Thanks for any help
You can use something like this:
select (case when jobnum like '%-%'
then left(jobnum, charindex('-', jobnum) - 1)
else jobnum
end)
This will not return an error if there is no hyphen (the reason for the case).

Looking to find a number embedded in a string at some random place - MS SQL Server 2008 R2

I'm converting an old access database to a SQL solution. There is a comment field that the users used for a six digit number (sometimes). It is a free-form field and contains things like "ID# 123456", "123456", "ID #123456". In many cases, however, it just contains random notes that contain some numbers such as dates or other numbers.
What I'd like to do (since this isn't super-critical and is just to get the database started in the direction of properly populating this field), in my select into statement is if there are six consecutive numbers in this comment field insert them into my new field that I'm using specifically for this number.
I've tried various trims and other things but haven't gotten an adequate result.
Thanks!
Try the following
to select only fields with 6 consecutive digits
select * from LegacyTable
where Comment like '%[0-9][0-9][0-9][0-9][0-9][0-9]%'
To extract those six digits from the string
select Comment
substring(Comment,PatIndex('%[0-9][0-9][0-9][0-9][0-9][0-9]%',Comment),6) as nbr
from legacytable
where Comment like '%[0-9][0-9][0-9][0-9][0-9][0-9]%'
Assuming Sql Server is your new database, you could try something like this:
Select *
From LegacyTable
-- Where Comment LIKE '%\d\d\d\d\d\d%' This doesn't work
Where Comment Like '%[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]%'
This should return rows that have 6 numbers consecutively in the Comment column.