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
Related
Basically, I would like to be able to Split a long text field after each date into unique rows that correspond to the dates. The source field "Notes" is just a long running text field with multiple comments over time with a distinct date ... initially, I tried splitting off the '-' after the date which works to some degree, except where there are dashes elsewhere in the text. So I'm thinking of something where I could split off of each unique instance of a date (mm/dd/yy) ... one issue is the length is not consistent meaning, it could be:
'm/d/yy-' or 'mm/dd/yy-' or 'mm/d/yy-'
Example Data > 'Notes' Column:
3/30/16-Had a meeting 2/5/16-LVM 10/5/15-Spoke to customer
*A single cell could have multiple dates and comments in it
Looking for end result like this:
Date Value
3/30/16 Had a meeting
2/15/16 LVM
10/5/15 Spoke to customer
I am using something basic like the below, but wondering if I can get a little more sophisticated with the STRING_SPLIT
SELECT NOTES, VALUE
FROM SRC_TABLE
CROSS APPLY STRING_SPLIT(NOTES, '-')
Appreciate any insights or ideas!
Hmmm. I think this does what you want:
select t.*, s.*
from src_table t outer apply
(select value as value_date
from string_split(t.notes, '-') s
where s.value like '%/%/%' and s.value not like '%/%/%/%'
) s;
EDIT:
If you just want to split on the first -, you can use:
select left(notes, charindex('-', notes + '-') - 1),
stuff(notes, 1, charindex('-', notes + '-'), '')
from src_table;
Here is a db<>fiddle.
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;
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
Have a ncarchar(MAX) field in SQL table. It has numbers such as 717.08064182582, 39.0676048113, etc. in which I need to only have 3 places after decimal. For instance 717.080, 39.067.
Without converting the field type, would like to get rid of those last n characters, however every row has different number of characters. I believe I could use ROUND (correct me if wrong), but would rather not.
Try this
SELECT CAST(ColumnName AS DECIMAL(18,3))
Without converting it data type As per #vkp Comment
SELECT SUBSTRING(ColumnName ,0,CHARINDEX('.', ColumnName )+4)
select CASE WHEN CHARINDEX('.', Your_column) > 0
THEN SUBSTRING(Your_column, 1, CHARINDEX('.', Your_column) + 3)
ELSE Your_column
END
this is similar as previous answers but more faster and safer
Try this:
SELECT SUBSTRING(Your_column, 1, PATINDEX('%.%', Your_column) + 3)
FROM Your_Table
Here is a sample of my data:
ABC*12345ABC
BCD*234()
CDE*3456789(&(&
DEF*4567A*B*C
Using SQL Server 2008 or SSIS, I need to parse this data and return the following result:
12345
234
3456789
4567
As you can see, the asterisk (*) is my first delimiter. The second "delimiter" (I use this term loosely) is when the sequence of numbers STOP.
So, basically, just grab the sequence of numbers after the asterisk...
How can I accomplish this?
EDIT:
I made a mistake in my original post. An example of another possible value would be:
XWZ*A12345%$%
In this case, I would like to return the following:
A12345
The value can START with an alpha character, but it will always END with a number. So, grab everything after the asterisk, but stop at the last number in the sequence.
Any help with this will be greatly appreciated!
You could do this with a little patindex and charindex trickery, like:
; with YourTable(col1) as
(
select 'ABC*12345ABC'
union all select 'BCD*234()'
union all select 'CDE*3456789(&(&'
union all select 'DEF*4567A*B*C'
union all select 'XWZ*A12345%$%'
)
select left(AfterStar, len(Leader) + PATINDEX('%[^0-9]%', AfterLeader) - 1)
from (
select RIGHT(AfterStar, len(AfterStar) - PATINDEX('%[0-9]%', AfterStar) + 1)
as AfterLeader
, LEFT(AfterStar, PATINDEX('%[0-9]%', AfterStar) - 1) as Leader
, AfterStar
from (
select RIGHT(col1, len(col1) - CHARINDEX('*', col1)) as AfterStar
from YourTable
) as Sub1
) as Sub2
This prints:
12345
234
3456789
4567
A12345
If you ignore that this is in SQL then the first thing that comes to mind is Regex:
^.*\*(.*[0-9])[^0-9]*$
The capture group there should get what you want. I don't know if SQL has a regex function.