White-space between two values - sql

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

Related

How to use SQL function CONCAT with LIKE

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

Apply like function on an array is SQL Server

I am getting array from front end to perform filters according that inside the SQL query.
I want to apply a LIKE filter on the array. How to add an array inside LIKE function?
I am using Angular with Html as front end and Node as back end.
Array being passed in from the front end:
[ "Sports", "Life", "Relationship", ...]
SQL query is :
SELECT *
FROM Skills
WHERE Description LIKE ('%Sports%')
SELECT *
FROM Skills
WHERE Description LIKE ('%Life%')
SELECT *
FROM Skills
WHERE Description LIKE ('%Relationship%')
But I am getting an array from the front end - how to create a query for this?
In SQL Server 2017 you can use OPENJSON to consume the JSON string as-is:
SELECT *
FROM skills
WHERE EXISTS (
SELECT 1
FROM OPENJSON('["Sports", "Life", "Relationship"]', '$') AS j
WHERE skills.description LIKE '%' + j.value + '%'
)
Demo on db<>fiddle
As an example, for SQL Server 2016+ and STRING_SPLIT():
DECLARE #Str NVARCHAR(100) = N'mast;mode'
SELECT name FROM sys.databases sd
INNER JOIN STRING_SPLIT(#Str, N';') val ON sd.name LIKE N'%' + val.value + N'%'
-- returns:
name
------
master
model
Worth to mention that input data to be strictly controlled, since such way can lead to SQL Injection attack
As the alternative and more safer and simpler approach: SQL can be generated on an app side this way:
Select * from Skills
WHERE (
Description Like '%Sports%'
OR Description Like '%Life%'
OR Description Like '%Life%'
)
A simple map()-call on the words array will allow you to generate the corresponding queries, which you can then execute (with or without joining them first into a single string).
Demo:
var words = ["Sports", "Life", "Relationship"];
var template = "Select * From Skills Where Description Like ('%{0}%')";
var queries = words.map(word => template.replace('{0}', word));
var combinedQuery = queries.join("\r\n");
console.log(queries);
console.log(combinedQuery);

Can't get delphi SQL LIKE to work with %

I'm doing a school project and need to code a query to filter a dataset to certain variables. All my SQL works fine, except I can't get the LIKE statement to work with %-signs. I believe my syntax is wrong. Can anybody please tell me what I'm doing wrong. Thanks
The code:
qryMovie.SQL.Clear;
qryMovie.SQL.Add('SELECT * FROM Movies');
qryMovie.SQL.Add('WHERE Genre = ' + QuotedStr(genre));
qryMovie.SQL.Add('AND Price BETWEEN ' + minPrice + ' AND ' + maxPrice);
qryMovie.SQL.Add('AND Title LIKE %' + title + '%');
qryMovie.Open;
Error produced:
'Syntax error in query expression 'Genre = 'Action/Adventure'
AND Price BETWEEN 0 AND 200
AND Title LIKE %Star Wars%''
LIKE %Star Wars%
but you need
LIKE '%Star Wars%'
You need to quote % with ':
qryMovie.SQL.Add(' AND Title LIKE ''%' + title + '%''');
Anyway you should use binded parameters instead of concatenating SQL string. It is error-prone and could lead to SQL Injection attacks.

SQL Inner Query In Replace Statement

I would like to replace a certain part of a columns field with something else. The only issue is that what I've tried doesn't work. I'm using HeidiSQL
This is a overview of the database:
Database Overview
What I would like to do is for each user_id with field_id 523 I would like to replace 'centraal-zorgportaal.nl/afbeeldingen/' with '/profiles/(for each user id here)/'.
So the end result example after running the query will look like this:
'/profiles/711/logo.gif', but then for each user with a diffrent logo.gif.
Note: Can't post more than two links so removed the http etc. in front.
This is the query I've tried and dind't work:
update wp_bp_xprofile_data
set value = replace( value,
'http://www.centraal-zorgportaal.nl/afbeeldingen/',
'/profiles/' +
(select user_id
from wp_bp_xprofile_data
where user_id = #n := #n + 1 n) +
'/')
where field_id = 523
(Table name is: wp_bp_xprofile_data)
This is the error message I received:
Error message:
Could anyone explain why this doesn't work, how to fix it and the best way to approach this problem?
In MySQL/MariaDB, you cannot refer to the table being modified. From your description, you seem to want something like this:
update wp_bp_xprofile_data
set value = replace(value,
'http://www.centraal-zorgportaal.nl/afbeeldingen/',
concat('/profiles/', user_id, '/')
)
where field_id = 523;

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
+'%' )