How to use SQL function CONCAT with LIKE - sql

I confirmed that the columns in the SQL table contain the results I'm searching for (the results being NA), but when I use the query below, no results are returned.
Could I get some help with this please?
SELECT DISTINCT *
FROM [DB_NAME].[dbo].[TABLE_NAME]
WHERE BECDescription like '%Time Warner%' AND
(CONCAT(PhysicalAddress1, PhysicalCity, PhysicalStateProvince) like ('%NA% %NA% %NA%')
) AND
PhysicalCountry like '%NA%' AND
CarrierName like '%NA%' AND
CurrNetChargeAmt = 1326.00

I'm a little lost on why you are using CONCAT() here. Doesn't this do what you want?
WHERE BECDescription like '%Time Warner%' AND
PhysicalAddress1 like '%NA%' AND
PhysicalCity like '%NA%' AND
PhysicalStateProvince like '%NA%' AND
PhysicalCountry like '%NA%' AND
CarrierName like '%NA%' AND
CurrNetChargeAmt = 1326.00
It is certainly simpler to understand.

You concat PhysicalAddress1, PhysicalCity, PhysicalStateProvince but you add spaces between the '%NA'.
Try
SELECT DISTINCT *
FROM [db_name].[dbo].[table_name]
WHERE BECDescription like '%Time Warner%' AND (CONCAT(PhysicalAddress1, ' ', PhysicalCity, ' ', PhysicalStateProvince) like ('%NA% %NA% %NA%')) AND PhysicalCountry like '%NA%' AND CarrierName like '%NA%' AND CurrNetChargeAmt = 1326.00

Related

how to use ignore case in spring JPA #query?

