I am working on a dtb makros model with SQL. I have a table with contacts and someone wrote Dr. in the firstname-colum, e.g. Dr. Anna. I only want the firstname without the Dr., so I just want the name Anna in the firstname column.
I have tried to get rid of it with
regexp_replace(trim(firstname), '\bDr.\b', '')
but it is not working. On the other hand, when I tried to get rid of Pfarrer (which is the German expression for priest) Anna, it worked.
I used
regexp_replace(trim(firstname), '\bPfarrer\b', '')
for this. I only got Anna back, as I wanted.
Why does it work with Pfarrer and not with Dr.?
Generally speaking, you might want to remove all titles, including Mr., Mrs., Dr., and anything else following this pattern:
SELECT REGEXP_REPLACE(TRIM(firstname), '^\w+\.[ ]*', '') AS firstname
FROM yourTable;
Related
I was reading the article at mssqltips and wanted to try the caret in regex. I understand regex pretty well and use it often, although not much in SQl Server queries.
For the following list of names, I had thought that 1) select * from people where name like '%[^m]%;' will return those names that do not contain 'm'. But it doesn't work like that. I know I can do 2) select * from people where name not like '%m%'; to get the result I want, but I'm just baffled why 1) doesn't work as expected.
Amy
Jasper
Jim
Kathleen
Marco
Mike
Mitchell
I am using SQL Server 2017, but here is a fiddle:
sql fiddle
'%[^m]%' would be true for any string containing a character that is not m. An expanded version would be '%[Any character not m]%'. Since all of those strings contain a character other than m, they are valid results.
If you had a string like mmm, where name like '%[^m]%' would not return that row.
I have a column that contains a persons name and I need to extract it to pass to another system but I need to remove the spaces but only from between the initials
for example I might have
Mr A B Bloggs and I want Mr AB Bloggs or
Mrs A B C Bloggs and I want Mrs ABC Bloggs
As there are millions of records in the table I wont know how many initials there are or indeed if there are any initials. All I know is the prefix (Mr, Mrs etc) will be more than 1 character and so will the surname. I've tried using trim, replace, charindex but obviously not in the right combination. Any help would be appreciated.
Unfortunately SQL server does not support regex. You have two options:
Use .Net in CLR to perform the transformation. This link explains how to implement regex in SQL server using CLR: https://www.simple-talk.com/sql/t-sql-programming/clr-assembly-regex-functions-for-sql-server-by-example/.
Other option is to use a cursor to iterate through all the reocords and transform each entry. This may be slow for a large table. For example, you could write a function that returns location of spaces surrounded by single letters and then remove them. The trick is not to remove them until you have recorded all of them, and then remove them from right to left to avoid the location changing.
Try this:
declare #test varchar(100)='Mrs A B C Bloggs'
select (substring (#test,0,charindex(' ',#test)))+' '+
replace(replace(replace(substring(#test,len((substring (#test,0,charindex(' ',#test))))+1,len(#test)),
(substring (#test,0,charindex(' ',#test))),''),reverse((substring (reverse(#test),0,charindex(' ',reverse(#test))))),''),' ','')
+' '+reverse((substring (reverse(#test),0,charindex(' ',reverse(#test)))))
i've got a problem with transferring "real-World" data into my schema.
It's actually a "project" for my Database course and they gave us ab table with dog race results. This Table has a column which contains the name of the Dog (which itself consists of the actuall name and the name of the breeder) and informations about the Birthcountry, actual living Country and the birth year.
Example filed are "Lillycette [AU 2012]" or "Black Bear Lee [AU/AU 2013]" or "Lemon Ralph [IE/UK 1998]".
I've managed it to get out the first word and save it in the right column with split_part like this:
INSERT INTO tblHund (rufname)
SELECT
split_part(name, ' ', 1) AS rufname,
FROM tblimport;
tblimport is a table where I dumped the data from the csv file.
That works just as it should.
Accessing the second part of the Name with this fails because sometimes there isn't a second part and sometimes times there second part consists of two words.
And this is the where iam stuck right now.
I tried it with substring and regular expressions:
INSERT INTO tblZwinger (Name)
SELECT
substring(vatertier from E'[^ ]*\\ ( +)$')AS Name
FROM tblimport
WHERE substring(vatertier from E'[^ ]*\\ ( +)$') != '';
The above code is executed without errors but actually does nothing because the SELECT statement just give empty strings back.
It took me more then 3h to understand a bit of this regular Expressions but I still feel pretty stupid when I look at them.
Is there any other way of doing this. If so just give me a hint.
If not what is wrong with my expression above?
Thanks for your help.
You need to use atom ., which matches any single character inside capturing group:
E'[^ ]*\\ (.+)$'
SELECT
tblimport.*,
ti.parts[1] as f1,
ti.parts[2] as f2, -- It should be the "middle part"
ti.parts[3] as f3
FROM
tblimport,
regexp_matches(tblimport.vatertier, '([^\s]+)\s*(.*)\s+\[(.*)\]') as ti(parts)
WHERE
nullif(ti.parts[2], '') is not null
Something like above.
I'm trying to craft a query which rejects a row when some field is all the same characters. Ie. I want to select people named Smith but not people named aaaaaa or bbbb.
I can't use regexes, as Firebird's SIMILAR TO doesn't have backreferences.
How would you do it?
Meh, this is not what I wanted, but this will do. It works on aaaaaa, but wouldn't on abbbbbb.
SELECT *
FROM PEOPLE
WHERE replace(upper(NAME), substring(upper(NAME) FROM 1 FOR 1), '') = ''
I have column store_name (varchar). In that column I have entries like prime sport, best buy... with a space. But when user typed concatenated string like primesport without space I need to show result prime sport. how can I achieve this? Please help me
SELECT *
FROM TABLE
WHERE replace(store_name, ' ', '') LIKE '%'+#SEARCH+'%' OR STORE_NAME LIKE '%'+#SEARCH +'%'
Well, I don't have much idea, and even I am searching for it. But may be what I know works for you, You can achieve this by performing different type of string operations:
Mike can be Myke or Myce or Mikke or so on.
Cat an be Kat or katt or catt or so on.
For this you should write a function to generate number of possible strings and then form a SQL Query using all these, and query the database.
A similar kind of search in known as Soundex Search from Oracle and Soundex Search from Microsoft. Have a look of it. this may work.
And overall make use of functions like upper and lower.
Have you tried using replace()
You can replace the white space in the query then use like
SELECT * FROM table WHERE replace(store_name, ' ', '') LIKE '%primesport%'
It will work for entries like 'prime soft' querying with 'primesoft'
Or you can use regex.