mysql query to fetch only hex ascii data? - mysql

I need a MySQL query to get the below expected output processing the above input data??(in my data i get junk data ranging from \x128 to \x160(Hex data) ASCII characters.).so,I need a regex pattern to fetch only the data that contains hex values and remaining all the values to be NULL except the key column Name.
Input data :
**NAME PHONE ADDRESS**
anu 345#2 hyderabad
vinu 1234 raj^am
ram 234 vizag
kheer 233&3 vz1m
palni 1333 rap#d
Required output data:
**NAME PHONE ADDRESS**
anu 345#2 NULL
vinu NULL raj^am
kheer 233&3 NULL
plain NULL rap#d

you have to use 2 query for this result first on PHONE column and 2nd is ADDRESS Column
1.UPDATE test1 SET Phone=NULL WHERE Phone REGEXP '[#^]'
2.UPDATE test1 SET Address=NULL WHERE Address REGEXP '[#^]'

HEX(phone) REGEXP '^(..)*[89ABCDEF]'
will match any phone with any non-Ascii bytes.
I suspect you did not mean x128, nor x160. Those numbers look more like decimal. If you want to catch >= 128 and < 160 (note: not <= 160):
HEX(phone) REGEXP '^(..)*[89]'
The REGEXP says:
^ -- anchored at start
(..)* -- skip any number of pairs of characters (remember they are hex)
[89] -- match 8x or 9x
-- ignore the rest of the column

Related

How to retrieve the rows from mysql whose field value should start with alphabet?

I'm trying to retrieve the rows from mysql table, whose field values should start with alphabeic letter and the value may contain numeric values along with letters.
Eg:
==
id : BKDyriu3
id : 12387545
id : YDfhety7
How can I retrieve id field which starts with letter ?
Can anyone please help me out...
We can use ASCII() function.
Returns the numeric value of the leftmost character of the string str.
Returns 0 if str is the empty string. Returns NULL if str is NULL
SELECT * FROM your_table
WHERE ASCII(id) BETWEEN 65 AND 90
OR
ASCII(id) BETWEEN 97 AND 122
Uppercase A-Z has ASCII value range from 65 to 90.
Lowercase a-z has ASCII value range from 97 to 122.

Sql Query to fetch invalid entries

I want a sql query to fetch invalid entries in ph_no such as entries which has text(a-z) or special characters or which are not 10 digit long or entries which are 10 digit long but has special character or text in it
I have used the following code
SELECT PH_NO FROM Table WHERE LENGTH(PH_NO)<=9
It is only fetching entries which are not 10 digit long but i want entries which are 10 digit long but contains text or special character as well
You can try using regular expresion in your where clause, you get when PH_NO length is less than 10, or when the length is 10 and is not in the regular expresion:
SELECT PH_NO
FROM table1
WHERE LENGTH(PH_NO)<=9 OR
(LENGTH(PH_NO)=10 AND PH_NO NOT REGEXP '^[[:digit:]]{10}$')
This query will help you achieving the specifics given by you
SELECT PH_NO FROM Table WHERE LENGTH(convert(PH_NO,signed))!=10;
The conver() will return 1 or 0 if PH_NO contains any character, so if your 10 digit PH_NO will contain any invalid entry than this query will give the right output.

how to check whether a string has exactly n words in mysql

Like my table consist of name street and city .
My query is to find customer name whose street address match any string of 1)exactly 3 character 2)at least 3 character
You can use String function - Length.
1)Exactly 3 character
SELECT customer_name,street_address FROM TABLE WHERE LENGTH(street_address ) = 3
2)At least 3 character
SELECT customer_name,street_address FROM TABLE WHERE LENGTH(street_address ) >= 3
Hope this helps.
It should help you: WHERE CustomerName LIKE 'a_%_%' Finds any values that start with "a" and are at least 3 characters in length

I need a trigger to create id's in my sql database with a string and some zeros

