Removing white spaces in sql field - mysql

I have a table with three fields (ID , machine_name, carpark_id) two of the machine names has (311A__) _ =spaces and (311B__) how to select and insert rows into machine name which have spaces in it.
sql_local = """SELECT id FROM customer_1.pay_machines WHERE machine_name="%s" """ % machine
sql_local = """INSERT INTO customer_1.pay_machines (machine_name, carpark_id) VALUES ("%s", 0)""" % machine
sql_local = """SELECT id FROM customer_1.pay_machines WHERE machine_name="%s" """ % machine
sql_local = """INSERT INTO customer_1.pay_and_display (plate, machine_id, ticket_datetime, expiry_datetime, ticket_name, ticket_price) VALUES ("%s", "%s", "%s", "%s", "%s", "%s") """ % (plate, machineId, entryDatetime, expiryDatetime, ticketName, ticketPrice)

I know what problem you are having!
MySQL always auto-trims your string, so inserting 'a ' will actually be just 'a'.
https://dev.mysql.com/doc/refman/5.0/en/char.html
"For VARCHAR columns, trailing spaces in excess of the column length are truncated prior to insertion and a warning is generated, regardless of the SQL mode in use. For CHAR columns, truncation of excess trailing spaces from inserted values is performed silently regardless of the SQL mode."
You can try using a blob, which will not ignore whitespaces
If you want to continue to use CHAR or VARCHAR fields, you can use LIKE 'String ' to include whitespaces, WHERE col = 'String ' will not work

Filtering machine with space: where machine like '% %';
Insert: What is the problem? Just put the name with the space between the single quotes.
I notice you are using double quotes instead of single quote, the usual string delimiter in MySQL.

Related

Teradata Masking - Retain all chararcters at position 1,4,8,12,16 .... in a string and mask remaining characters with 'X'

