Replace spaces with dash and copy into new column - mysql

I have table named "city" with column named "city_name" with about 200 records.
I have created another colum named slugs where I want to copy all the records from "city_name" in the same row with spaces replaced with dash - and lowercase.
How can I achieve this via phpmyadmin.
Thanks

You should be able to do this via the following query:
UPDATE city SET slugs=LOWER(REPLACE(city_name, " ", "-"))
Breaking this down, we're using REPLACE to swap all instances of " " with "-" in the existing city_name column, and then passing the result of this to the LOWER function (to convert the data to lower case) before setting the slugs field with this value.
Depending on how "clean" your data is, you might also want to TRIM the data (to remove any leading or trailing spaces in the city_name field) before you apply the REPLACE as such:
UPDATE city SET slugs=LOWER(REPLACE(TRIM(city_name), " ", "-"))
Incidentally, if you've not used (My)SQL much I'd recommend a read of the String Functions manual page - the time you spend on this now will more than repay itself in the future.

Here is SQLFiddle
MYSql documentation for function LOWER(str) and REPLACE(str,from_str,to_str)
UPDATE city SET slugs = LOWER(REPLACE(city_name," ", "-"));

Related

Pyspark How to Ignore Double quotes from the data present in the CSV files

I am having " (single quotes) in my data ,all the corresponding column values clubbed into one column even though I have used the delimiter value. In my case '|' is my delimiter.
Actual Data:
a|"b|c|d|
Expected Output:
a|"b|c|d
Actual Output Came:
a|"b**|c|d|**null|null| ( here 3rd & 4th column coming as single column, in place of actual 3rd & 4th col.. getting values as null)
I have tried below approach:
Approach 1:
df=spark.read.csv(filepath,header=True,sep='|',quote='')
Above approach gives particular column data correctly but empty columns coming values as """" but we need empty column as it is.
Approach 2:
df=spark.read.csv(filepath,header=True,sep='|',quote='',escape='\"')
Above approach gives values clubbing into single column as like actual output.
After some heads & trails
found solution
read the file with both below options :
quote='',escape='\"'

Handle empty cell in Db using SSIS

I have a database(Source) which has a column named "country name" and few cells in it are empty & when I am transferring its data to another database (destination) it is also empty
I have tried to use derived column in which I had used REPLACE() but is didn't work I thought it would have been any "", " " or "\t" but it was neither of these then I thought may be it is NULL & I used ISNULL() but this also failed.
I assume you are trying to work out what the "empty" data in the source column actually is? If thats the case then just convert the value to its ascii value and look that value up in an ascii table to find out what it is.
If the empty data is actually more than one character you may need to extract each character individually using, for example, the MID function.
You did not specify which DB is in the source and which is in the target, and it has meaning.
Anyway, add this code to the Derived Column component:
(DT_WSTR,100)country_name == "" || ISNULL(country_name) == TRUE ? "new_value" : country_name
Results:

SSIS remove Left zero from csv Flat File Source

I have a csv file that I want to process in SSIS. The file contains a column type string Unicode string [DT_WSTR], example: ColumnA -> ("00000123400").
I want to delete the zeroes that are on the left of 123400 and also delete the quotes and have a result as following: 123400.
For quotation marks I find the following solution via Derived Column: REPLACE (ColumnA, "\" "," "), which gives me the following result: 00000123400.
How to remove the zeroes which are on the left?
After deleting the quotation marks, I tried to convert my string to integer [DT_I4], but that does not remove the zeroes.
Do you have the answer to my case? Thanks in advance.
The solution of a part of the case is:
in our Derived column put the expression:
REPLACE(LTRIM(REPLACE(ColumnA,"0","")),"","0")
It remove just left zero
you can see the link: Removing left padding zero in SSIS
It work perfectly, but is it possible to trim left zero, and also delete quotation marks in the same time in expression?
Example: I have Column1 which is string with quotation marks and left zero - "0000123400"
I try this expression:
REPLACE(REPLACE(LTRIM(REPLACE(column1, "0", " ")), " ", "0"),"\""," ")
but it doesn't work, it deletes all zeros and returns 1234.
The solution that I want is to get 123400.
Should I do it one by one? Create a delivered column and delete quotation marks first, and after create an other delivered column for Left zero ?
Thanks in advance.
It looks like you want the output to be in numeric form? If so, the following expression will remove the quotes and leading zeros while preserving the trailing zeros from Unicode text. This can be done in a single operation, with one Derived Column that will create a new column (add a new column option) with an integer output data type in the data flow.
(DT_I4)REPLACE(ColumnA,"\"","")
If you want to keep this as the Unicode data type the expression below will do this, also in a single Derived Column. Just adjust the length according to your columns.
(DT_WSTR, 50)(DT_I4)REPLACE(CoulmnA,"\"","")

How to search a json encoded value in a mysql column?

I have stored some extra form data as json in a mysql column (extraData).
Example data
{"1":{"name":"sadsdaf ","surname":"sdafa","occupation":"sdaas","email":"dsasdaf#asdf.nl","barcode":"052355491"},"2":{"name":"sadafd","surname":"sdafa","occupation":"sddaf","email":"sda#daf.nl","barcode":"052355492"}}
I would like to do a search query which also does a search for the barcode stored in the json encoded field? So not only this
$sql = "SELECT name FROM visitors WHERE barcode = '".$barcode."'";
But also
$sql = "SELECT name FROM visitors WHERE barcode = '".$barcode."' OR extraData = '".$barcode."'";
What do I need to do to extraData so the variable barcode is checked and I get the corresponding name, surname etc?
SELECT TRIM(BOTH '"' FROM SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(V.extraData,'"name":', -1),',',1),'}',1)
FROM visitors V
WHERE V.extraData LIKE '%"barcode"%'
AND :barcode = TRIM(BOTH '"' FROM SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(V.extraData,'"barcode":', -1),',',1),'}',1);
the LIKE '%"barcode"%' filters the rows with the field extraData containing barcode values
the :barcode means to be the value to match, the TRIM is to get rid of the extra double quotes , the nested SUBSTRING_INDEX searchs for:
the text starting at the end of the first match of '"barcode":' up to
the next match of ',' (if there are more key/values) up to the next
match of '}' (if it's the last key/value at that level)
keep in mind that if you find that a certain key/val is always present in every row and is accessed everywhere you should consider adding an extra field to the table/data model and create an index out of it (ie. name, surname).
the values retrieved must be decoded "always".
"Einstürzende Neubauten" is stored as "Einst\u00fcrzende Neubauten"
doing search and compare numbers are "ok", like in the example (the trim double quotes sometimes would not be necesary as if you json_encode an integer var it will be stored as "number":99 ). but fractional numbers would be stored depending on the locale "1,5" / "1.5"
search and compare strings are more tricky: you have to "jsonencode" the value to match
"https://www.google.com" is stored as "https:\/\/www.google.com"
check your data normalization skills.
check your optimization skills
use an online tool to see how your data will be encoded

MySQL query to delete Joomla user with same first & last name

I know this isn't correct, but this is basically what I want to do:
delete FROM jos_users WHERE name like '<firstname>=<lastname>'
In Joomla the name field is one input and there are a lot of spam accounts that have the same first & last name - for example Uzhuzaio Uzhuzaio - that I would like to delete.
So in summary, I need a query that will delete the users from jos_users where the name field contains 2 of the same word
I think that
DELETE FROM jos_users WHERE SUBSTRING_INDEX(name, '=', 1 ) = SUBSTRING_INDEX(name, '=', -1 )
should do the trick, have you tried it?
You can use SUBSTRING_INDEX, which returns a substring before or after a certain number of occurrences of a delimiter.
In the example below, space is the delimiter, and a count of 1 will return everything to the left of the first space and a count of -1 everything on the right of the first space.
DELETE FROM jos_users WHERE
SUBSTRING_INDEX(name, " ", 1)=SUBSTRING_INDEX(name, " ", -1)
I would however perform a SELECT first, to see that there is no legitimate user in there (rare, but it could happen)
Important note: this only works if the name is in the form <name> <surname>. Multiple spaces will pose a problem. In the case of multiple spaces (e.g. <composite name> <composite name>) you should use different count values (2;-2 3;-3 etc).
Joomla by default does not have separate fields for first and last names. Most likely you have them separated by some symbol. I would recommend to define it and write a simple PHP code. At least this is what I would do.