SQL Server select looks like 'Like' - sql-server

I have question about SQL Server's LIKE operator.
I have a table Table1:
app ex
----------
test 210
I am writing some select from program select is looks like this:
select app
from Table1
where ex Like = '210203'
I have tried to use (%,%.%,[]).
If I try [] this one like this [210]203 it is working but there will be more data so if there will be 2102 in ex I want this to choose this one.
But it isn't selecting nothing beacouse ver is 210203 and in ex is 210
How can I manage this that I selected 210 the variable can't be changed there will be always variable bigger than 'ex' data
Please help me anyone.

You query is not right, you don't use "=" operator in Like.
SELECT app FROM Table1 WHERE ex LIKE '%210%'
In this case will filter values who have 210 inside.
If you use for ex: Like ='%210' will filter values who ends with 210.. and so on.
Update
You can use this too, i think will help you
declare #Value varchar(50)
set #value = '123AAAAA123'
select * from Table where #Value like '%' + column + '%'

Related

Get the specified input parameter from stored procedure

I want to get all the related information from database. For example I have a summary column which contains data like "A01.1 | Foodland Inbound QIP Details and Pre Unloading" and I want to pass only "A01" in input parameter and get all the data related to "A01"
select * from TableName where summary_column like 'A01%';
Based on the code in your comment, the where clause will only find a summary that is the equal to A01.2 e.g:
TaskMaster.Summary =#summary
You want to use LIKE and a wild card:
WHERE TaskMaster.Summary LIKE '''' + #summary + '%' + ''''
The above will find everything that starts with A01.2, so if you want to find all with A01, pass #summary as A01

Sql Server - How do I get JSON nested value in my SQL Select statement