I have query as below:
#Query("SELECT b FROM Brand b WHERE b.name1 LIKE %:name1% or b.name2 like %:name2%")
List<Brand> findSome(#Param("name1") String name1, #Param("name2") String name2);
I want to ignore case so that I tried to modified it
#Query("SELECT b FROM Brand b WHERE lower(b.name1) LIKE lower(%:name1%) or lower(b.name2) like lower(%:name2%)")
But this looks like don't work, how can I do?
The problem is probably the syntax of your JP-Query. Try to use this:
lower(b.name1) LIKE concat('%', lower(:name1), '%')
Further reference: http://openjpa.apache.org/builds/1.2.3/apache-openjpa/docs/jpa_langref.html#jpa_langref_like

White-space between two values

I have a column as like given below
EB.Liter+'L'+EB.CC+'cc'+'-ci'+EB.BlockType+EB.Cylinders+
(
EB.EngBoreIn+'x'+
EB.EngStrokeIn+';'+
EB.EngBoreMetric+'x'+
EB.EngStrokeMetric
) as EngineBase
This pull data something like this
1.3 L 1339 cc L 4 (2.87 x3.15 ;73.0 x80.0 )
but i am trying to get something like
1.3L 1339cc -ci L4 (2.87x3.15; 73.0x80.0)
may anyone please suggest if possible
i have tried something like this.
LTRIM(RTRIM(EB.Liter))+'L'+LTRIM(RTRIM(EB.CC))+'cc'+'-
ci'+LTRIM(RTRIM(EB.BlockType))+LTRIM(RTRIM(EB.Cylinders))+
+ (
LTRIM(RTRIM(EB.EngBoreIn))+'x'+
LTRIM(RTRIM(EB.EngStrokeIn))+';'+
LTRIM(RTRIM(EB.EngBoreMetric))+'x'+
LTRIM(RTRIM(EB.EngStrokeMetric))
) as EBase
which get me result as below
1.3L1339cc-ciL42.87x3.15;73.0x80.0
but correct output is as
1.3L 1339cc -ciL4(2.87x3.15; 73.0x80.0)
modify like this
LTRIM(RTRIM(EB.Liter))+'L '+LTRIM(RTRIM(EB.CC))+'cc '+'-ci'+LTRIM(RTRIM(EB.BlockType))+LTRIM(RTRIM(EB.Cylinders))+
+ '('+
LTRIM(RTRIM(EB.EngBoreIn))+'x'+
LTRIM(RTRIM(EB.EngStrokeIn))+'; '+
LTRIM(RTRIM(EB.EngBoreMetric))+'x'+
LTRIM(RTRIM(EB.EngStrokeMetric))
+')' as EBase
you can use trim functionality to remove leading and trailing spaces. Your query would look like that then:
TRIM(EB.Liter) + 'L'+TRIM(EB.CC)+'cc'+'-ci'+TRIM(EB.BlockType)+TRIM(EB.Cylinders)+
(
TRIM(EB.EngBoreIn)+'x'+
TRIM(EB.EngStrokeIn)+';'+
TRIM(EB.EngBoreMetric)+'x'+
TRIM(EB.EngStrokeMetric)
) as EngineBase

Grouping multiple 'AND x not like y' In SQL statement

I currently have an SQL statement that is too long for my program (I have a maximum number of character that I can use. I'm using sccm report). The problem is my SQL statement look like this:
Select distinct v_GS_ADD_REMOVE_PROGRAMS_64.DisplayName0, v_GS_ADD_REMOVE_PROGRAMS_64.Publisher0, v_GS_ADD_REMOVE_PROGRAMS_64.Version0
FROM v_GS_ADD_REMOVE_PROGRAMS_64
JOIN v_R_System ON v_GS_ADD_REMOVE_PROGRAMS_64.ResourceID = v_R_System.ResourceID
WHERE (v_R_System.Netbios_Name0 = #computername)
DisplayName0 NOT LIKE 'hpp%'
AND
DisplayName0 NOT LIKE 'Logitech SetPoint%'
AND
DisplayName0 NOT LIKE 'HP Document Manager%'
AND
DisplayName0 NOT LIKE 'HP Imaging Device Functions%'
AND
DisplayName0 NOT LIKE 'PyQt4 - PyQwt5%'
And it goes on and on for 20 pages. How can I minimize the amount of code this request contains? Is there a way to group all the displayName0 not like ?? with something like a NOT IN(value1, value2, ...)?
If you are OK without tailing % in you pattern you can replace it with:
SELECT ... WHERE DisplayName0 NOT IN ('hpp','Logitech SetPoint','HP Document Manager',...)
It would make it somehow shorter.
But it seems to me that proper solution would be to create [temp] table with all the names you need to filter against and then join it.
Could you store the values in a separate table and then reference it in your query like this?:
SELECT DISTINCT v_GS_ADD_REMOVE_PROGRAMS_64.DisplayName0
,v_GS_ADD_REMOVE_PROGRAMS_64.Publisher0
,v_GS_ADD_REMOVE_PROGRAMS_64.Version0
FROM v_GS_ADD_REMOVE_PROGRAMS_64
JOIN v_R_System ON v_GS_ADD_REMOVE_PROGRAMS_64.ResourceID = v_R_System.ResourceID
WHERE (v_R_System.Netbios_Name0 = #computername) DisplayName0 NOT IN (
SELECT DisplayName0
FROM < NewTableName >
)

Is there an advanced like function where i can get results even if one word matches

hello I am trying to achieve something like this
should give results if
where 'Robert Doug' like '%Robert'
or
where 'Robert Doug' like '%Doug Robert'
if users enters first name or last name and if it matches in that whole string it should return the record.
Any help is appreciated.
Try like this
SELECT * FROM [YOUR TABLE_NAME] WHERE [COLUMN_NAME] LIKE ('%'+[USER PROMPT]+'%')
Or try this..
SELECT PATINDEX('%Robert%',NAME_COLUMN)
FROM [YOUR TABLE_NAME]
Or..
SELECT NAME_COLUMN
FROM [YOUR TABLE_NAME]
WHERE PATINDEX('%Robert%',NAME_COLUMN)<>'0'
Thank you all i tried something like this
WHERE
( d_searchJobTitle.term like '%'+case when CHARINDEX(' ','implementation analyst') >0 then SUBSTRING('implementation analyst',1,CHARINDEX(' ','implementation analyst')) else 'implementation analyst' end+'%'
and
d_searchJobTitle.term like '%'+ case when CHARINDEX(' ','implementation analyst') >0 then SUBSTRING('implementation analyst',CHARINDEX(' ','implementation analyst'),len('implementation analyst'))
else 'implementation analyst' end
+'%' )

Why like operator doesn't work - Sqlite

I have this query that would return a search from base with the operation Like '%srch'
srch=search.getText().toString();
...
db.rawQuery("SELECT * FROM MyTable WHERE Name LIKE 'srch%' or Name LIKE '%srch' ",null);
but it doesn't work.
Can you explain why it doesn't work, and how I can fix it ?
thanks a lot
You can fix the query by changing it as below:
db.rawQuery("SELECT * FROM MyTable WHERE Name LIKE ?",
new String[] { "%" + srch + "%" });
why dont you use just one LIKE
db.rawQuery("SELECT * FROM MyTable WHERE Name LIKE '%'" + srch + "'%' ",null);
i find the solution i just do like this
db.rawQuery("SELECT * FROM MyTable WHERE name LIKE '%"+srch+"%'",null);
and its okay thanks all for help