I am retreiving information from a BES (Blackberry Enterprise Server). I need to change the Display Name field from the BES to a name.name format.
The Display name comes across as First Last and possible First Last-EXT for example. I need to drop the dash after any last name with it, and also add a . between first and last.
I have tried using REPLACE to add the .
And I have tried CASE to remove the - and everthing after it. But how can I combine into one statement to do both?
,CASE WHEN CHARINDEX('-',[DisplayName])>0 THEN LEFT([DisplayName], CHARINDEX('-',[DisplayName])-1) ELSE [DisplayName] END
,REPLACE([DisplayName], ' ','.') AS 'Display Name'
edit : added code block
Try below code.
you have done all the work.
in your case statement you return the DisplayName that you want (removing '-' from lastname and every character following '-') so use that output and apply replace to replace your blank space to '.'
CASE WHEN CHARINDEX('-',[DisplayName])>0
THEN REPLACE(LEFT([DisplayName], CHARINDEX('-',[DisplayName])-1), ' ','.')
ELSE REPLACE([DisplayName], ' ','.')
END
Related
Thank you for your help.
How do I get data that contains a space at the front or the back?
For example: (see .png)
enter image description here
I tried:
select* from customer_table where customer like '% %'
but:
it gave me all of them because name contains a space in between.
I need to get the ones that have a white-space at the front.
thank you!
I think you want:
where customer like ' %' or customer like '% '
like patterns match the entire string. So the first gets spaces at the begging and the second spaces at the end. If you wanted both, you can do that in a single patter:
where customer like ' % '
Use TRIM function on the column to fit you select condition, for instance
WHERE LTRIM([CUSTOMER]) = 'Joe'
This example is for Microsoft SQL Server but I believe all DBMS have a similar function you can use trim as the keyword to search.
I am looking for some help in regards to removing trailing spaces from my queue names. The following is an example of a table that I am using:
QUEUE_NAME
Queue A
Queue B
Queue C
The problem I have is that there is an extra space at the end of the queue name and when trying the following code:
SELECT
TRIM(TRAILING ' ' FROM QUEUE_NAME)
FROM
TABLE_QUEUE;
the space is still there.
I was reading the searches from Google and came across the following code to remove special characters [https://community.oracle.com/blogs/bbrumm/2016/12/11/how-to-replace-special-characters-in-oracle-sql] and this removed all the spaces including the one at the end. The code I wrote:
SELECT
REGEXP_REPLACE(QUEUE_NAME, '[^0-9A-Za-z]', '')
FROM
TABLE_QUEUE;
Only issue I have now is that my result is shown as the following:
QUEUE_NAME
QueueA
QueueB
QueueC
I have never really used regexp_replace hence not sure what I need to change to the code to leave the spaces in between the queue names, so would really appreciate it if somebody could advise on how I could fix this.
Thanks in advance.
---- code edited as should not include [.!?]+
You may try to use trim only as in the following select statement :
with t(col0) as
(
select ' Queue A ' from dual union all
select ' Queue B ' from dual union all
select ' Queue C ' from dual
)
select trim(col0)
from t;
trimmedText
-----------
Queue A
Queue B
Queue C
you get no surrounding spaces around.
You want to remove space from the end of the string:
regexp_replace(queue_name, '[[:space:]]+$', '')
(The '$' in the pattern marks the end.)
If this still doesn't work, then you are dealing with some strange invisible character that is not considered space. Use
regexp_replace(queue_name, '[^0-9A-Za-z]+$', '')
instead, which, as you already know, removes all characters except for letters and digits. The '$' restricts this to the end of the string.
Columns of type CHAR (e.g. CHAR(8)) are always blank-padded on the right to the full width of the field. So if you store 'Queue A' in a CHAR(8) field the database helpfully adds a single space to the end of it - and there's no way to remove that extra space from the column. The solution is to change the field so it's defined as either VARCHAR2 (preferred in Oracle) or VARCHAR:
ALTER TABLE TABLE_QUEUE MODIFY QUEUE_NAME VARCHAR2(8);
Then the database will only store the characters you give it, without blank-padding the field.
Best of luck.
How would i go about constructing a query, that would return all material numbers that have a "blank white space" either BEFORE or AFTER the number string? We are exporting straight from SSMS to excel and we see the problem in the spreadsheet. If i could return all of the material numbers with spaces.. i could go in and edit them or do a replace to fix this issue prior to exporting! (the mtrl numbers are imported in via a windows application that users upload an excel template to. This template has all of this data and sometimes they place in spaces in or after the material number). The query we have used to work but now it does not return anything, but upon export we identify these problems you see highlighted in the screenshot (left screenshot) and then query to find that mtrl # in the table (right screenshot). And indeed, it has a space before the 1.
Currently the query we use looks like:
SELECT Mtrl
FROM dbo.Source
WHERE Mtrl LIKE '% %'
Since you are getting the data from a query, you should just have that query remove any potential spaces using LTRIM and RTRIM:
LTRIM(RTRIM([MTRL]))
Keep in mind that these two commands remove only spaces, not tabs or returns or other white-space characters.
Doing the above will make sure that the data for the entire set of data is fine, whether or not you find it and/or fix it.
Or, since you are copying-and-pasting from the Results Grid into Excel, you can just CONVERT the value to a number which will naturally remove any spaces:
SELECT CONVERT(INT, ' 12 ');
Returns:
12
So you would just use:
CONVERT(INT, [MRTL])
Now, if you want to find the data that has anything that is not a digit in it, you would use this:
SELECT Mtrl
FROM dbo.Source
WHERE [Mtrl] LIKE '%[^0-9]%'; -- any single non-digit character
If the issue is with non-space white-space characters, you can find out which ones they are via the following (to find them at the beginning instead of at the end, change the RIGHT to be LEFT):
;WITH cte AS
(
SELECT UNICODE(RIGHT([MTRL], 1)) AS [CharVal]
FROM dbo.Source
)
SELECT *
FROM cte
WHERE cte.[CharVal] NOT BETWEEN 48 AND 57 -- digits 0 - 9
AND cte.[CharVal] <> 32; -- space
And you can fix in one shot using the following, which removes regular spaces (char 32 via LTRIM/RTRIM), tabs (char 9), and non-breaking spaces (char 160):
UPDATE src
SET src.[Mtrl] = REPLACE(
REPLACE(
LTRIM(RTRIM(src.[Mtrl])),
CHAR(160),
''),
CHAR(9),
'')
FROM dbo.Source src
WHERE src.[Mtrl] LIKE '%[' -- find rows with any of the following characters
+ CHAR(9) -- tab
+ CHAR(32) -- space
+ CHAR(160) -- non-breaking space
+ ']%';
Here I used the same WHERE condition that you have since if there can't be any spaces then it doesn't matter if you check both ends or for any at all (and maybe it is faster to have a single LIKE instead of two).
I have a table of common words that are used in sentences (i.e. A, the, and, where, etc...)
What I want to do is loop through all those words and strip them out of the descriptions that people have entered to attempt to generate common keywords or tags. But I can't use replace because replace will remove any instance of the common word regardless of whether it is only a couple of letters that make up a larger word. For instance:
I want to replace A in the description. Now obviously a lot of words contain the letter a. So all those A's will be stripped from the words. I don't want that. I only want it when A is used a a whole word. I can figure this out using regular expressions but was wondering if there was anyway to do this in SQL without having to resort to CLR proc.
Maybe I am missing something but I couldn't seem to find an easy way to do this without having to write some specific scenarios like: word plus space before, word plus space after, word plus period after, etc... I don't think that is the best way.
For quick and dirty, I used to slosh through the various SQL functions PATINDEX, LEFT, RIGHT and LIKE to do this sort of thing. For one-time data prep, I export to something like Excel and eyeball it.
A good approach also is to create a new StringSubstitutionTable with two columns SOURCESTRING and TARGETSTRING and run a replace function to replace the SOURCESTRING with the TARGETSTRING on the joined table. This is cool because you can just add substitution entries as needed.
You can try nesting the replaces for each word you would like to replace. For example:
UPDATE TableName
SET ColumnName = REPLACE(REPLACE(REPLACE(REPLACE(TableName.ColumnName,' a ',' '),' the ',' '),' and ',' '), ' ', ' ')
Let me know if this is what you were looking for.
Here is they way I did something similar to what you are trying to do.
During your replace action...
Append a space before and after the common word.
Append a space before and after the description.
Let's suppose you want to remove the CommonWord "A" from the Description.
Description: "A good phrase never starts with A or ends with A"
CommonWord: "A"
Update TableName
Set Description =
LTRIM(RTRIM(Replace(' ' + Description + ' ', ' ' + CommonWord + ' ', ' ')))
This lets you delete all whole words that equal 'A'. Because you are replacing ' A ' with a space you need to LTRIM RTRIM to remove any leading or trailing spaces.
You could also do this in two steps:
--
-- Step 1 Loop through all common words removing them
--
Update TableName
Set Description = Replace(' ' + Description + ' ', ' ' + CommonWord + ' ', ' ')
--
-- Step 2 Unconditionally Trim all Descriptions
--
Update TableName
Set Description = LTRIM(RTIM(Description))
Simply put, I have a select that will return multiple single characters, and thus won't work. Is there any way to bunch all the single characters into a single returnable string?
My current slow and ugly solution:
,'('+(Select Left(max(AE_D1),1)
FROM ACCESS_EVENTS
WHERE LEFT(AE_D1,1) like 'W'
AND replace(HR.firstname,' ','')+' '+replace(HR.lastname, ' ','') LIKE Left(AE_D2,len(replace(HR.firstname,' ','')+' '+replace(HR.lastname, ' ','')))) +')'
+'('+(Select Left(max(AE_D1),1)
FROM ACCESS_EVENTS
WHERE LEFT(AE_D1,1) like 'M'
AND replace(HR.firstname,' ','')+' '+replace(HR.lastname, ' ','') LIKE Left(AE_D2,len(replace(HR.firstname,' ','')+' '+replace(HR.lastname, ' ','')))) +')'
. . . Repeat until all cases are covered . . .
EDIT - Extra information: Each 'W' or 'M' is the first character of an access event's name, related to where the event took place. There are about 9 different event characters to cover. The point of the query is to show when a person has events from more than one area associated to their name. The problem now is just to display it cleanly.
I think you want to read this.
Could an indexed view work for your desired data?
http://technet.microsoft.com/en-us/library/cc917715.aspx#XSLTsection124121120120