Environment: SQL Server 2014 and above
How do I access the email value in my JSON value with my SELECT statement?
select JSON_VALUE('[{"data":{"email":"test#email.com"}}]', '$.email') as test
Json support was only introduced in SQL Server 2016 - so with any prior version you would need to either use string manipulation code or simply parse the json outside of SQL Server (maybe using a CLR function)
For 2016 version or higher, you can use JSON_VALUE like this:
declare #json as varchar(100) = '[{"data":{"email":"test#email.com"}}]';
select JSON_VALUE(#json, '$[0].data.email') as test
For older versions - you might be able to get away with this, but if your json value does not contain an email property, you will get unexpected results:
select substring(string, start, charindex('"', string, start+1) - start) as test
from (
select #json as string, charindex('"email":"', #json) + 9 as start
) s
You can see a live demo on db<>fiddle
Another way. PatternSplitCM is great for stuff like this.
Extract a single Email value:
DECLARE #json as varchar(200) = '[{"data":{"email":"test#email.com"}}]';
SELECT f.Item
FROM dbo.patternsplitCM(#json,'[a-z0-9#.]') AS f
WHERE f.item LIKE '%[a-z]%#%.%[a-z]%'; -- Simple Email Check Pattern
Extracting all Email Addresses (if/when there are more):
DECLARE #json VARCHAR(200) = '[{"data":{"email":"test#email.com"},{"email2":"test2#email.net"}},{"data":{"MoreEmail":"test3#email.555whatever"}}]';
SELECT f.Item
FROM dbo.patternsplitCM(#json,'[a-z0-9#.]') AS f
WHERE f.item LIKE '%[a-z]%#%.%[a-z]%'; -- Simple Email Check Pattern
Returns:
Item
--------------------------
test#email.com
test2#email.net
test3#email.555whatever
Or... the get only the first Email address that appears:
SELECT TOP (1) f.Item
FROM dbo.patternsplitCM(#json,'[a-z0-9#.]') AS f
WHERE f.item LIKE '%[a-z]%#%.%[a-z]%' -- Simple Email Check Pattern
ORDER BY ROW_NUMBER() OVER (ORDER BY f.ItemNumber)
Nasty fast, super-simple. No cursors, loops or other bad stuff.
With v2014 there is no JSON support, but - if your real JSON is that simple - it is sometimes a good idea to use some replacements in order to transform the JSON to XML like here, which allows for the native XML methods:
DECLARE #YourJSON NVARCHAR(MAX)=N'[{"data":{"email":"test#email.com"}}]';
SELECT CAST(REPLACE(REPLACE(REPLACE(REPLACE(#YourJSON,'[{"','<'),'":{"',' '),'":"','="'),'}}]',' />') AS XML).value('(/data/#email)[1]','nvarchar(max)');
It can be done in two ways:
First, if your JSON data is between [ ] like in your question:
select JSON_VALUE('[{"data":{"email":"test#email.com"}}]','$[0].data.email' ) as test
And if your JSON data is not between [ ]:
select JSON_VALUE('{"data":{"email":"test#email.com"}}','$.data.email' ) as test
You can teste the code above here
Your query should be like this (SQL Server 2016):
DECLARE #json_string NVARCHAR(MAX) = 'your_json_value'
SELECT [key],value
FROM OPENJSON(#json_string, '$.email'))
UPDATE :
select JSON_VALUE(#json_string, '$[0].data.email') as test

SQL select statement where the text I am looking for has single quotes

I am trying to do a select statement in Microsoft SQL server management studio. The data looks like this:
My select statement is:
SELECT * FROM OSUSR_W7I_INVENTORYCHANGELOG
WHERE OBJECTTYPEID = 3
and CREATEDBY is null
and TEXT = 'State changed from ''Awaiting Prep'' to ''Import Error'''
But this brings back no results, despite there being lots of records with this text.
I was able to retrieve some data by using this statement:
SELECT * FROM OSUSR_W7I_INVENTORYCHANGELOG
WHERE OBJECTTYPEID = 3
and CREATEDBY is null
and TEXT like '%Awaiting Prep%'
and TEXT like '%Import Error%'
but unfortunately it brings back too many results because it is bringing back data that states: 'State changed from 'Import Error' to 'Awaiting Prep''
where I am only looking for 'State changed from 'Awaiting Prep' to 'Import Error''
Please can anyone help. From reading other posts putting a double quote in should solve the issue of the single quotes but in this instance it doesn't work.
Many thanks
You can combine your LIKE to one using % as placeholders, instead of:
SELECT * FROM OSUSR_W7I_INVENTORYCHANGELOG
WHERE OBJECTTYPEID = 3
and CREATEDBY is null
and TEXT like '%Awaiting Prep%'
and TEXT like '%Import Error%'
do:
SELECT * FROM OSUSR_W7I_INVENTORYCHANGELOG
WHERE OBJECTTYPEID = 3
and CREATEDBY is null
and TEXT like '%Awaiting Prep%Import Error%'
You can try using LTRIM and RTRIM in sql server. If there is case of some white space. Otherwise your statement is seems ok
SELECT * FROM OSUSR_W7I_INVENTORYCHANGELOG
WHERE OBJECTTYPEID = 3
and CREATEDBY is null
and LTRIM(RTRIM(TEXT)) = 'State changed from ''Awaiting Prep'' to ''Import
Error'''

SQL Server - remove HTML Tag from a varchar

I know there are a few about similar topics on here but I can't find one related to my issue, this is it:
I have a table with an ID Column and a QRCode column. each time an item is added the primary key auto increments. The QRCode will scan in to be like the following:
"http://somewebsite.com/12345/987654321"
i want to be able to remove the "http://somewebsite.com/" from the string, I know how to do this in C# however I am unsure of how to do this in Sql Server. any guidance would be great, thanks
Regular formats are like the following, and used in the example below.
"http://somewebsite.com/12345/456564654"
"http://somewebsite.com/12345/989886765"
"http://somewebsite.com/12346/987654321"
the query returns the following results:
SELECT
REPLACE
(
REPLACE(QRCode, 'http://somewebsite.com/', '')
,'/', ' '
) AS QRCode
FROM
QRTable
WHERE
QRCode LIKE '%http://somewebsite.com/%'
"12345 456564654"
"12345 989886765"
"12346 987654321"
Now i need to update the table with those new results however as there's 3 results, i get the error message "Subquery returned more than 1 value". is there a way to replace the selected values in the table with the ones that exist based on the primary key field?
**Removed previous example
A more complete answer based on your updated question. This removes the first portion of the URL as well as the trailing / so that you get your desired output.
DECLARE #Variable VARCHAR(50)
SET #Variable = 'http://somewebsite.com/12345/456564654'
SET #Variable =
REPLACE
(
REPLACE(#Variable, 'http://somewebsite.com/', '')
,'/', ' '
)
PRINT #Variable
Output = 12345 456564654
Looking at your SQL statement you want this:
SELECT
REPLACE
(
REPLACE(QRCode, 'http://somewebsite.com/', '')
,'/', ' '
) AS QRCode
FROM
QRTable
WHERE
QRCode LIKE '%http://somewebsite.com/%'

How to remove a full stop from the end of data

I need to update my table because a number of fields in a column have got a fullstop on the end, i need to remove this.
So, in TableA, Field1: the data looks like
1002243.
1007053.
1007403.
1104098.
1110010.
NOTE: Not all the fields are the same length.
I need to remove the full stops.
Im using SQL Server 2005, cheers :)
UPDATE TableA
SET Field1 = LEFT(Field1 ,LEN(Field1)-1)
WHERE Field1 LIKE '%.'
Then you have data after the period. Try this to see:
Print '[' + Field1 + ']' and see if you get something like this:
[101. ]
There is a space after the period.

Resources