SIMPLE SQL Select Where Query - sql

Anyone got any idea why this doesn't work. Im at a loss
The following
SELECT * FROM tblCustomerDetails WHERE AccountNo='STO00900'
Returns nothing however if i run the same query with any othe accoutn number it works.
and this account will show when i run
SELECT TOP 10 * FROM tblCustomerDetails ORDER BY ID desc
Picture explains it better.
Thanks

Try as Notulysses suggested, but I would recommend it a bit differently:
SELECT * FROM tblCustomerDetails WHERE LTRIM(RTRIM(AccountNo)) = 'STO00900'
The LIKE operator will likely match more rows than you need (if te AccountNo column is not unique), so I'd go with trimming the whitespaces and then checking for a specific account.

There may be some space in the entry either in the start or at the end ,try to trim both ends of the entry.

Try
SELECT * FROM tblCustomerDetails WHERE AccountNo LIKE '%STO00900%'
As there can be hidden characters.

Related

How to reference a column in SQL that has count?

How do I get the column "count(division)" instead of getting the actual number of counts?
select * from num_taught;
gets me this
select count(division) from num_taught;
gets me this, but I actually want the third column "count(division)" from the previous image
I want to know this because I'm doing this right now:
sql> select * from num_taught as a, num_taught as b
...> where a.count(division) = b.count(division);
Error: near "(": syntax error
but as you can see, there's a syntax error and I think it's because the code is not referencing the "count(division)" columns but actually finding the count instead.
My end goal is to output the "Titles" that have the same "Division" and have the same count(division).
So for example, the end table would have the rows "Chief Accountant", "Programmer Trainee", "Scrivener", "Technician", "Wizard". Since these are the rows that have a match in division and count(division)
Thanks!
What does DESC num_taught return? I am curious how the third column is populated - is it some kind of pseudo-column? You may want try wrapping the column name with [], see: How to deal with SQL column names that look like SQL keywords?
i.e. try:
select [count(division)] from num_taught;
You need to escape your column name using quotes (in case it's Sqlite like you mentioned in the comments).
select "count(division)" from num_taught;
or:
select * from num_taught as a, num_taught as b
where a."count(division)" = b."count(division)";
If you don't you are using the count-function provided by your Database-system.
It's very unusual to name a column like this, it might be either a trap by your tutor or an error while initializing the table in your case.
I think you just want a count(distinct):
select count(distinct division)
from num_taught;

SQL full text search behavior on numeric values

I have a table with about 200 million records. One of the columns is defined as varchar(100) and it's included in a full text index. Most of the values are numeric. Only few are not numeric.
The problem is that it's not working well. For example if a row contains the value '123456789' and i look for '567', it's not returning this row. It will only return rows where the value is exactly '567'.
What am I doing wrong?
sql server 2012.
Thanks.
Full text search doesn't support leading wildcards
In my setup, these return the same
SELECT *
FROM [dbo].[somelogtable]
where CONTAINS (logmessage, N'28400')
SELECT *
FROM [dbo].[somelogtable]
where CONTAINS (logmessage, N'"2840*"')
This gives zero rows
SELECT *
FROM [dbo].[somelogtable]
where CONTAINS (logmessage, N'"*840*"')
You'll have to use LIKE or some fancy trigram approach
The problem is probably that you are using a wrong tool since Full-text queries perform linguistic searches and it seems like you want to use simple "like" condition.
If you want to get a solution to your needs then you can post DDL+DML+'desired result'
You can do this:
....your_query.... LIKE '567%' ;
This will return all the rows that have a number 567 in the beginning, end or in between somewhere.
99% You're missing % after and before the string you search in the LIKE clause.
es:
SELECT * FROM t WHERE att LIKE '66'
is the same as as using WHERE att = '66'
if you write:
SELECT * FROM t WHERE att LIKE '%66%'
will return you all the lines containing 2 'sixes' one after other

Exclude records with specific value '*' SQL SELECT

I am in the processing of preparing for migration to a new database system for a stock based retail store. In the current database products which have been deactivated have a leading * added to the record.
The owners do not want to bring deactivated products into the new system so this leading * is my only reference point to work from.
I need to create a SELECT query that will exclude products that have a leading * but so for to no avail.
I have tried the below
SELECT prdcod
FROM prdtbl
WHERE prodcod<>'*%';
The first 10 results returned are:
71A022
051116
070505PRO
*031620
458508
501315
*070247PE
370002
070278STU
*CO20302
I suspect I may not be able to use the * as an excluding factor
Any thoughts would be appreceated
This can be done in so many ways..
Using NOT LIKE
SELECT prdcod
FROM prdtbl
WHERE prodcod NOT LIKE '*%'
Using LEFT/SUBSTRING
SELECT prdcod
FROM prdtbl
WHERE LEFT(prodcod,1) <> '*' -- SUBSTRING(prodcod from 1 for 1) <> '*'
Note : Not Like is preferred approach if you have a Index on prodcod column
try this
SELECT prdcod
FROM prdtbl
WHERE prodcod not like '*%';
SELECT prdcod
FROM prdtbl
WHERE prodcod NOT LIKE '*%';
instead of using "<>" operator, use "not like":
Select prdcod
From prdtbl
Where prodcod not like '*%';

using select count based on partial data

I'm currently making a call to an SQL database that counts all entries where the cell starts with NOI, but ends with anything else.
I thought using the below would work, but it doesn't seem to, anyone have any ideas? I know the % sign is the wildcard for foxpro, I don't know if this is the same in SQL
SELECT COUNT * FROM DIARY WHERE PTNOTE = 'NOI%'
You have to use LIKE if you want to use the wildcard characters:
SELECT COUNT(*) FROM DIARY WHERE PTNOTE LIKE 'NOI%'
(also added the parantheses around *)
You are missing parentheses:
SELECT COUNT(*)
FROM DIARY
WHERE PTNOTE = 'NOI%';
It is not the case even in Foxpro. You should use parentheses and "like":
SELECT COUNT(*) FROM DIARY WHERE PTNOTE like 'NOI%'

Return rows where first character is non-alpha

I'm trying to retrieve all columns that start with any non alpha characters in SQlite but can't seem to get it working. I've currently got this code, but it returns every row:
SELECT * FROM TestTable WHERE TestNames NOT LIKE '[A-z]%'
Is there a way to retrieve all rows where the first character of TestNames are not part of the alphabet?
Are you going first character only?
select * from TestTable WHERE substr(TestNames,1) NOT LIKE '%[^a-zA-Z]%'
The substr function (can also be called as left() in some SQL languages) will help isolate the first char in the string for you.
edit:
Maybe substr(TestNames,1,1) in sqllite, I don't have a ready instance to test the syntax there on.
Added:
select * from TestTable WHERE Upper(substr(TestNames,1,1)) NOT in ('A','B','C','D','E',....)
Doesn't seem optimal, but functionally will work. Unsure what char commands there are to do a range of letters in SQLlite.
I used 'upper' to make it so you don't need to do lower case letters in the not in statement...kinda hope SQLlite knows what that is.
try
SELECT * FROM TestTable WHERE TestNames NOT LIKE '[^a-zA-Z]%'
SELECT * FROM NC_CRIT_ATTACH WHERE substring(FILENAME,1,1) NOT LIKE '[A-z]%';
SHOULD be a little faster as it is
A) First getting all of the data from the first column only, then scanning it.
B) Still a full-table scan unless you index this column.