Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 28 days ago.
Improve this question
When I use:
SELECT *
FROM Customers1
WHERE email LIKE '%[^#]%'
There are no customers on the output at all, when there should be two on the output. Any ideas what might be causing this? I need to find customers email addresses which don't contain the '#'symbol.
I've tried running code:
SELECT *
FROM Customers1
WHERE email LIKE '%[#]%';
This shows 13 on the output.
I've also tried the '%[!#]%'; but this makes no difference.
In my understanding, you are trying to get all the email in a table that is not a valid mail address (by checking that email must have # symbol in it).
The way you are trying to achieve this will not work as this will fetch all data in the column except the null and blank (empty string) data.
[^] wildcard will check LIKE and PATINDEX in some special cases like.
A column has only single characters.
You are fetching a string where a specific position has not that value, etc.
For this scenario I will rather suggest, use a simple TSQL like.
SELECT *
FROM Customers1
WHERE email NOT LIKE '%#%';
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 months ago.
Improve this question
I have to take over old colleague's code and trying to understand this one SQL statement like below:
SELECT * FROM my_table WHERE date_key = '{0}'
Column date_keycontains int values such as 20220712, 20220120, etc.
The first guess is that SELECT statement filters for rows with 0 value in column date_key. However, when running that line of code, I receive this error :
SQL Error [100038] [22018]: Numeric value '{0}' is not recognized
What exactly does that line of code do?
That looks like a placeholder, replaced with an actual value in code when calling the query.
See similar What is {0},{1},{2},{3} in the SQL query
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm using Oracle, this information comes from the "HR.EMPLOYEES" table, but I don't know how to structure that query.
There are several ways to achieve your goal. In no particular order:
Using SUBSTR
Just use SUBSTR. This function allow you to extract a part from a string:
SELECT first_name FROM table WHERE SUBSTR(last_name, 2, 1) = 'o'
Remember, you are only filtering the rows based on lower case as 'o' which is not same as upper case 'O'. And, you need to create a function-based index on the column too, to avoid any performance issues due to FTS.
If you have only one column for the name, then use SUBSTR and INSTR in the SELECT to get the first_name. I would like to leave this up to you. Let me know if you really struggle with INSTR. A hint for you to try and learn yourself, INSTR( string, substring [, start_position [, nth_appearance ] ] )
Using regular expression
The same could be achieved using REGEXP_LIKE, however, it would be much resource consuming:
SELECT first_name FROM table WHERE REGEXP_LIKE(last_name, '^.o')
Here, I am looking for a string such as:
^ after the start of the string
. I have any character
o then an o
Using LIKE pattern matching
This solution is rather database-vendor agnostic: it will work with (almost?) any RDBMS:
SELECT first_name FROM table WHERE last_name LIKE '_o%'
Pattern matching is more or less like the wildcard you might use in your shell. Except than is SQL _ is used for any character and % is used for any string (incl. empty string).
Other
By searching through the web and in various Oracle's documentation you might probably be able to find several other -- more or less exotic -- options.
For example, one might think of using a virtual column on the second letter of last_name.
Or, if and only if, you need case insensitive approach, you can have a look at this demo http://lalitkumarb.wordpress.com/2014/01/22/oracle-case-insensitive-sorts-compares/. Please make sure you have utmost understanding about the trivial aspects as mentioned above, before jumping onto complex things.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Just for reference I am using SQL Azure.
I noticed when I am trying to select data from a table based on a license plate and the state of that plate I get no results back if the state is "IN". I realize the word "IN" is reserved in SQL server; however, I am containing that within quotes in my query. I currently am in testing phase and have only one record in the table which has a lisence plate 287YGB and state IN.
If I write my query as follows I get nothing back.
SELECT MakeModel, CitizenID, VehicleID FROM tblVehicles WHERE tblVehicles.Lisence = '287YGB' AND tblVehicles.PlateState = 'IN'
If I write my query this way I get back my result. But this is not good enough.
SELECT MakeModel, CitizenID, VehicleID FROM tblVehicles WHERE tblVehicles.Lisence = '287YGB'
And finally, if I write my query this way I get the only row in the table.
SELECT MakeModel, CitizenID, VehicleID FROM tblVehicles
From these tests I can see that the last where parameter is causing the problem. I am assuming it is due to the fact that the word "IN" is reserved. Is there a way around this?
Reserved words usually only cause problems if you're using them as field names, and in that case you need to wrap them with brackets ("[]") to eliminate the problem. I will amost guarantee you that your PlateState has some garbage in it, so you need to either trim it first (LTRIM(RTRIM(PlateState)) = 'IN') or use Like '%IN%' instead, and this will return the results you expect.
try this
SELECT MakeModel, CitizenID, VehicleID FROM tblVehicles WHERE tblVehicles.Lisence = '287YGB' AND LTRIM(RTRIM(tblVehicles.PlateState)) = 'IN'
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Using SQL only, is there a way to query all values that are emails from a column of misc data?
I tried like '%#%', but there could be texts like this: Fort Knox # Room 123.
Edit: Regex is unavailable.
I've also tried like '%#%.%' but did not cover cases with spaces.
Here is SQLFiddle for you.
SELECT *
FROM TABLENAME
WHERE email LIKE '%#%.%'
AND email NOT LIKE '% %';
If you don't mind using CLR functions, I'd suggest writing a .NET method which uses a proper regular expression (you can find various ones online) to validate the email address, then calling that function as part of your query.
select * from whatever
where dbo.IsThisAnEmailAddress(myColumn) = 1
I would use regular expressions in your query to get at the emails. There are tons of links on this site to valid email regexes.
I agree with others, RegEx is better, however, if not available, try the following
WHERE fieldName LIKE '%#%'
AND fieldName LIKE '%.%'
AND charindex(' ',fieldName)=0
It's not great, and slow, but should get you pretty close. I.E Contains both an # and an . and no spaces...
SQLFiddle: http://www.sqlfiddle.com/#!3/5f6d8/1
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want write a query which would give two columns :
1> Type of Query 2> Count
This result set should have following structure
Here the 1st column values should be predefined and the count has to
be calculated . I want to check the Request column of source table and
find specific pattern . If that pattern is found increase the count .
For Example :
If there is a word "greenhopper" found in the request column then that
belongs to type GREENHOPPER .
OR
If there is a word "gadgets" found it is of the type DASHBOARD . and
so on ...
So I want to analyze the usage of various categories by using the log
table .
Hence Finally I can get the amount of usage and after that I can build
a pie chart out of it .
SELECT 'Greenhopper' AS TypeOfQuery, COUNT(*) AS Cnt
FROM YourTable
WHERE Request LIKE '%Greenhopper%'
UNION ALL
SELECT 'Dashboard', COUNT(*)
FROM YourTable
WHERE Request LIKE '%gadgets%'
-- And so forth
You said they were predefined right? So you'd have ~10 different statements UNION'd together.
WITH Requests AS
(
SELECT
CASE
WHEN Request LIKE '%Greenhopper%' THEN 'GreenHopper'
WHEN Request LIKE '%gadgets%' THEN 'Gadgets'
-- and so on
ELSE 'Misc'
END RequestType
FROM YourTable
)
SELECT
RequestType,
COUNT(*) RequesCount
FROM Requests
GROUP BY RequestType
;
No data to test but I believe this approach will perform better as the table will be scanned less times. Performance is never going to be ideal though because of the LIKE and first wild card. This will prevent seeks.
Further explanation of why LIKE does not perform here
Having just re looked at the question you may be able to improve performance further by changing the search string so it only has a wildcard on the right
e.g. LIKE 'GET /rest/gadget%' and so on.