SQL select does not return any entries - sql

I am using a Sql query to load a subset of data
SELECT [sfb_id]
,[prs_id_201304]
,[prs_id_201204]
,[vorname]
,[sex]
FROM [IAB\KruegerJ049].[test]
WHERE sex='Weiblich'
the column sex contains the characters Männlich or Weiblich (male or female). I only want to read out rows which contain sex='Weiblich'. But this code does not return any entries!
Thanks in advance

It may be your collation is not right. change the collation and repeat the query . there is not error on your query.

Try WHERE sex LIKE '%Weiblich%' in case there are some spaces before or after.

Try to debug it with babysteps. First check should be if
SELECT * FROM [IAB\KruegerJ049].[test]
returns entrys, then try
SELECT * FROM [IAB\KruegerJ049].[test] WHERE sex='Weiblich'
i think you misstyped something :) you can also try
WHERE sex LIKE '*Weiblich*'

Try this:
WHERE LTRIM(RTRIM(LOWER(sex))) = 'weiblich'

Related

LIKE clause in stored procedure

IN my stored procedure I have
SELECT properName
FROM nameTable
WHERE properName like '%IN_newName%'
But it isn't working on a value that has a space in the first character spot " Name"
I've tried like IN_newName concat'%' and also LOCATE(), but I'm not getting any results. I just need to be able to match the values of a string even if there is a space in the front or back and concat doesn't seem to get it. For reference, properName and IN_newName are both CHARACTER(10)
What else can I try here? This is db2 for iSeries jVersion 7
Have you tried:
like trim(IN_newName) concat'%'
Perhaps you could try:
SELECT properName
FROM nameTable
WHERE properName like CONCAT(LTRIM(IN_newName),'%')

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

Using a GUID In The Where Clause

For some reason I'm unable to use comparisons on GUID columns, it does not return any results.
See below, with the WHERE clause set to the exact value of the 'secguid' column, it does not return any results. What's going on?
SELECT * FROM dbMobileFile
SELECT * FROM dbMobileFile WHERE secguid = '3137459D-EFDE-449E-94A3-89345A8580FA'
SELECT * FROM dbMobileFile WHERE secguid LIKE '3137459D-EFDE-449E-94A3-89345A8580FA'
Using LIKE does not work either.
Try this
SELECT [fileID],
[fileCOde],
[filePassword],
[fileDescription],
[rowguid],
[secguid]
FROM [dbo].[dbMobileFile]
WHERE CAST(secguid as uniqueidentifier) = CAST('3137459D-EFDE-449E-94A3-89345A8580FA' as uniqueidentifier)
Since you mention that the column is stored as NVARCHAR, its possible that the string has leading or trailing whitespaces, which is why it might not be popping up in the query with the WHERE clause.
You can try this :
SELECT [fileID],
[fileCOde],
[filePassword],
[fileDescription],
[rowguid],
[secguid]
FROM [dbo].[dbMobileFile]
WHERE LTRIM(RTRIM(secguid)) = '3137459D-EFDE-449E-94A3-89345A8580FA'
which should show you the result as leading and trailing whitespaces are eliminated in the WHERE clause.
Also, in case you want to make use of the LIKE operator, you can write your query as :
SELECT [fileID],
[fileCOde],
[filePassword],
[fileDescription],
[rowguid],
[secguid]
FROM [dbo].[dbMobileFile]
WHERE secguid LIKE '%3137459D-EFDE-449E-94A3-89345A8580FA%'
Hope this helps!!!
I've had this problem with a corrupt database. Some GUIDs be contained in the WHERE clause, with other GUIDS on the same table would not return results.
Turns out that database had Index issues. Run DBCC to make sure your database isn't corrupt.
The accepted answer works, but it is a bit verbose and probably not the intended way to do this. UniqueIdentifier is qualified by {}, so the following is the easiest;
SELECT * FROM dbMobileFile WHERE secguid = '{3137459D-EFDE-449E-94A3-89345A8580FA}'
See inside the database that guid value stored as 32 hex digits:00000000000000000000000000000000 so if we search by 32 hex digits separated by hyphens: 00000000-0000-0000-0000-000000000000, it's not get any output
Try this:
SELECT * FROM dbMobileFile WHERE secguid = ('3137459D-EFDE-449E-94A3-89345A8580FA')
Use parentheses to enclose GUID string LIKE ('GUID')

Remove string in SQL Server

In my table Products in SQL Server, I have a column UrlLink with values that look like this:
Id UrlLink
-----------------------------------
1 domain/product1.html?7
2 domain/product2.html?34
3 domain/product294.html?6576
4 domain/product54.html?765
How to remove parameter
?7, ?34, ?6576, ?765
from column UrlLink?
Thanks!
To remove the query string part from the UrlLink column in the table, you need to use Left and CharIndex in your UPDATE statement.
UPDATE Products
SET UrlLink = LEFT(UrlLink, CHARINDEX('?',UrlLink)-1)
Using left and charindex should work:
select left(UrlLink, charindex('?',UrlLink)-1) from Products;
This would return everything before the first occurrence of a ?. You might want to add some null checks if parameter isn't mandatory in the UrlLink column.
Try with this code:
DECLARE #myvar varchar(100);
SET #myvar = 'domain/product1.html?7';
SELECT REVERSE(SUBSTRING((REVERSE(#myvar)), (CHARINDEX('?',REVERSE(#myvar))+1),500)) AS result ;
GO
Simple and won't fail in case you have a Urllink that does not contain?.
In case of multiple ? removes everything from the first ? since in URL this is the only ? with special significance.
select id,left(Urllink,charindex('?',Urllink+'?')-1)
from Products
You just have to find ? in your url and take string upto it. You can use LEFT/SUBSTRING for getting substring and CHARINDEX for finding ? in your string. Check out my query below
select id, substring(urllink,1,charindex('?',urllink)-1)
from products
In case multiple ? symbols are there in the UrlLink column values and if you want to take the value after the last ? symbol. Then use a combination of SUBSTRING and CHARINDEX .
Query
SELECT
[Id],
SUBSTRING([UrlLink], 1,
LEN([UrlLink]) - CHARINDEX('?', REVERSE([UrlLink]), 1)) AS [UrlLink]
FROM [Products];
Demo

SIMPLE SQL Select Where Query

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.