Why is this query not working for number 1? - sql

this query returns 5 results as expected
select * from [SalesLogix].[sysdba].[LEAD] where USERFIELD1 like '0'
but this query returns nothing
select * from [SalesLogix].[sysdba].[LEAD] where USERFIELD1 like '1'
the USERFIELD1 is a varchar 80 field
Here is all the data that is in the DB and as you can see there are two records with USERFIELD1 = '1' but there seems to be a line break after them...maybe that is causing the issue...is there anyway to grab those two records
LEADID,CREATEUSER,CREATEDATE,MODIFYUSER,MODIFYDATE,ACCOUNTMANAGERID,ASSIGNDATE,BUSINESSDESCRIPTION,COMPANY,COMPANY_UC,CREDITRATING,DATAQUALITY,DESCRIPTION,DIVISION,DONOTSOLICIT,EMAIL,EMPLOYEES,FAX,FIRSTNAME,HOMEPHONE,IMPORTID,IMPORTSOURCE,INDUSTRY,INTERESTS,ISPRIMARY,LASTCALLDATE,LASTNAME,LASTNAME_UC,LEADSOURCEID,MIDDLENAME,MOBILE,NEXTCALLDATE,NOTES,PREFERRED_CONTACT,PREFIX,PRIORITY,QUALIFICATION_CATEGORYID,REVENUE,SECCODEID,SICCODE,STATUS,SUFFIX,TICKER,TITLE,TOLLFREE,TYPE,USERFIELD1,USERFIELD2,USERFIELD3,USERFIELD4,USERFIELD5,USERFIELD6,USERFIELD7,USERFIELD8,USERFIELD9,USERFIELD10,WEBADDRESS,WORKPHONE,LEAD_ADDRESSID,DONOTEMAIL,DONOTFAX,DONOTMAIL,DONOTPHONE
Q134915558 ,U6UJ9A00000S,2011-09-20 17:36:10.053,U6UJ9A00000S,2011-09-20 17:36:10.053,NULL,2011-09-20 17:36:10.053,NULL,Johndoe,JOHNDOE,NULL,NULL,NULL,NULL,0,test#gmail.com,NULL,NULL,Harry,NULL,NULL,NULL,NULL,Restaurant Pro Express demo download,T,NULL,Scott,SCOTT, ,NULL,NULL,NULL,this is from the site,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,1
,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4075559999,QQ134915558 ,NULL,NULL,NULL,NULL
Q39769667 ,U6UJ9A00000S,2011-09-20 17:46:18.103,U6UJ9A00000S,2011-09-20 17:46:18.103,NULL,2011-09-20 17:46:18.103,NULL,scaoo,SCAOO,NULL,NULL,NULL,NULL,0,harry333#harry.com,NULL,NULL,upper2,NULL,NULL,NULL,NULL,Aldelo for Restaurants demo download,T,NULL,Scott,SCOTT,L6UJ9A000004,NULL,NULL,NULL,this is a download,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4074615519,QQ39769667 ,NULL,NULL,NULL,NULL
Q488888476 ,U6UJ9A00000S,2011-09-20 17:49:28.963,U6UJ9A00000S,2011-09-20 17:49:28.963,NULL,2011-09-20 17:49:28.963,NULL,Johndoe,JOHNDOE,NULL,NULL,NULL,NULL,0,markus#gmail.com,NULL,NULL,upper,NULL,NULL,NULL,sales,posnation.com online demo request,T,NULL,Scott,SCOTT,L6UJ9A000004,NULL,NULL,NULL,this is from upper,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4074615519,QQ488888476 ,NULL,NULL,NULL,NULL
Q504845720 ,U6UJ9A00000S,2011-09-20 17:06:10.053,U6UJ9A00000S,2011-09-20 17:06:10.053,U6UJ9A00000G,2011-09-20 17:06:10.053,NULL,Rafner ext.,RAFNER EXT.,NULL,NULL,NULL,NULL,0,raf#raf.com,NULL,NULL,James,4075615519,NULL,NULL,sales,NULL,NULL,NULL,Rafner,RAFNER, ,NULL,NULL,NULL,Raf associates is asking a question,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,QQ504845720 ,NULL,NULL,NULL,NULL
Q539171226 ,U6UJ9A00000S,2011-09-20 17:49:28.963,U6UJ9A00000S,2011-09-20 17:49:28.963,NULL,2011-09-20 17:49:28.963,NULL,scaoo,SCAOO,NULL,NULL,NULL,NULL,0,harry333#harry.com,NULL,NULL,upper3,NULL,NULL,NULL,NULL,Aldelo for Restaurants demo download,T,NULL,Scott,SCOTT,L6UJ9A000004,NULL,NULL,NULL,this is a download,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4074615519,QQ539171226 ,NULL,NULL,NULL,NULL
Q547088411 ,U6UJ9A00000S,2011-09-20 17:46:18.103,U6UJ9A00000S,2011-09-20 17:46:18.103,NULL,2011-09-20 17:46:18.103,NULL,Johndoe,JOHNDOE,NULL,NULL,NULL,NULL,0,markus#gmail.com,NULL,NULL,upper,NULL,NULL,NULL,sales,posnation.com online demo request,T,NULL,Scott,SCOTT,L6UJ9A000004,NULL,NULL,NULL,this is from upper,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4074615519,QQ547088411 ,NULL,NULL,NULL,NULL
Q913526837 ,U6UJ9A00000S,2011-09-20 17:36:10.053,U6UJ9A00000S,2011-09-20 17:36:10.053,NULL,2011-09-20 17:36:10.053,NULL,Johndoe,JOHNDOE,NULL,NULL,NULL,NULL,0,test#gmail.com,NULL,NULL,Parry,NULL,NULL,NULL,NULL,Restaurant Pro Express demo download,T,NULL,Scott,SCOTT, ,NULL,NULL,NULL,this is from the site agan,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,1
,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4075559999,QQ913526837 ,NULL,NULL,NULL,NULL
Q925684753 ,U6UJ9A00000S,2011-09-21 09:36:10.420,U6UJ9A00000S,2011-09-21 09:36:10.420,NULL,2011-09-21 09:36:10.420,NULL,POSfasion,POSFASION,NULL,NULL,NULL,NULL,0,sdfa#ss.com,NULL,NULL,Maria,NULL,NULL,NULL,NULL,Aldelo for Restaurants demo download,T,NULL,becker4,BECKER4,L6UJ9A000004,NULL,NULL,NULL,this is another lead from the live site,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,7778889999,QQ925684753 ,NULL,NULL,NULL,NULL

