Count number of character before 2 # - sql

I have a problem with my query. I need to create a query to count all characters except of all characters between symbol #. For example I have :
#CN#Test
#DATE#hjkjhkjh#DATE#hjkh#SC#hkhkjhkjhkjhkj#SC#
I can have multiple # into an expression
I want to get value 4 not 8, so without #CN#.
I tried :
SELECT e.id as event_id,length(em.value) as num_count
FROM event_mt em
INNER JOIN event e ON e.id = em.id_event
WHERE e.del_date IS NULL
AND em.value not like '%[^0-9]%'
The regex is not correct.
Help me please.

Try this.
DECLARE #str VARCHAR(30)='#CN#Test'
SELECT len(LEFT(#str, Charindex('#', #str)-1) +
+ Reverse(LEFT(Reverse(#str), Charindex('#', Reverse(#str))-1)))

I resolve it with replaces nested :
SELECT e.id as event_id,em.id,
(LENGTH(REPLACE(REPLACE(REPLACE(REPLACE(em.value, 'dfd', ''),'ghgfh',''),'wer',''),'65756',''))) as message_count
FROM event

Related

LEN() & CHARINDEX() problem in formatting field

I have the following query:
UPDATE P
SET Street = TRIM(LEFT(FormattedAddress, CHARINDEX(',', FormattedAddress)-1)),
Town = TRIM(RIGHT(FormattedAddress, LEN(FormattedAddress)-CHARINDEX(',', FormattedAddress)))
FROM Person P
and it's suddenly started failing with the following error:
Msg 537, Level 16, State 2, Line 3
Invalid length parameter passed to the LEFT or SUBSTRING function.
The statement has been terminated.
How could I find the data that is causing this to fail and fix the update statement so it doesn't fail in the future?
How could I find the data that is causing this to fail and fix the
update statement so it doesn't fail in the future?
Try like this to find. You will get the rows which are breaking your query.
SELECT * FROM
Person P
WHERE CHARINDEX(',', FormattedAddress) <=0
Better way of writing same query as suggested by Jeroen Mostert is like following.
SELECT * FROM
Person P
WHERE FormattedAddress NOT LIKE '%,%'
You are getting the Error because there are values in FormattedAddress which do not have a "," and hence the CHARINDEX function will return the value as 0 for those. since for LEFT,RIGHT and SUBSTRING functions accept only positive integers greater than 0, These rows will throw an Error.
You can easily identify the rows that are causing the issue using the following query
SELECT
*
FROM Person P
WHERE CHARINDEX(',', FormattedAddress) = 0
There are multiple ways you can overcome the same
Approach# 1
Use Case inside the LEFT / RIGHT
UPDATE P
SET
Street = TRIM(LEFT(FormattedAddress,
CASE WHEN CHARINDEX(',', FormattedAddress)>1
THEN CHARINDEX(',', FormattedAddress)-1
ELSE LEN(FormattedAddress) END)
),
Town = TRIM(RIGHT(FormattedAddress,
CASE WHEN CHARINDEX(',', FormattedAddress)>0
THEN LEN(FormattedAddress)-CHARINDEX(',', FormattedAddress)
ELSE LEN(FormattedAddress) END)
)
FROM Person P
Approach# 2
Filter the records in the WHERE clause
UPDATE P
SET
Street = TRIM(LEFT(FormattedAddress, CHARINDEX(',', FormattedAddress)-1)),
Town = TRIM(RIGHT(FormattedAddress, LEN(FormattedAddress)-CHARINDEX(',', FormattedAddress)))
FROM Person P
WHERE CHARINDEX(',', FormattedAddress) > 1
You can use WHERE cease :
WHERE FormattedAddress LIKE '%,%'
However, you can also do :
UPDATE P
SET Street = TRIM(LEFT(FormattedAddress, CHARINDEX(',', FormattedAddress + ',')-1)),
Town = TRIM(RIGHT(FormattedAddress, LEN(FormattedAddress)-CHARINDEX(',', FormattedAddress)))
FROM Person P;

MS access SQL left function not returning anything

Hi im trying to create a query to only display those with the same 3 letters of a postcode, however, it returns nothing at all. This is my code:
SELECT Custtbl.Name, Custtbl.PostCode
FROM Custtbl
Where (((Custtbl.PostCode)=Left([PostCode],3)))
any help would be appreciated!
This is how:
SELECT DISTINCT
Custtbl.Name,
Custtbl.PostCode
FROM
Custtbl,
Custtbl As T
WHERE
Custtbl.Name <> T.Name
AND
Left(Custtbl.PostCode, 3) = Left(T.[PostCode], 3)
You are missing the Left function on Custtbl.PostCode. Your query should be
SELECT Custtbl.Name, Custtbl.PostCode
FROM Custtbl
Where ((LEFT(Custtbl.PostCode,3)=Left([PostCode],3)))
I think you're looking for the LIKE condition. TutorialPoint
SELECT Custtbl.Name, Custtbl.PostCode
FROM Custtbl
Where (Custtbl.PostCode LIKE Left([PostCode],3) + '%')
This will return any row where the first 3 characters in [PostCode] match the first 3 characters in Custtbl.PostCode

Replace LIKE by SUBSTR

I tried to select the name of students that end with 'a'. I wrote this code:
Select name form students where name like '%a' ;
How can I get the same results using SUBSTR?
I actually think using RIGHT() would make the most sense here:
SELECT name
FROM students
WHERE RIGHT(name, 1) = 'a'
The above query would work on MySQL, SQL Server, and Postgres, but not Oracle, where you would have to use SUBSTR():
SELECT name
FROM students
WHERE SUBSTR(name , -1) = 'a'
Not all platforms accept negative start integers or length integers for SUBSTR()
Can you try if your DBMS supports the RIGHT() string function?
Works like this:
SQL>SELECT RIGHT('abcd',1) AS rightmost_char;
rightmost_char
--------------
d
Happy playing ...
Marco
You can use :
Select name from students where SUBSTR(name, -1, 1) = 'a' ;
Using SUBSTR().
SELECT name
FROM students
WHERE SUBSTR(name , -1) = 'a'

SQL special group by on list of strings ending with *

I would like to perform a "special group by" on strings with SQL language, some ending with "*". I use postgresql.
I can not clearly formulate this problem, even if I have partially solved it, with select, union and nested queries which are not elegant.
For exemple :
1) INPUT : I have a list of strings :
thestrings
varchar(9)
--------------
1000
1000-0001
1000-0002
2000*
2000-0001
2000-0002
3000*
3000-00*
3000-0001
3000-0002
2) OUTPUT : That I would like my "special group by" return :
1000
1000-0001
1000-0002
2000*
3000*
Because 2000-0001 and 2000-0002 are include in 2000*,
and because 3000-00*, 3000-0001 and 3000-0002 are includes in 3000*
3) SQL query I do :
SELECT every strings ending with *
UNION
SELECT every string where the begining NOT IN (SELECT every string ending with *) <-- with multiple inelegant left functions and NOT IN subqueries
4) That what I'm doing return :
1000
1000-0001
1000-0002
2000*
3000*
3000-00* <-- the problem
The problem is : 3000-00* staying in my result.
So my question is :
How can I generalize my problem? to remove all string who have a same begining string in the list (ending with *) ?
I think of regular expressions, but how to pass a list from a select in a regex ?
Thanks for help.
Select only strings for which no master string exists in the table:
select str
from mytable
where not exists
(
select *
from mytable master
where master.str like '%*'
and master.str <> mytable.str
and rtrim(mytable.str, '*') like rtrim(master.str, '*') || '%'
);
Assuming that only one general pattern can match any given string, the following should do what you want:
select coalesce(tpat.thestring, t.thestring) as thestring
from t left join
t tpat
on t.thestring like replace(tpat.thestring, '*', '%') and
t.thestring <> tpat.thestring
group by coalesce(tpat.thestring, t.thestring);
However, that is not your case. However, you can adjust this with distinct on:
select distinct on (t.thestring) coalesce(tpat.thestring, t.thestring)
from t left join
t tpat
on t.thestring like replace(tpat.thestring, '*', '%') and
t.thestring <> tpat.thestring
order by t.thestring, length(tpat.thestring)

How to remove the first character if it is a specific character in SQL

I currently have a table Telephone it has entries like the following:
9073456789101
+773456789101
0773456789101
What I want to do is remove only the 9 from the start of all the entries that have a 9 there but leave the others as they are.
any help would be greatly appreciated.
While all other answer are probably also working, I'd suggest to try and use STUFF function to easily replace a part of the string.
UPDATE Telephone
SET number = STUFF(number,1,1,'')
WHERE number LIKE '9%'
SQLFiddle DEMO
Here is the code and a SQLFiddle
SELECT CASE
WHEN substring(telephone_number, 1, 1) <> '9'
THEN telephone_number
ELSE substring(telephone_number, 2, LEN(telephone_number))
END
FROM Telephone
Update Telephone set number = RIGHT(number,LEN(number)-1) WHERE number LIKE '9%';
I recently solved a similar problem with a combination of RIGHT(), LEN() & PATINDEX(). PATINDEX will return the integer 1 when it finds a 9 as the first character and 0 otherwise. This method allows all records to be returned at once without a CASE WHEN statement.
SELECT
RIGHT(number, LEN(number) - PATINDEX('9%', number))
FROM Telephone
UPDATE dbo.Telephone
SET column_name = SUBSTRING(column_name, 2, 255)
WHERE column_name LIKE '9%';
Stuff is a great function for this. However, using it with an update statement with a where clause is great, but what if I was doing an insert, and I needed all of the rows inserted in one pass. The below will remove the first character if it is a period, does not use the slower case statement, and converts nulls to an empty string.
DECLARE #Attachment varchar(6) = '.GIF',
#Attachment2 varchar(6)
SELECT
#Attachment2 = ISNULL(ISNULL(NULLIF(LEFT(#Attachment, 1), '.'), '') + STUFF(#Attachment, 1, 1, ''), '')
SELECT
#Attachment2
DECLARE #STR nvarchar(200) = 'TEST'
SET #STR = STUFF(#STR,1,1,'')
PRINT #STR
Result will be "EST"
You can use replace in select statement instead of where or update
SELECT REPLACE(REPLACE('_'+number,'_9',''),'_','') FROM #tbl