SQL query encountering issue after introducing apostrophe symbol ''`

SELECT CONCAT (
        schoolname
        ,CASE 
            WHEN schoolcity IS NULL
                OR schoolcity = ''
                THEN ''
            ELSE (', ' + REPLACE(schoolcity, 'ã', 'a'))
            END
        ,CASE 
            WHEN schoolname IS NULL
                OR schoolstate = ''
                THEN ''
            ELSE (', ' + schoolstate)
            END
        ) AS schoolname
FROM schools
WHERE schoolname LIKE '"+filter+"%'
ORDER BY schoolname

This particular query is causing some unexpected behavior during execution. It seems like when an apostrophe is added in the filter from the search bar, the desired results are not being displayed.

For example: Trying to search for "Types children'" and nothing appears

The expected outcome should be to display school names containing "children's". Can anyone provide assistance with this issue?

Answer №1

It seems that the issue lies with how your WHERE clause is being processed:

WHERE schoolname LIKE 'children'%'

When you input an apostrophe, it is mistakenly recognized as the end of the value. To include another apostrophe, it must be escaped as a double apostrophe.

If you type this into the search bar, it should be understood as:

WHERE schoolname LIKE 'children''%'

Test these query options in SSMS directly to observe the distinction.

Answer №2

Remember to always use two quotes when using apostrophes like ''

Here is a straightforward query:

select schoolname from school where schoolname like 'children''%'

If you are passing @filter as a variable, you can do the following:

declare @filter nvarchar(20)

select CONCAT(schoolname, CASE WHEN schoolcity IS NULL OR schoolcity = '' THEN '' Else  (', ' + REPLACE(schoolcity,'ã','a')) END,CASE WHEN schoolname IS NULL OR schoolstate = '' THEN '' Else (', ' + schoolstate) END) as schoolname from schools where schoolname like '%' + @filter + '%' order by schoolname

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error: The identifier has already been declared and cannot be re-declared

I am attempting to create a modal-cookie feature that will display a modal on page load if a cookie named "name" does not exist. However, I encountered an error: Uncaught SyntaxError: Identifier 'addCookie' has already been declared. This erro ...

Looking to divide a string into smaller parts without using the split function

Can someone help me with splitting a string like this? 123.45.67.890-1292 connected to EDS via 10.98.765.432-4300. I want to split it in the following way: "123.45.67.890-1292 connected to EDS via 10.98.765.432-4300." -----+------- --+- ...

Displaying Elements of a JSON Array using v-for

I am currently working with an API that stores my Data in JSON format. While I am able to successfully load and display the entire array, I am facing challenges in displaying specific fields. The array is located in a field: peoples: '&ap ...

When loading JSON data dynamically into a D3 network visualization, the links may not be immediately visible

In my possession is a json file containing nodes and links defined with source and target attributes. { "links":[ {"_id": "5a4b2866235fa755b6576903", "target": 6746, "source": 2169}, {"_id": "5a4b2866235fa755b65768e3", "target": 67 ...

Bootstrap 4 rows causing dropdown clipping

I am experiencing a strange problem with Bootstrap 4 when using dropdown. I have additional actions listed in the dropdown menu that are supposed to be visible when clicking on the "plus icon". However, when I do click on the "plus icon", the dropdown ends ...

What is the reason for the inclusion of [Circular] in the hash?

Below is the code snippet: db.query(str, arr, function selectCb(error, results, fields) { if (error) { return proceed(false, {errno:'010',message:error.message}, request); } var q = async.queue ...

Error: CSS and JavaScript files cannot be found

Currently working with an Orchard Asp.net MVC 3 Project. Upon examining the source code in the browser, I notice all the JS and CSS files being referenced/imported. However, clicking on the URL for certain JS files located in the MediaLibrary or Modules f ...

Changing color in JQuery based on class and the content of HTML elements

I am attempting to utilize jQuery to extract the inner html of a div with the class name "box_bottom". These divs are dynamically generated by JavaScript, fetching data from XML. As a result, there could be multiple occurrences based on the XML, and I nee ...

The compatibility issue between Rails 7 and Bootstrap 5.2.3, along with importmaps JavaScript, is causing dysfunction in the

Feeling a bit lost here, as I've tried several solutions from Stack Overflow related to getting bootstrap 5.2.3 javascript to work for a dropdown menu. Importmaps seem like the best approach, although esbuild was attempted with no luck. Below is a sn ...

Passing Optional Parameters to AngularJS Modal

Is there a way to pass an optional parameter to my angularJS modal? Let's take a look at the code: TRIGGER CONTROLLER: $modal.open({ templateUrl: 'UploadPartial.html', controller: 'photos.uploadCtrl', resolve: { presele ...

Creating multiple div columns dynamically with jQuery

Check out the code snippet below: <div id="container"> <div class="b">test1</div> <div class="b">test2</div> <div class="b">test3</div> <div class="b">test4</div> <div class="b"& ...

I am seeking to perform these operations solely using pure WebGL, without relying on the Three.js library

if( keyboard.pressed("up")) pobjects[movementControls.translate].translateX(1); if( keyboard.pressed("down")) pobjects[movementControls.translate].translateX(-1); if( keyboard.pressed("left")) pobjects[mo ...

The error message "Each item within a list must be assigned a unique 'key' prop" is being displayed

At the moment, I'm immersed in a project that utilizes React, Next.js, and Ant-Design. However, during the development process, I encountered an error due to the absence of a unique key like so: Here's the detailed log of the error: Warning: Ea ...

String representation of a failed test

While working on some coding exercises on codewars, I came across a simple one that requires creating a function called shortcut to eliminate all the lowercase vowels in a given string. Here are some examples: shortcut("codewars") // --> cdwrs shortcut ...

What is the method for obtaining the present date in C#?

What is the best way to retrieve the current date in C# without including the time? For instance: 19/03/2011 [Edit] I was able to find the solution on my own. Thanks for your help! :) DateTime currentDate = DateTime.Now; string formattedDate = current ...

JS: Best way to transfer selected information to the following page?

Within our SharePoint list, users often need to add very similar items. To streamline this process, I am exploring the possibility of adding a new column that includes a button or turning one of the existing columns into a clickable link. When clicked: ...

Steps for displaying a division on clicking a hyperlink

I am currently working on my main menu layout in HTML, and here is the code snippet: <div id="main-container"> <div id="main-wrapper"> <div id="logo"> <h1 id="title">port ...

Strengthening JavaScript Security

Throughout the past few years, I have delved into various javascript libraries like Raphael.js and D3, experimenting with animations sourced from different corners of the internet for my own learning. I've obtained code snippets from Git repositories ...

The schema does not accept fileLink as valid

I'm currently facing an issue with implementing Simple Schema in my Meteor React project. Despite my efforts, I can't seem to make it work as expected. Below is the schema I am using: Comments.schema = new SimpleSchema({ city: { type: S ...

Discover a way to retrieve the index of elements in Vue JS that are not within a v-for loop

I am interested in learning how to access the index of elements outside of a v-for loop <template> <div class="hero-text"> <h4>0{{ index + 1 }}/{{ homePageImageList.length }}</h4> </div> <VueSlickC ...