I am trying to perform the following query:
SELECT * FROM table_name_here WHERE Date LIKE %Jan % 2014%
Now, the table name is different and hidden here, but it just won't go through. It says there is an error in my syntax around %Jan % 2014%
I can get this to work, so I know the connection works: SELECT * FROM table_name_here
So the problem lies with the WHERE and LIKE part.
I also tried to perform this on my hosting sites DB management tool:
SELECT *
FROM `table_name`
WHERE `Date` LIKE '%Jan % 2014%'
and that one works
You have two syntax errors, firstly the word Date is a keyword, so needs to be wrapped and you need quotes around your string, like so:
SELECT * FROM table_name_here WHERE `Date` LIKE "%Jan % 2014%"
Assuming that date is begin stored as a date/datetime column, don't use like on it. The like implicitly converts it to a string, using some local format.
Instead, be explicit:
where month(`date`) = 1 and year(`date`) = 2014
or
where date_format(`date`, '%Y-%m') = '2014-01'
As for your original question, you discovered that quotes are important around string constants. I would recommend using single quotes (as opposed to double quotes), because single quotes are the ANSI standard string delimiter.
You need quotes around your LIKE match:
SELECT * FROM table_name_here WHERE Date LIKE "%Jan % 2014%"
If you are using the DATE datatype, you can use this
SELECT * FROM table_name_here WHERE 'Date' LIKE '2014-01-%'
this is how mysql store the DATE value: 'YYYY-MM-DD' eg. '2014-01-07.
MySQL Manual
Related
I am trying to pull a product code from a long set of string formatted like a URL address. The pattern is always 3 letters followed by 3 or 4 numbers (ex. ???### or ???####). I have tried using REGEXP and LIKE syntax, but my results are off for both/I am not sure which operators to use.
The first select statement is close to trimming the URL to show just the code, but oftentimes will show a random string of numbers it may find in the URL string.
The second select statement is more rudimentary, but I am unsure which operators to use.
Which would be the quickest solution?
SELECT columnName, SUBSTR(columnName, LOCATE(columnName REGEXP "[^=\-][a-zA-Z]{3}[\d]{3,4}", columnName), LENGTH(columnName) - LOCATE(columnName REGEXP "[^=\-][a-zA-Z]{3}[\d]{3,4}", REVERSE(columnName))) AS extractedData FROM tableName
SELECT columnName FROM tableName WHERE columnName LIKE '%___###%' OR columnName LIKE '%___####%'
-- Will take a substring of this result as well
Example Data:
randomwebsite.com/3982356923abcd1ab?random_code=12480712_ABC_DEF_ANOTHER_CODE-xyz123&hello_world=us&etc_etc
In this case, the desired string is "xyz123" and the location of said pattern is variable based on each entry.
EDIT
SELECT column, LOCATE(column REGEXP "([a-zA-Z]{3}[0-9]{3,4}$)", column), SUBSTR(column, LOCATE(column REGEXP "([a-zA-Z]{3}[0-9]{3,4}$)", column), LENGTH(column) - LOCATE(column REGEXP "^.*[a-zA-Z]{3}[0-9]{3,4}", REVERSE(column))) AS extractData From mainTable
This expression is still not grabbing the right data, but I feel like it may get me closer.
I suggest using
REGEXP_SUBSTR(column, '(?<=[&?]random_code=[^&#]{0,256}-)[a-zA-Z]{3}[0-9]{3,4}(?![^&#])')
Details:
(?<=[&?]random_code=[^&#]{0,256}-) - immediately on the left, there must be & or &, random_code=, and then zero to 256 chars other than & and # followed with a - char
[a-zA-Z]{3} - three ASCII letters
[0-9]{3,4} - three to four ASCII digits
(?![^&#]) - that are followed either with &, # or end of string.
See the online demo:
WITH cte AS ( SELECT 'randomwebsite.com/3982356923abcd1ab?random_code=12480712_ABC_DEF_ANOTHER_CODE-xyz123&hello_world=us&etc_etc' val
UNION ALL
SELECT 'randomwebsite.com/3982356923abcd1ab?random_code=12480712_ABC_DEF_ANOTHER_CODE-xyz4567&hello_world=us&etc_etc'
UNION ALL
SELECT 'randomwebsite.com/3982356923abcd1ab?random_code=12480712_ABC_DEF_ANOTHER_CODE-xyz89&hello_world=us&etc_etc'
UNION ALL
SELECT 'randomwebsite.com/3982356923abcd1ab?random_code=12480712_ABC_DEF_ANOTHER_CODE-xyz00000&hello_world=us&etc_etc'
UNION ALL
SELECT 'randomwebsite.com/3982356923abcd1ab?random_code=12480712_ABC_DEF_ANOTHER_CODE-aaaaa11111&hello_world=us&etc_etc')
SELECT REGEXP_SUBSTR(val,'(?<=[&?]random_code=[^&#]{0,256}-)[a-zA-Z]{3}[0-9]{3,4}(?![^&#])') output
FROM cte
Output:
I'd make use of capture groups:
(?<=[=\-\\])([a-zA-Z]{3}[\d]{3,4})(?=[&])
I assume with [^=\-] you wanted to capture string with "-","\" or "=" in front but not include those chars in the result. To do that use "positive lookbehind" (?<=.
I also added a lookahead (?= for "&".
If you'd like to fidget more with regex I recommend RegExr
I am trying to sort a row "naturally" in SQL, and I found this answer which supposedly does exactly what I need:
https://stackoverflow.com/a/17354420/7563153
But when I tried recreating it in "SQL Fiddle" I couldn't get it to work...
Here's my attempt
What am I doing wrong?
The difference between the example you cite and your own trial, is that your data combines numeric and non-numeric characters in the same field, so casting to an integer doesn't work (it just gives 0). You can visualize why the query isn't working for you by adding the order by expressions in your select list like this:
SELECT test
,test REGEXP '^[A-Za-z]+$'
,CAST(test as SIGNED INTEGER)
,CAST(REPLACE(test,'-','')AS SIGNED INTEGER)
FROM table1
ORDER BY test REGEXP '^[A-Za-z]+$'
,CAST(test as SIGNED INTEGER)
,CAST(REPLACE(test,'-','')AS SIGNED INTEGER)
,test
You'll see that they all evaluate to 0, which does not give you the sort order you're looking for.
You need to separate the numeric portion of your value from the non-numeric portion, and sort them separately. Something like this:
SELECT test
FROM table1
ORDER BY substring_index(test,'-',1)
,CAST(substring(test, locate('-', test)+1)AS SIGNED INTEGER)
,test
You might need to adjust that if your actual data isn't as simple and straightforward as your example data.
For your given database schema, is your attempt even necessary?
Try something like this,
SELECT `test`
FROM table1
ORDER BY LENGTH(`test`),
`test`;
Assuming (!) that your data have the format "blabla-integer" with one and only one dash (-) separating the alphanumeric part from the integer part I would go this way:
select * from table1
order by cast(substring(test,
locate('-', test) + 1,
length(test) - locate('-', test)
) as signed integer
);
I am trying to select a record which clearly exists, but my SQL query does not bring it up. Any idea how to get this working?
SELECT * FROM Users WHERE 'local.email'='burgundy#email.com' LIMIT 1
The issue is that you're using single quotes ( ' ) around your column name, rather than using backticks ( ` ).
Try using this instead:
SELECT *
FROM Users
WHERE `local.email` = 'burgundy#email.com'
LIMIT 1
Like Crocodile said, anything that is a SQL variable like a table name or column name can also be surrounded by `` Back ticks (hold shift and hit ~). This tells SQL to look at them as literals.
Please help me resolve my query when using query - I just want to subtract a few characters and then use the % to find the matching LIKE:
select * from `providers` WHERE `name` LIKE SUBSTR('telin',1,4)%
Please let me know what i'm doing wrong, any kind of help is greatly appreciated!
Assuming telin is a column name rather than the literal string, it should be quoted in backticks. If it is the literal string, then there is obviously no need to extract a substring from it. I suspect however, that it was the result of a PHP variable you pasted here after echoing out the full query, then it is correctly single-quoted.
Anyway, you will need to concatenate the SUBSTR() result onto the '%' via CONCAT():
SELECT * FROM `providers` WHERE `name` LIKE CONCAT(SUBSTR(`telin`,1,4), '%');
But better would be to use LEFT() to compare the first 4 characters of each:
SELECT * FROM `providers` WHERE LEFT(`name`, 4) = LEFT(`telin`,4);
I have a column with datatype varchar using MySQL database. Suppose the value from a web form that gets saved in this column is : 2/4/2013
My search query goes like:
SELECT * FROM tbl WHERE colValue LIKE %2/4/2013%
But, it is crashing. For any other string am getting correct results. But, is it the forward slash which makes it crash. How can this be fixed ?
Regards !
since you want to select for specific date, why not use =
SELECT * FROM tbl WHERE colValue = '2/4/2013'
but if the data type of the column is DATE or DATETIME, use proper formatting although mysql automatically converts it,
SELECT * FROM tbl WHERE colValue = '2013-02-04'
For Using like operator you could use DATEPART() function...
select * from tbl
where (DATEPART(yy, colValue) = 2013
AND DATEPART(mm, colValue) = 04
AND DATEPART(dd, colValue) = 02)
Like this you can do like in SQL
Use an escape character for the /. The mysql escape character is the \.