If there are characters after the 1 in the userField1 then try using a wildcard, as shown below.
select * from [SalesLogix].[sysdba].[LEAD] where USERFIELD1 like '1%'
Also the statement you wrote
select * from [SalesLogix].[sysdba].[LEAD] where USERFIELD1 like '1'
Is essentially equivalent to
select * from [SalesLogix].[sysdba].[LEAD] where USERFIELD1 = '1'
You may want to read up on using the LIKE clause
http://msdn.microsoft.com/en-us/library/ms179859.aspx

Use
where USERFIELD1 = '1
'
You don't need LIKE as you aren't using any wild cards.

You're using LIKE wrong. Put a wildcard or a pattern in there or use an = operator.
Your data seems malformed.

Try a like with the value surrounded by a wildcard
ie USERFIELD1 like '1%'
Should sort out whatever problem you're having if it is related to the line break field

Related

SELECT instead of TABLE and WHERE in UPDATE?

I've seen may answers to the same kind of question but I still doubt.
UPDATE in SQL should be something like :
UPDATE *Table*
SET *choose value*
WHERE *what do we change*
I would like to know if there is possibilites to use a select instead of TABLE (an so instead of WHERE)
Like
UPDATE *Select conditions and rows*
SET *What do we change (the where is implicit)
I know UPDATE/SET/WHERE works well, but I'm exploring other possibilites :)
Thanks,
Nicolas
EXAMPLE :
Have to do :
update produits
set `NO_FOURNISSEUR` = "30"
where `NO_FOURNISSEUR` = "3"
would like to try something like :
update select * from produits where produits.`no_fournisseur`= "30"
set `NO_FOURNISSEUR`= "3"
MariaDB has a with expression, like so:
https://mariadb.com/kb/en/library/with/
so you would have:
WITH t AS (
select *
from produits
where produits.no_fournisseur= '30')
UPDATE t SET t.no_fournisseur = '3';
Yes. The ANSI SQL standard way to do this is using a Common Table Expression:
with dt as
(
select *
from produits
where produits.no_fournisseur= '30'
)
update dt set NO_FOURNISSEUR = '3'
This standard syntax supports joins and other query constructs in the SELECT part, and gives you a simple way to examine the rows before applying the update.

