I'm attempting to query on a field/column/table in a MySQL DB where the field type is varchar, but some values contains spaces. In my query, I tried to put the exact string to match on in single quotes in a where clause. However, the only rows that are returned are the strings that do not contain spaces.
Here are the values stored in the table/column:
Here is the query and the result that is only returning fields without spaces:
I expected to find a row for "New Business", a row for "Monetary Endorsement", etc. Any idea on how I can modify my query to return the desired fields? Thanks for your help in advance!
Maybe the other values have leading or trailing spaces. You can either use one of the suggestion below:
1.) Use TRIM()
WHERE TRIM(PTD_TRANS_TYPE) = 'NEW BUSINESS'
2.) Use LIKE
WHERE PTD_TRANS_TYPE LIKE '%NEW BUSINESS%'
Here's a Demo.
Related
MariaDB version 10.3.22 and 10.3.23
I have a table with column name entries like these. The second entry contains trailing spaces. Datatype of the column is Varchar(50)
"John"
"John "
I want the below query to return only the first entry, but it returns both entries. Seems like internally, it's doing the trim while comparing. Any way to perform the exact match
Select name from <table> where name="John";
Similarly, the below query should not return any rows as I'm trying to match with extra space, but it outputs two rows. Some behind the scene trimming is happening. Need to either disable it or anyway to do the exact match
Select name from <table> where name="John ";
Use the BINARY option to compare exactly rather than as strings.
Select name from <table> where BINARY name="John";
I've got a table where identifiers(XXXXX-065-00) should match a division(65). I'd like to query all the rows that don't match.
The following SQL seems to work, but is there a better or more efficient way of doing this?
select id,division,identifier from table where identifier not REGEXP '.....-'+division+'-..'
You can concat() the regex string:
where identifier not regexp concat('^.{5}-', lpad(division, 3, '0'), '-..$')
Note that in MySQL + is a numeric addition (it does not do string concatenation, like in SQL Server for example).
Other remarks:
you probably need to pad the division with '0's so it has exactly 3 characters
I added ^/$ to make the regex match on the entire string rather than do partial matching, since this seems to be what you want
you can use quantifiers: .{5} stands for .....
My database has content that has been previously escaped, resulting in string such as
This value is \"invalid\".
I want to get rid of escape character \ but I'm having a hard time to find these rows. My first attempt
select value from content where value like '%\\"%';
fails to separate \" from ", and returns valid rows such as
This value is "valid".
So how can I query for the string \", preferably in a way than can be used in an update clause to remove the slash?
EDIT: SQL Fiddle here http://sqlfiddle.com/#!9/fc3d3/6
Notice that the query at line 3 returns both rows.
I've checked your sqlfiddle.
This gets the invalid rows:
SELECT * from myTable where content<>REPLACE(content,'\\\"','\"')
If this works, then you can simply update your content column to REPLACE(content,'\\\"','\"').
I have a table where a field can have single quotes, but I need to be able to search by that field without single quotes. For example, if the search query is "Johns favorite", I need to be able to find a row where that field contains "John's favorite". I was looking into regex for it, but that seems to return a 0 or 1 when used in a select statement, if I'm understanding it correctly.
Take a look at:
http://www.artfulsoftware.com/infotree/queries.php#552
This will give you the distance between two strings. I.e. you can check whether levensthein distance is less than 3, which means, less than 3 operations are required to be equal.
Try using REPLACE:
SELECT
IF(
REPLACE("John's favorite","'","") = "Johns favorite" ,
"found",
"not found"
)
It's not optimal but it should do the job.
I am trying to insert the following query and I get syntax errors. Can you please help me with the below query:
INSERT INTO ABCTABLE (COLUMN1) values ('DECODE(MDSE_CD,NULL,'0000000000000000',LPAD(TO_NUMBER(MDSE_CD,'16',' '))');
Since you haven't really said anything other than "this query doesn't work, fix it", I have to take a stab in the dark what you want. From the query you have, I'm therefore guessing you want the value of the column to be DECODE(MDSE_CD,NULL,'0000000000000000',LPAD(TO_NUMBER(MDSE_CD,'16',' '))
In which case, you have to escape the single quotes within your string literal. Do this by doubling up the quotes:
INSERT INTO ABCTABLE (COLUMN1)
VALUES ('DECODE(MDSE_CD,NULL,''0000000000000000'',LPAD(TO_NUMBER(MDSE_CD,''16'','' ''))')
Try properly escaping the inner single quotes
INSERT INTO ABCTABLE (COLUMN1)
VALUES ('**DECODE**(MDSE_CD,NULL,''0000000000000000'',**LPAD**(TO_NUMBER(MDSE_CD,''16'','' ''))');
The problem is the use of quote marks. If we tried to break up your query it would look like this:
INSERT INTO ABCTABLE
(COLUMN1)
values
(
'DECODE(MDSE_CD,NULL,'
0000000000000000
',LPAD(TO_NUMBER(MDSE_CD,'
16
','
'))'
);
...which clearly makes no sense.
You might want to think about how to escape a quote mark inside a string.
Sql Server:
DECOD function in Sql Server can be replaced with CASE construct
LPAD function in Sql Server has not a direct correspondence but you can pad your string using string manage function REPLACE (replicate a character a number of specified times)
My Sql:
DECOD function in MySql can be replaced with CASE construct
LPAD function in MySql is existent
What do you want to store... a string literal 'DECODE(MDSE...))', or did you want to call a function to derive a value?
To store a string literal containing single quotes, you need to "escape" each single quote within the string with an extra single quote, e.g.
O'Hare Int'l ==> 'O''Hare Int''l'
The DECODE function is Oracle specific. That expression will need to be rewritten using different functions in both MySQL and SQL Server.