I'm currently using this trigger which adds id's with 3 zeros and two zeros and then the id from the sequences table.
BEGIN
INSERT INTO sequences VALUES (NULL);
SET NEW.deelnemernr = CONCAT('ztmr16', LPAD(LAST_INSERT_ID(), 3, '0'));
END
I changed the 3 to 4 but then it didn't increment the id anymore, resulting in and multiple id error. It stayed at ztmr16000. So what can I do to add more zeros and still get the id from the sequencestable?
The MySQL LPAD function limits the number of characters returned to the specified length.
The specification is a bit unclear, what you are trying to achieve.
If I need a fixed length string with leading zeros, my approach would be to prepend a boatload of zeros to my value, and then take the rightmost string, effectively lopping off extra zeros from the front.
To format a non-negative integer value val into a string that is ten characters in length, with the leading characters as zeros, I'd do something like this:
RIGHT(CONCAT('000000000',val),10)
As a demonstration:
SELECT RIGHT(CONCAT('000000000','123456789'),10) --> 0123456789
SELECT RIGHT(CONCAT('000000000','12345'),10) --> 0000012345
Also, I'd be cognizant of the maximum length allowed in the column I was populating, and be sure that the length of the value I was generating didn't exceed that, to avoid data truncation.
If the value being returned isn't be truncated when it's inserted into the column, then what I think the behavior you observe is due to the value returned from LAST_INSERT_ID() exceeding 1000.
Note that for a non-negative integer value val, the expression
LPAD(val,3,'0')
will allow at most 1000 distinct values. LPAD (as I noted earlier) restricts the length of the returned string. In this example, to three characters. As a demonstration of the behavior:
SELECT LPAD( 21,3,'0') --> 021
SELECT LPAD( 321,3,'0') --> 321
SELECT LPAD( 54321,3,'0') --> 543
SELECT LPAD( 54387,3,'0') --> 543
There's nothing illegal with doing that. But you're going to be in trouble if you depend on that to generate "unique" values.
FOLLOWUP
As stated, the specification ...
"adds id's with 3 zeros and two zeros and then the id from the sequences table."
is very unclear. What is it exactly that you want to achieve? Consider providing some examples. It doesn't seem like there's an issue concatenating something to those first five fixed characters. The issue seems to be with getting the id value "formatted" to your specification
This is just a guess of what you are trying to achieve:
id value formatted return
-------- ----------------
1 0001
9 0009
22 0022
99 0099
333 0333
4444 4444
55555 55555
666666 666666
You could achieve that with something like this:
BEGIN
DECLARE v_id BIGINT;
INSERT INTO sequences VALUES (NULL);
SELECT LAST_INSERT_ID() INTO v_id;
IF ( v_id <= 9999 ) THEN
SET NEW.deelnemernr = CONCAT('ztmr16',LPAD(v_id,4,'0'));
ELSE
SET NEW.deelnemernr = CONCAT('ztmr16',v_id);
END IF;
END

Transforming a column to have 10 Digits

I have a csv file that contains phone numbers, some of them have 9 digits and some of them have 10. Is there a command that would allow the transformation of the column such that numbers that have only 9 digits will have a 0 appended in front of the numbers.
For example,
if the column has values "443332332" and "0441223332", I would like to have the value of the one with 9 digits changed to "0443332332"?
Sorry, I should have elaborated.
I was wondering if there was a command to do it in SQLlite easily? I prefer not to use excel to transform the column as if I can get it to working with sqllite it would be so much easier and faster.
A more generic solution would be:
select substr('0000000000'||'1234567', -10, 10) from table_name;
The above query would always return 10 digits and add leading zeroes to the missed out number of digits.
For example, the above query would return : 0001234567
For Update, use
UPDATE TABLE_NAME SET PHONE_NO = substr('0000000000'|| PHONE_NO, -10, 10);
If you're sure that just prepending a zero on strings with length 9 will work for your application, something simple will work:
SELECT CASE WHEN LENGTH(phone_number) = 9 THEN '0'||phone_number
ELSE phone_number
END AS phone_number
FROM your_table
;
You could also update the table, depending on your needs:
UPDATE your_table
SET phone_number = '0'||phone_number
WHERE LENGTH(phone_number) = 9
;
Open the .csv using Excel,
Add a filter to the column,
Sort from A-Z to get all the columns with 9 digits,
Then follow the steps here
http://office.microsoft.com/en-au/excel-help/keep-leading-zeros-in-number-codes-HA010342581.aspx