my table have column contain data like this.
SQL 2008
97W
125/ 122Q
121/ 118Q
121/ 118S
123/ 120S
112H
111H
i am trying to remove data before / so output will look like as
97W
122Q
118Q
118S
120S
112H
111H
can anyone share experience how can i achieve if came across such scenario.
Thanks,
Try this:
SELECT LTRIM(RIGHT(mycol, LEN(mycol) - CHARINDEX('/', mycol)))
FROM mytable
Demo here
Look at this one
Select LTRIM(SUBSTRING(col, CHARINDEX('/', col) + 1, LEN(col)))
from table
Use a CASE expression to check whether the string contains /, if yes use CHARINDEX to find the index of / and get the right part. Else the string as it is.
Query
SELECT
CASE WHEN your_column_name LIKE '%/%'
THEN LTRIM(RIGHT(your_column_name, CHARINDEX('/', REVERSE(your_column_name), 1) - 1))
ELSE your_column_name END AS new_string
FROM your_table_name;
SQL Fiddle Demo
Related
I have a column SysTraNo with some specific data like
HO/20-21/DRP/0001
215/21-22/AGP/0003
I want to trim that whole column and only take 20-21 or 21-22 of that data. How can I do this?
Using the base string functions we can try:
SELECT val,
SUBSTRING(val,
CHARINDEX('/', val) + 1,
CHARINDEX('/', val, CHARINDEX('/', val) + 1) -
CHARINDEX('/', val) - 1) AS nums
FROM yourTable;
Demo
The logic here is to take a substring starting from the character after the first / until the character before the second /. On other databases, we could have used regular expressions, but SQL Server has no native support for this.
In sql server trim works like below.
As one example is above Here I have given another example using substring on email to extract the domain part.
SELECT
email,
SUBSTRING(
email,
CHARINDEX('#', email)+1,
LEN(email)-CHARINDEX('#', email)
) domain
FROM
practice
ORDER BY
email;
I have these 2 strings:
/Iwish/I dont have this problem
I wish/I dont have this problem
How can I get /I wish/ and I wish/ ?
I tried something like:
select substring(column, charindex('/', column), charindex('/', column)) from my_table
I wanted to get also:
I dont have this problem
It´s possible to do this with?
Is this what you were looking for?
select substr(column, (len(column) - CHARINDEX('/', reverse(column)) +1) +1)
from my_table
i would make a script that iterate over the records of a table with a cursor
and extract from a column value formatted like that "yyy://xx/bb/147011"
only the final number 147011and to put this value in a variable.
It's possible to do something like that?
Many thanks.
You don't need a cursor for this. You can just use a query. The following gets everything after the last /:
select right(str, charindex('/', reverse(str)) - 1 )
from (values ('yyy://xx/bb/147011')) v(str)
It does not specifically check if it is a number, but that can be added as well.
You can also use the below query.
SELECT RIGHT(RTRIM('yyy://xx/bb/147011'),
CHARINDEX('/', REVERSE('/' + RTRIM('yyy://xx/bb/147011'))) - 1) AS LastWord
If numeric value has exact position defined with sample data, then you can do :
SELECT t.*, SUBSTRING(t.col, PATINDEX('%[0-9]%', t.col), LEN(t.col))
FROM table t;
I have url like:
http://mysites.xyz.com/_layouts/ng/ActivityStream.aspx/id/2624D92223261D370D7287C9E83CAEEA/Activity%20Stream%20Post.
I need to get the 2624D92223261D370D7287C9E83CAEEA. But not able to do so. my current query is
SUBSTRING(#URL2,LEN('http://mysites.xyz.com/_layouts/ng/ActivityStream.aspx/id/'),LEN(#URL2)-LEN('http://mysites.xyz.com/_layouts/ng/ActivityStream.aspx/id/')- CHARINDEX('/',REVERSE(#URL2))))
Please suggest.
Try this:
SELECT SUBSTRING(
#URL2,
CHARINDEX('/id/', #URL2)+4,
CHARINDEX('/', #URL2, CHARINDEX('/id/', #URL2)+5)
- (CHARINDEX('/id/', #URL2)+4))
Note: This assumes that the id is always followed by at least one more slash.
Breakdown:
The first argument of the substring is the string that contains the full expression.
The second one is the first index after /id/.
The third one is the desired length - calculated by the first index of / after /id/ - the first index after /id/.
update
To cope with strings that does not contain a slash after the id value, use case:
SELECT SUBSTRING(
#URL,
CHARINDEX('/id/', #URL)+4,
CASE WHEN CHARINDEX('/', #URL, CHARINDEX('/id/', #URL)+5) > 0 THEN
CHARINDEX('/', #URL, CHARINDEX('/id/', #URL)+5)
- (CHARINDEX('/id/', #URL)+4)
ELSE
LEN(#URL)
END
)
If the structure is always the same then you can try:
SELECT REPLACE(RIGHT(#s, LEN(#s) - 58), '/Activity%20Stream%20Post', '')
or:
SELECT REPLACE(REPLACE(#s,'/Activity%20Stream%20Post', ''), 'http://mysites.xyz.com/_layouts/ng/ActivityStream.aspx/id/', '')
I have data in SQL server database column like the following:
Column
Ot ntri - Non Cash - (6932)
Otr Contri- Cash - (6930)
anth C-Cash - (6935)
Phil Cor-Non Cash - (6937)
Poll Conh - (6940)
I need a query to select data that is present only withing the parantheses ().
Please help:
I need to select only
6932
6930
6935
etc for the column
Thanks
A combination of SUBSTRING and CHARINDEX could do this for you:
SELECT * ,
REVERSE(t.[Column]) AS Reversed ,
CASE WHEN t.[Column] LIKE '%(%'
AND t.[Column] LIKE '%)%'
THEN REVERSE(SUBSTRING(REVERSE(t.[Column]),
CHARINDEX(')', REVERSE(t.[Column])) + 1,
CHARINDEX('(', REVERSE(t.[Column]))
- CHARINDEX(')', REVERSE(t.[Column])) - 1))
ELSE NULL
END AS result
FROM dbo.[Table] AS t
I know you have accepted a good answer. This is another way of doing it. Same functions are used with CTE:
SQL server 2008 fiddle example to get the string within last brackets.
;with cte as (
select col, charindex(')',reverse(col),1) brk1,
charindex('(',reverse(col),1) brk2
from t
)
select col, reverse(substring(reverse(col),brk1+1,brk2-brk1-1)) mystr
from cte