I have a requirement where I need to mask all but characters in position 1,4,8,12,16.. for a variable length string with 'X'
For example:
Input string - 'John Doe'
Output String - 'JXXn xxE'
SPACE between the two strings must be retained.
Kindly help or reach out for more details if required.
I think maybe an external function would be best here, but if that's too much to bite off, you can get crafty with strtok_split_to_table, xml_agg and regexp_replace to rip the string apart, replace out characters using your criteria, and stitch it back together:
WITH cte AS (SELECT REGEXP_REPLACE('this is a test of this functionality', '(.)', '\1,') AS fullname FROM Sys_Calendar.calendar WHERE calendar_date = CURRENT_DATE)
SELECT
REGEXP_REPLACE(REGEXP_REPLACE((XMLAGG(tokenout ORDER BY tokennum) (VARCHAR(200))), '(.) (.)', '\1\2') , '(.) (.)', '\1\2')
FROM
(
SELECT
tokennum,
outkey,
CASE WHEN tokennum = 1 OR tokennum mod 4 = 0 OR token = ' ' THEN token ELSE 'X' END AS tokenout
FROM TABLE (strtok_split_to_table(cte.fullname, cte.fullname, ',')
RETURNS (outkey VARCHAR(200), tokennum integer, token VARCHAR(200) CHARACTER SET UNICODE)) AS d
) stringshred
GROUP BY outkey
This won't be fast on a large data set, but it might suffice depending on how much data you have to process.
Breaking this down:
WITH cte AS (SELECT REGEXP_REPLACE('this is a test of this functionality', '(.)', '\1,') AS fullname FROM Sys_Calendar.calendar WHERE calendar_date = CURRENT_DATE)
This CTE is just adding a comma between every character of our incoming string using that regexp_replace function. Your name will come out like J,o,h,n, ,D,o,e. You can ignore the sys_calendar part, I just put that in so it would spit out exactly 1 record for testing.
SELECT
tokennum,
outkey,
CASE WHEN tokennum = 1 OR tokennum mod 4 = 0 OR token = ' ' THEN token ELSE 'X' END AS tokenout
FROM TABLE (strtok_split_to_table(cte.fullname, cte.fullname, ',')
RETURNS (outkey VARCHAR(200), tokennum integer, token VARCHAR(200) CHARACTER SET UNICODE)) AS d
This subquery is the important bit. Here we create a record for every character in your incoming name. strtok_split_to_table is doing the work here splitting that incoming name by comma (which we added in the CTE)
The Case statement just runs your criteria swapping out 'X' in the correct positions (record 1, or a multiple of 4, and not a space).
SELECT
REGEXP_REPLACE(REGEXP_REPLACE((XMLAGG(tokenout ORDER BY tokennum) (VARCHAR(200))), '(.) (.)', '\1\2') , '(.) (.)', '\1\2')
Finally we use XMLAGG to combine the many records back into one string in a single record. Because XMLAGG adds a space in between each character we have to hit it a couple of times with regexp_replace to flip those spaces back to nothing.
So... it's ugly, but it does the job.
The code above spits out:
tXXs XX X XeXX oX XhXX fXXXtXXXaXXXy
I couldn't think of a solution, but then #JNevill inspired me with his idea to add a comma to each character :-)
SELECT
RegExp_Replace(
RegExp_Replace(
RegExp_Replace(inputString, '(.)(.)?(.)?(.)?', '(\1(\2[\3(\4', 2)
,'(\([^ ])', 'X')
,'(\(|\[)')
,'this is a test of this functionality' AS inputString
tXXs XX X XeXX oX XhXX fXXXtXXXaXXXy
The 1st RegExp_Replace starts at the 2nd character (keep the 1st character as-is) and processes groups of (up to) 4 characters adding either a ( (characters #1,#2,#4, to be replaced by X unless it's a space) or [ (character #3, no replacement), which results in :
t(h(i[s( (i(s[ (a( (t[e(s(t( [o(f( (t[h(i(s( [f(u(n(c[t(i(o(n[a(l(i(t[y(
Of course this assumes that both characters don't exists in your input data, otherwise you have to choose different ones.
The 2nd RegExp_Replace replaces the ( and the following character with X unless it's a space, which results in:
tXX[s( XX[ X( X[eXX( [oX( X[hXX( [fXXX[tXXX[aXXX[y(
Now there are some (& [ left which are removed by the 3rd RegExp_Replace.
As I still consider me as a beginner in Regular Expressions, there will be better solutions :-)
Edit:
In older Teradata versions not all parameters were optional, then you might have to add values for those:
RegExp_Replace(
RegExp_Replace(
RegExp_Replace(inputString, '(.)(.)?(.)?(.)?', '(\1(\2[\3(\4', 2, 0 'c')
,'(\([^ ])', 'X', 1, 0 'c')
,'(\(|\[)', '', 1, 0 'c')

Inserting values starting with zero in access database

I have a field called Product_Id(type string), which has length of 7 and starting with 0. But while inserting through VBA into a table field of type text the zeros is not getting inserted.
This is the insert query:
dbs.Execute "INSERT INTO tablename (PROD_NBR)VALUES (" & prodID & ");"
I think I have fixed the error - you need to declare the value in single quotes.
The PROD_NBR is a string type and field in the table is text type, then the inserting variable should be declared inside single quotes then double quotes and between two & symbols:
dbs.Execute "INSERT INTO tablename (PROD_NBR)VALUES ('" & prodID & "');"
Responding to #Cherry's answer, the following method is less tedious than a parameterized query, aka prepared statement. prodID can safely contain quotes and other special characters.
With dbs.OpenRecordset("tablename")
.AddNew
.Fields("PROD_NBR") = prodID
.Update
End With
Regarding the "starting with zero" part of your question, do you want PROD_NBR to always be 7 characters, padded with leading 0's? Then replace prodID with:
Right("0000000" & prodID, 7)

snprintf truncates the last sign

Consider the following code:
char *myContent = "content";
int size = snprintf(NULL, 0, "INSERT INTO myTable (col1) VALUES('%s')",myContent);
char *query = malloc(size+2);
snprintf(query, size, "INSERT INTO myTable (col1) VALUES('%s')",myContent);
Now I have the problem that the last bracket is truncated:
(gdb) print query
$2 = 0x616080 "INSERT INTO myTable (col1) VALUES('content'"
This is not a valid SQL statement, so have you an idea what the reason could be that the last bracket is missing?
snprintfreturns:
the number of characters printed (not including the trailing '\0' used to end output to strings)
But the size argument is:
and vsnprintf() write at most size bytes (including the trailing null byte ('\0'))
So you should:
char *query = malloc(size+1);
snprintf(query, size+1, "INSERT INTO myTable (col1) VALUES('%s')",myContent);
snprintf returns the size of the resulting string, not including the NULL terminator. I think you need to pass size+1 to the second snprintf.
What the others have said. But as myContent hasn't changed, it's safe to simply say:
sprintf(query, "INSERT INTO myTable (col1) VALUES('%s')",myContent);

I am searching for a string like Cityname, State in mysql table where state name is 2 letter char

I am searching for a string like Cityname, State (e.g.Moline,IL) in mysql table.
How can i make the query using regexp in mysql.
mySQL has a REGEXP operator that should do what you need:
11.4.2. Regular Expressions
You can try something like:
select foo from bar where foo REGEXP '[a-zA-Z ]+,\s*(AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY)'
This expects valid city names to be made of upper/lower case letters A-Z, and optionally include one or more spaces. You can add other valid characters to the city name in the first set of [ ] brackets.
To see what regex features are supported by MySQL, see the POSIX ERE column on http://www.regular-expressions.info/refflavors.html
Regex Explanation
NODE EXPLANATION
--------------------------------------------------------------------------------
[a-zA-Z ]+ any character of: 'a' to 'z', 'A' to 'Z',
' ' (1 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
, ','
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
AL|AK|AS|... 'AL' or 'AK' or 'AS' or ...
(valid state abbreviations)
--------------------------------------------------------------------------------
) end of \1

MySQL query to replace spaces in a column with underscores

I have a MySQL database table 'photos' with a column 'filename'.
I need to replace the spaces in the filename column values with underscores.
Is it possible with a single/multiple query? If so how?
You can use the REPLACE function :
REPLACE(str,from_str,to_str)
Returns the string str with all
occurrences of the string from_str
replaced by the string to_str.
REPLACE() performs a case-sensitive
match when searching for from_str.
So, to replace all occurences of a character by another one in all lines of a table, something like this should do :
update photos set filename = replace(filename, ' ', '_');
ie, you search for ' ' in the column filename and use '_' instead ; and put the result back into filename.
update photos set filename = replace(filename,' ', '_');