Remove string in SQL Server - sql

In my table Products in SQL Server, I have a column UrlLink with values that look like this:
Id UrlLink
-----------------------------------
1 domain/product1.html?7
2 domain/product2.html?34
3 domain/product294.html?6576
4 domain/product54.html?765
How to remove parameter
?7, ?34, ?6576, ?765
from column UrlLink?
Thanks!

To remove the query string part from the UrlLink column in the table, you need to use Left and CharIndex in your UPDATE statement.
UPDATE Products
SET UrlLink = LEFT(UrlLink, CHARINDEX('?',UrlLink)-1)

Using left and charindex should work:
select left(UrlLink, charindex('?',UrlLink)-1) from Products;
This would return everything before the first occurrence of a ?. You might want to add some null checks if parameter isn't mandatory in the UrlLink column.

Try with this code:
DECLARE #myvar varchar(100);
SET #myvar = 'domain/product1.html?7';
SELECT REVERSE(SUBSTRING((REVERSE(#myvar)), (CHARINDEX('?',REVERSE(#myvar))+1),500)) AS result ;
GO

Simple and won't fail in case you have a Urllink that does not contain?.
In case of multiple ? removes everything from the first ? since in URL this is the only ? with special significance.
select id,left(Urllink,charindex('?',Urllink+'?')-1)
from Products

You just have to find ? in your url and take string upto it. You can use LEFT/SUBSTRING for getting substring and CHARINDEX for finding ? in your string. Check out my query below
select id, substring(urllink,1,charindex('?',urllink)-1)
from products

In case multiple ? symbols are there in the UrlLink column values and if you want to take the value after the last ? symbol. Then use a combination of SUBSTRING and CHARINDEX .
Query
SELECT
[Id],
SUBSTRING([UrlLink], 1,
LEN([UrlLink]) - CHARINDEX('?', REVERSE([UrlLink]), 1)) AS [UrlLink]
FROM [Products];
Demo

Related

How to replace the first letter of a string using SQL and Postgres?

I have a database column of varchar(191) with strings in the database. We need to replace the first letter of every string with an "E". So for instance, we have:
Cuohvi-AQNqalPq8zdr1cOA
Needs to be changed to
Euohvi-AQNqalPq8zdr1cOA
Do you know how we can achieve this in Postgres with a SQL query? It needs to be updated for the whole table.
Per docs use overlay():
UPDATE the_table SET the_field = overlay(the_field placing 'E' from 1 for 1);
Use a combination of the CONCAT function and the RIGHT function with an argument of -1.
SELECT CONCAT('E', RIGHT('Cuohvi-AQNqalPq8zdr1cOA', - 1))
FROM yourtable
SELECT CONCAT('E', RIGHT(yourfield, - 1))
FROM yourtable
dbfiddle: https://dbfiddle.uk/?rdbms=postgres_9.6&fiddle=e198b05f02283137afc39c24bb6c788d

SQL full text search behavior on numeric values

I have a table with about 200 million records. One of the columns is defined as varchar(100) and it's included in a full text index. Most of the values are numeric. Only few are not numeric.
The problem is that it's not working well. For example if a row contains the value '123456789' and i look for '567', it's not returning this row. It will only return rows where the value is exactly '567'.
What am I doing wrong?
sql server 2012.
Thanks.
Full text search doesn't support leading wildcards
In my setup, these return the same
SELECT *
FROM [dbo].[somelogtable]
where CONTAINS (logmessage, N'28400')
SELECT *
FROM [dbo].[somelogtable]
where CONTAINS (logmessage, N'"2840*"')
This gives zero rows
SELECT *
FROM [dbo].[somelogtable]
where CONTAINS (logmessage, N'"*840*"')
You'll have to use LIKE or some fancy trigram approach
The problem is probably that you are using a wrong tool since Full-text queries perform linguistic searches and it seems like you want to use simple "like" condition.
If you want to get a solution to your needs then you can post DDL+DML+'desired result'
You can do this:
....your_query.... LIKE '567%' ;
This will return all the rows that have a number 567 in the beginning, end or in between somewhere.
99% You're missing % after and before the string you search in the LIKE clause.
es:
SELECT * FROM t WHERE att LIKE '66'
is the same as as using WHERE att = '66'
if you write:
SELECT * FROM t WHERE att LIKE '%66%'
will return you all the lines containing 2 'sixes' one after other

MS Access Expression to Find Records Where the Value to the Left of a Comma is Greater than Zero

I have some records in a column that contain IDs and some of these records contain multiple IDs separated by commas. Additionally there are some records where I have ",3" and ",2" when they should simply be "3" and "2". I do not have write privileges in this DB so updating those records is not an option.
I am trying to write a query that returns records that have a comma where the value to the left of any comma in the record is greater than 0 e.g. "2,3", "2,3,12" etc but NOT ",3" or ",2".
What would this expression look like in MS Access?
Thanks in advance.
If you want to remove the starting comma from the records when you return them, you can do so using a simple query:
SELECT IIF(MyField LIKE ",*", Right(MyField, Len(MyField)-1), MyField)
FROM MyTable
To answer your original question, you could simply use Val:
SELECT * FROM YourTable WHERE Val([YourField]) > 0
I would simply use:
select t.*
from t
where val not like ",*";
This doesn't handle the 0 part, but you don't give any examples in your answer. Perhaps this answers that part:
select t.*
from t
where val not like ",*" and val not like "*0,*";

Using a GUID In The Where Clause

For some reason I'm unable to use comparisons on GUID columns, it does not return any results.
See below, with the WHERE clause set to the exact value of the 'secguid' column, it does not return any results. What's going on?
SELECT * FROM dbMobileFile
SELECT * FROM dbMobileFile WHERE secguid = '3137459D-EFDE-449E-94A3-89345A8580FA'
SELECT * FROM dbMobileFile WHERE secguid LIKE '3137459D-EFDE-449E-94A3-89345A8580FA'
Using LIKE does not work either.
Try this
SELECT [fileID],
[fileCOde],
[filePassword],
[fileDescription],
[rowguid],
[secguid]
FROM [dbo].[dbMobileFile]
WHERE CAST(secguid as uniqueidentifier) = CAST('3137459D-EFDE-449E-94A3-89345A8580FA' as uniqueidentifier)
Since you mention that the column is stored as NVARCHAR, its possible that the string has leading or trailing whitespaces, which is why it might not be popping up in the query with the WHERE clause.
You can try this :
SELECT [fileID],
[fileCOde],
[filePassword],
[fileDescription],
[rowguid],
[secguid]
FROM [dbo].[dbMobileFile]
WHERE LTRIM(RTRIM(secguid)) = '3137459D-EFDE-449E-94A3-89345A8580FA'
which should show you the result as leading and trailing whitespaces are eliminated in the WHERE clause.
Also, in case you want to make use of the LIKE operator, you can write your query as :
SELECT [fileID],
[fileCOde],
[filePassword],
[fileDescription],
[rowguid],
[secguid]
FROM [dbo].[dbMobileFile]
WHERE secguid LIKE '%3137459D-EFDE-449E-94A3-89345A8580FA%'
Hope this helps!!!
I've had this problem with a corrupt database. Some GUIDs be contained in the WHERE clause, with other GUIDS on the same table would not return results.
Turns out that database had Index issues. Run DBCC to make sure your database isn't corrupt.
The accepted answer works, but it is a bit verbose and probably not the intended way to do this. UniqueIdentifier is qualified by {}, so the following is the easiest;
SELECT * FROM dbMobileFile WHERE secguid = '{3137459D-EFDE-449E-94A3-89345A8580FA}'
See inside the database that guid value stored as 32 hex digits:00000000000000000000000000000000 so if we search by 32 hex digits separated by hyphens: 00000000-0000-0000-0000-000000000000, it's not get any output
Try this:
SELECT * FROM dbMobileFile WHERE secguid = ('3137459D-EFDE-449E-94A3-89345A8580FA')
Use parentheses to enclose GUID string LIKE ('GUID')

How to substring records with variable length

I have a table which has a column with doc locations, such as AA/BB/CC/EE
I am trying to get only one of these parts, lets say just the CC part (which has variable length). Until now I've tried as follows:
SELECT RIGHT(doclocation,CHARINDEX('/',REVERSE(doclocation),0)-1)
FROM Table
WHERE doclocation LIKE '%CC %'
But I'm not getting the expected result
Use PARSENAME function like this,
DECLARE #s VARCHAR(100) = 'AA/BB/CC/EE'
SELECT PARSENAME(replace(#s, '/', '.'), 2)
This is painful to do in SQL Server. One method is a series of string operations. I find this simplest using outer apply (unless I need subqueries for a different reason):
select *
from t outer apply
(select stuff(t.doclocation, 1, patindex('%/%/%', t.doclocation), '') as doclocation2) t2 outer apply
(select left(tt.doclocation2), charindex('/', tt.doclocation2) as cc
) t3;
The PARSENAME function is used to get the specified part of an object name, and should not used for this purpose, as it will only parse strings with max 4 objects (see SQL Server PARSENAME documentation at MSDN)
SQL Server 2016 has a new function STRING_SPLIT, but if you don't use SQL Server 2016 you have to fallback on the solutions described here: How do I split a string so I can access item x?
The question is not clear I guess. Can you please specify which value you need? If you need the values after CC, then you can do the CHARINDEX on "CC". Also the query does not seem correct as the string you provided is "AA/BB/CC/EE" which does not have a space between it, but in the query you are searching for space WHERE doclocation LIKE '%CC %'
SELECT SUBSTRING(doclocation,CHARINDEX('CC',doclocation)+2,LEN(doclocation))
FROM Table
WHERE doclocation LIKE '%CC %'