Returning values from a database with a Substring

Let's say I've got 3 product description fields with the values AC-120 XXX, AC-120,CCC and AC-120 BBB.
How would I get that information from a table using only AC-120 as my search argument?
I've tried using the subStr function but that won't return any values either
SELECT TbArtikel.Artikel_Merk, TbArtikel.Artikel_Groep, TbArtikel.Artikel_Categorie_ID, TussenMAATenARTKEL.VoorraadNummer, TbArtikel.Artikel_ID, TussenMAATenARTKEL.ArtikelDetail_ID, TbArtikel.Artikel_Prijs_Advies, TbArtikel.Artikel_Prijs_Bees, TbArtikel.Artikel_Omschrijving
FROM TbArtikel INNER JOIN TussenMAATenARTKEL ON TbArtikel.Artikel_ID = TussenMAATenARTKEL.Artikel_ID
WHERE (((TbArtikel.Artikel_Merk)="Yonex") AND ((TbArtikel.Artikel_Omschrijving)="%AC-102%"));
LIKE is very appropriate for this comparison. However, MS Access uses different wildcards from standard SQL. So you want:
TbArtikel.Artikel_Omschrijving) LIKE "*AC-102*"
In standard SQL, this would be:
TbArtikel.Artikel_Omschrijving) LIKE "%AC-102%"
Try '((TbArtikel.Artikel_Omschrijving) LIKE "%AC-102%"' instead of '((TbArtikel.Artikel_Omschrijving)="%AC-102%"'
Use MID() function as
MID(TbArtikel.Artikel_Omschrijving, 1, 6) = 'AC-120'
Instead of
TbArtikel.Artikel_Omschrijving ="%AC-102%"

Google BigQuery: TABLE_QUERY AND TABLE_DATE_RANGE

