I am trying to display one column name with '%' i.e the "ip_utilization" column as "usage%" column here. But i am not able to, can anyone suggest a solution please. I am sharing the query.
select ip_name, concat (first_name, ' ' ,last_name) as Contact, ip_utilization as usage_% from database_name;
Just give it a different column name? Like usage_percentage.
It may be possible to have the percent sign using backticks, but to what end? What point is there in jumping through hoops to somehow have a weird special character in a name that usually won't be displayed in an application anyway?*
(Unless it's really a public-facing app and you can't change the name on application level at all. But that should be the rare exception.)
Enclose usage_% in `` as below:
select ip_name, concat (first_name, ' ' ,last_name) as Contact, ip_utilization as `usage_%` from database_name;
If you have further problems, leave a comment.
You should be able to use backticks
select ip_name, concat (first_name, ' ' ,last_name) as Contact, ip_utilization as `usage_%` from database_name;
Quote usage%. But I advice to change the name, because % is keyword used for like command.
select ip_name, concat (first_name, ' ' ,last_name) as Contact,
ip_utilization as 'usage_%' from database_name;
Related
I've got a table with real names, e.g. "Matt Smith", but I need to convert them to be used as URL paths in a web app. "Matt Smith" would become "matt-smith", "Danny O'Brien" would become "danny-obrien", etc.
I need to lowercase, strip special characters, and replace spaces with dashes.
I know I can do this to replace spaces:
SELECT REPLACE( table.field, ' ', '-' ) FROM table;
And I know there's a LOWER() function as well.
What I don't know is:
How to strip special characters
How to combine all three into one SQL query that actually works
If it's possible to make it work automatically when I create the view, and for it to keep working as new rows are added via the web app
If you are using MySQL 8+, then REGEXP_REPLACE is one option:
SELECT
LOWER(REPLACE(REGEXP_REPLACE(field, '[^A-Za-z0-9]', ''), ' ', '-')) AS alias
FROM yourTable;
The innermost call to REGEXP_REPLACE strips off all non alphanumeric characters.
I want to write an SQL query that will fetch all the students who live in a specific Post Code. Following is my query.
SELECT * FROM `students` AS ss WHERE ss.`postcode` LIKE 'SE4 1NA';
Now the issue is that in database some records are saved without the white space is postcode, like SE41NA and some may also be in lowercase, like se41na or se4 1na.
The query gives me different results based on how the record is saved. Is there any way in which I can handle this?
Using regexp is one way to do it. This performs a case insensitive match by default.
SELECT * FROM students AS ss
WHERE ss.postcode REGEXP '^SE4[[:space:]]?1NA$';
[[:space:]]? matches an optional space character.
REGEXP documentation MySQL
Whether case matters depends on the collation of the string/column/database/server. But, you can get around it by doing:
WHERE UPPER(ss.postcode) LIKE 'SE4%1NA'
The % will match any number of characters, including none. It is a bit too general for what you might really need -- but it should work fine in practice.
The more important issue is that your database does not validate the data being put into it. You should fix the application so the postal codes are correct and follow a standard format.
Use a combination of UPPER and REPLACE.
SELECT *
FROM students s
WHERE UPPER(REPLACE(s.postcode, ' ', '')) LIKE '%SE41NA%'
SELECT * FROM students AS ss
WHERE UPPER(REPLACE(ss.postcode, ' ', '')) = 'SE41NA' ;
SELECT *
FROM students AS ss
WHERE UPPER(ss.postcode) LIKE SELECT REPLACE(UPPER('SE4 1NA'), ' ', '%'); ;
I propose using the spaces replaced with the'%' placeholder. Also transform the case to upper for both sides of the LIKE operator
select replace(lastname,'%%',firstname) as new1 from names;
When I run this, lastname is returned. Why? I expect it to search names.lastname for everything (%% wildcard) and return names.firstname.
All the syntax I have reviewed suggest I am doing this right, it seems so simple...
Why?
The expression REPLACE(lastname,'%%',firstname) will return lastname, whenever lastname doesn't contain two contiguous percent sign characters. Why? Because that's the documented behavior of the REPLACE function.
The '%' is a wildcard when used with LIKE. It's not a wildcard in the REPLACE() function.
The expression in your question will search the value of lastname for occurrences of two contiguous percent sign characters, and replace those occurrences with the value in firstname.
For example:
SELECT REPLACE('fee%%fi%%fo','%%','-dah ') AS foo
foo
-------------------
fee-dah fi-dah fo
(I believe this answers the question you asked.)
What are you trying to achieve?
I'm pretty sure wildcards are not allowed in REPLACE, but the way you have written it suggests that you want to SELECT the lastname with all its characters replaced with the string firstname, which is the same as SELECT firstname.
If you need to change the lastname in the table, you will need to run an UPDATE:
UPDATE names SET lastname=firstname
If you simply want a concatenation of the two then SELECT CONCAT(lastname,' ', firstname) is your game.
When I concat($name, $surname), is there a way of putting a space in between the $name $surname using my sql not php so when i get the result it formats a little cleaner?
You can concatenate string literals along with your fields, so you can add a space character in a string between the fields you're concatenating.
Use this:
CONCAT(name, " ", surname)
This functionality is documented quite clearly on the MySQL manual page for the CONCAT() function.
There is also the CONCAT_WS function which allows you to specify a separator to be used between each of the other fields passed to the function. If you're concatenating more than two fields in the same way, this function might be considered cleaner than repeating the separator between each field.
For example, if you wanted to add a middle name field, you could use this function to specify the separator only once:
CONCAT_WS(" ", first_name, middle_name, surname)
Just add a space in there.
SELECT CONCAT(name,' ',surname) AS full_name FROM table;
EDIT oops, bad spelling there... ;p
Use this, no version dependencies
concat(name,Char(32),venue)
Use CONCAT(name, ' ', surname).
after trying this i tried to do this
concat(name, _utf8 ' ' ,name1)
Hey i had the same problem and this is what i used and it really worked out great
CONCAT(column1," ",column2)
the issue is you add physical space in between the double courts("") using the space bar on your keyboard and then thats it
The above all options are not working ..
My query is
SELECT CONCAT(FIRSTNAME, ' ' , LASTNAME) AS FULLNAME FROM USERS;
getting the below error:
ORA-00909: invalid number of arguments 00909 00000- "invalid number of arguments" *Cause: *Action: Error at line 26 column :9
Use this methoid to add separator
CONCAT_WS(' ', first_string, second_string, n_string, ..., last_string)
I'm not exactly sure what happened because I migrated from One server to another of the same spec and SQL...
Still in comments and titles the new database shows the characters ' instead of '
and I was wondering if I could ask for help in either replacing ' with '
or if it was simpler just deleting '
Thanks so much...
Steff
You could use MySQL's REPLACE method (look here):
UPDATE
Changed the statement to reflect the OP's naming:
UPDATE database1.vb_ppgal_albums
SET pp_photos = REPLACE(pp_photos, ''', '\'')
Good luck.
The following is the coding that I use to update double-quote in MySQL. I use the REPLACE function. The first parameter is the field_name that I want to have searched, the second is the escaping of the double quote (\") as the search string, followed by escaping of the escape character (\) followed by the double quote, which will insert into the field name (\"). In the table I will now have a measurement of '1/2\"' instead of '1/2"', which was my goal. I hope this helps. (PS, the Where clause is for show, you may not need it.)
UPDATE `table_name`
SET
`field_name` = REPLACE(`field_name`, '\"', '\\"')
WHERE `Id` > 125