Trim Spaces in a String - sql-server

UserId should be like 'keerthi.ks' in the database.
But in some tables some data is like 'keerthi .ks'
Need to clear the middle spaces.
Please help me with this.

You simply need to replace the space by nothing.
REPLACE ( string_expression , string_pattern , string_replacement )
More info here:
https://msdn.microsoft.com/en-us/library/ms186862.aspx

Try this
SELECT REPLACE('keerthi .ks',' ','')
Note, this will remove all the space in the text

Simply use
SELECT REPLACE('keerthi .ks', ' ', '')
You can also use column name instead of hard coded value.

Related

How do I remove specific words from a Netezza string?

I want to be able to remove specific words from my EARNINGS_CODE column. The words I need to remove are "Earnings" and "Results". I tried using the translate function but that just replaced every letter, not the exact word.
SELECT TRANSLATE(EARNINGS_CODE,'EARNINGS','')
FROM PROD_SRC..UWH_OCS_HCM_PAYROLL_COSTING_BIWEEKLY
Try
select
regexp_replace(earnings_code, 'EARNINGS', '')
from ....
For more things to replace, just add to the regex
select
regexp_replace(earnings_code, 'EARNINGS|foo|bar|foobar', '')
from ....

TSQL - Split delimited String into columns

The Problem
I have a number of filename strings that I want to parse into columns using a tilda as delimiter. The strings take on the static format:
Filepath example C:\My Documents\PDF
Surname example Walker
First Name example Thomas
Birth Date example 19991226
Document Created Datetime example 20180416150322
Document Extension example .pdf
So a full concatenated example would be something like:
C:\My Documents\PDF\Walker~Thomas~19991226~20180416150322.pdf
I want to ignore the file path and extension given in the string and only parse the following values into columns:
Surname, First Name, Birth Date, Document Created Datetime
So something like:
SELECT Surname = --delimitedString[0]
FirstName = --delimitedString[1]
--etc.
What I have tried
I know that I have several tasks I would need to perform in order to split the string, first I would need to trim off the extension and file path so that I can return a string delimited by tildas (~).
This is problem one for me, however problem 2 is splitting the new delimted string itself i.e.
Walker~Thomas~19991226~20180416150322
Ive had a good read through this very comprehensive question and It seems (as im using SQL Server 2008R2) the only options are to use either a function with loops or recursive CTE's or attempt a very messy attempt using SUBSTRING() with charIndex().
Im aware that If I had access to SQL Server 2016 I could use string_split but unfortunately I cant upgrade.
I do have access to SSIS but im very new to it so decided to attempt the bulk of the work within a SQL statement
Here is a way without a splitter that shouldn't be too complicated...
declare #var table (filepath varchar(256))
insert into #var values
('C:\My Documents\PDF\Walker~Thomas~19991226~20180416150322.pdf')
;with string as(
select
x = right(filepath,charindex('\',reverse(filepath))-1)
from #var
)
select
SurName= substring(x,1,charindex('~',x) - 1)
,FirstName = substring(x,charindex('~',x) + 1,charindex('~',x) - 1)
from string
I know you mentioned wanting to avoid the charindex() option if at all possible, but I worked it out in a hopefully semi-readable way. I find it somewhat easy to read complex functions like this when I space each parameter on a different line and use indent levels. It's not the most proper looking, but it helps with legibility:
with string as (select 'C:\My Documents\PDF\Walker~Thomas~19991226~20180416150322.pdf' as filepath)
select
substring(
filepath,
len(filepath)-charindex('\',reverse(filepath))+2, --start location, after last '\'
len(filepath)- --length of path
(len(filepath)-charindex('\',reverse(filepath))+2)- --less characters up to last '\'
(len(filepath)-charindex('.',filepath)) --less file extention
)
from string
Fritz already have a great start, my answer just add on top it
with string as (select 'C:\My Documents\PDF\Walker~Thomas~19991226~20180416150322.pdf' as filepath)
, newstr as (
select
REPLACE(substring(
filepath,
len(filepath)-charindex('\',reverse(filepath))+2, --start location, after last '\'
len(filepath)- --length of path
(len(filepath)-charindex('\',reverse(filepath))+2)- --less characters up to last '\'
(len(filepath)-charindex('.',filepath)) --less file extention
) , '~', '.') as new_part
from string
)
SELECT
PARSENAME(new_part,4) as Surname,
PARSENAME(new_part,3) as [First Name],
PARSENAME(new_part,2) as [Birth Date],
PARSENAME(new_part,1) as [Document Created Datetime]
FROM newstr

Concat the values in a string with SQL Server

I want to select a list of items and part numbers for for each item as a string:
SELECT top 100 *
FROM ii
OUTER APPLY
(SELECT def, ( ipr.part_number + ',') as prt
FROM ipr
WHERE ii.item_id = ipr.item_id
FOR XML PATH('') ) PN
The error is:
[Error Code: 8155, SQL State: S0002] No column name was specified for
column 1 of 'PN'.
How can I fix this?
I think that your whole OUTER APPLY statement generates one XML for both default_part_number and concatenated string, which(the whole XML) doesn't have a name.
What you could try to do would be adding alias like this AS PN(TestThis).
However, I don't think that you're expecting result you're going to get. It would better if you'd give us some example data and expected output. It will be easier for us to solve your problem in that case.
The combination of XML and STUFF is funny but perfectly fitting to your needs.
First you concat your strings with the ', ' in front, then you must return your XML with ", TPYE). You must read the result with ".value()" and use STUFF to replace the first ', '.
You'll find a lot of exampels in the net...

PL/SQL procedure: UPDATE uppercase names to initcaps, with special handling for some values

I need a procedure to do something like this: original name: AMSTERDAM new name: Amsterdam.
I made this procedure for it:
create or replace PROCEDURE NaamRoutine
is
BEGIN
update Gemeentenew
set gemeentenaam = lower(gemeentenaam);
update Gemeentenew
set gemeentenaam = initcap(gemeentenaam);
END;
The problem is there are a couple of names that start like 'S GRAVENHAGE and need to be 's Gravenhage.
Assuming the special handling is necessary only for names like 'S..., adding a simple REPLACE should work. BTW, you don't need two separate UPDATE statements - INITCAP will automatically convert non-initial characters to lowercase.:
with v_data(name) as (
select 'AMSTERDAM' from dual union all
select '''S GRAVENHAGE' from dual union all
select 'DEN HAAG' from dual union all
select 'IJSLAND' from dual
)
select
replace(nls_initcap(name, 'NLS_SORT=xDutch'), '''S', '''s') name
from v_data
Name
----
Amsterdam
's Gravenhage
Den Haag
IJsland
This will replace all occurrences of 'S with 's. If you need to handle other cases as well, I suggest you try REGEXP_REPLACE().
The function NLS_INITCAP helps with some globalization issues. For example, it capitalizes both the I and the J in IJSLAND. But it doesn't help with 'S names. I'm not sure if that is a bug with Oracle's globalization functions or if those city names are all exceptions.
In the first place, if you want to manipulate the same field twice, nest them rather than perform them separately.
select initcap( lower( '''S GRAVENHAGE' )) as Resp from dual;
However, you don't even need to do that in this case. initcap will give you what you want all by itself.
Now, if you have something that starts with an 's and a space ('s xxx), then you can then run it through a regex replacement.
select regexp_replace( initcap( '''S GRAVENHAGE' ), '^''S ', '''s ' ) as Resp from dual;

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