I have Big Query tables like below, and like to issue a query to the tables marked <=.
prefix_AAAAAAA_20170320
prefix_AAAAAAA_20170321
prefix_AAAAAAA_20170322 <=
prefix_AAAAAAA_20170323 <=
prefix_AAAAAAA_20170324 <=
prefix_AAAAAAA_20170325
prefix_BBBBBBB_20170320
prefix_BBBBBBB_20170321
prefix_BBBBBBB_20170322 <=
prefix_BBBBBBB_20170323 <=
prefix_BBBBBBB_20170324 <=
prefix_BBBBBBB_20170325
prefix_CCCCCCC_20170320
prefix_CCCCCCC_20170321
prefix_CCCCCCC_20170322
prefix_CCCCCCC_20170323
prefix_CCCCCCC_20170324
prefix_CCCCCCC_20170325
I made a query as this
SELECT * FROM
(TABLE_QUERY(mydataset,
'table_id CONTAINS "prefix" AND
(table_id CONTAINS "AAAAAA" OR table_id CONTAINS "BBBBBB")' )
AND
TABLE_DATE_RANGE(mydataset.prefix, TIMESTAMP('2017-03-22'), TIMESTAMP('2017-03-24')))
I got this error.
Error: Encountered " "AND" "AND "" at line 5, column 4. Was expecting: ")" ...
Does anybody has ideas?
You cannot mix TABLE_QUERY and TABLE_DATE_RANGE for exactly same FROM!
Try something like below
#legacySQL
SELECT *
FROM (TABLE_QUERY(mydataset, 'REGEXP_MATCH(table_id, "prefix_[AB]{7}_2017032[234]")'))
Consider Migrating to BigQuery Standard SQL
In this case you can Query Multiple Tables Using a Wildcard Table
See How to Migrate from TABLE_QUERY() to _TABLE_SUFFIX
I think, in this case your query can look like
#standardSQL
SELECT *
FROM `mydataset.prefix_*`
WHERE REGEXP_CONTAINS(_TABLE_SUFFIX, '[AB]{7}_2017032[234]')
I can not migrate to Standard SQL because ...
If I would like to search for example between 2017-03-29 and 2017-04-02, do you have any smart SQL
Try below version
#legacySQL
SELECT *
FROM (TABLE_QUERY(mydataset,
'REGEXP_MATCH(table_id, r"prefix_[AB]{7}_(\d){8}") AND
RIGHT(table_id, 8) BETWEEN "20170329" AND "20170402"'))
Of course yo can adjust above to use whatever exactly logic yo need to apply!

looking for db2 text function or method I can do a text contain rather than like

I'm looking for a db2 function that does a text contain search. At present I am running the following query against the data below....
SELECT distinct
s.search_id,
s.search_heading,
s.search_url
FROM repman.search s, repman.search_tags st
WHERE s.search_id = st.search_id
AND ( UPPER(s.search_heading) LIKE (cast('%REPORT%' AS VARGRAPHIC(32)))
OR (UPPER(st.search_tag) LIKE cast('%REPORT%' AS VARGRAPHIC(32)))
)
ORDER BY s.search_heading;
Which returns...
But if I change the search text to %REPORTS% rather than %REPORT% (which I need to do) the like search does not work and I get zero results.
I read a link that used a function named CONTAINS like below but when trying to use the function I get an error.
SELECT distinct
s.search_id,
s.search_heading,
s.search_url
FROM repman.search s, repman.search_tags st
WHERE s.search_id = st.search_id
AND CONTAINS(s.search_heading, 'REPORTS') = 1
Has anynoe got any suggestions? I'm on db2 version DB2/LINUXPPC 9.1.6.
Thanks
In order to look for a pattern in a string, you can use Regular Expressions. They are built-in DB2 with xQuery since DB2 v9. There are also other ways to do that. I wrote an article in my blog (in Spanish that you can translate) about Regular Expressions in DB2.
xmlcast(xmlquery('fn:matches(\$TEXT,''^[A-Za-z 0-9]*$'')')

SQL Server can not find word

When I search for the Dünyas word with a LIKE operator I can see the results but when I search Dünyası word with LIKE operator, it returns no results. Problem is not ı or ü letters I tried with i and u letters as well.
This query:
select * from DbDivx where NameTurkish like '%Dünyas%' =>
returns:
Boksörün Dünyası
Geleceğin Dünyası
Su Dünyası
However:
select * from DbDivx where NameTurkish like '%Dünyası%' =>
returns nothing.
Can you help me please?
http://sqlfiddle.com/#!6/8c359/1
create table test (someName nvarchar(300))
insert into test (someName)
select N'Boksörün Dünyası'
union all select N'Geleceğin Dünyası'
union all select N'Su Dünyası'
select * from test where someName like N'%Dünyası%'
Not having any issues with the above fiddle. Note that when i inserted data without the N in front of it, the like query did not return results.
hopefully this helps!