split string based on a delimeter in sql server 2008 [duplicate] - sql-server

This question already has answers here:
Using T-SQL, return nth delimited element from a string
(14 answers)
Closed 6 years ago.
I want to split a column string
This is the string :
Final Settlement Of Security Fee /BTS-15-0114/SYED AKHNOKH HUSSAIN
I want to split on the bases of '/', i want this result :
Final Settlement Of Security Fee
BTS-15-0114
SYED AKHNOKH HUSSAIN
and i want to get the middle string 'BTS-15-0114'

Does all the values is same format like you posted?
Here is a sample for your sample string.
DECLARE #s VARCHAR(max)='Final Settlement Of Security Fee /BTS-15-0114/SYED AKHNOKH HUSSAIN'
SELECT PARSENAME( REPLACE(#s,'/','.'),2)
WIll get:
BTS-15-0114

Related

SQL Server remove special character from table [duplicate]

This question already has answers here:
What is the meaning of the prefix N in T-SQL statements and when should I use it?
(4 answers)
Closed 4 months ago.
I have table in SQL Server with a column Team, but when inserting data through BCP, some special character are on the first line:
TEAM
1.Insta Acq
I tried with the code shown here, but with no success. I copied the special symbol and paste it in the replace function as
REPLACE(COLUMN NAME,'1.Insta Acq', '1.Insta Acq' )
Datatypes:
COLUMN_NAME DATA_TYPE TYPE_NAME
-------------------------------------
TEAM -9 nvarchar
SOURCE 12 varchar
STAGE 12 varchar
TARGET OCT'22 4 int
Sample data in the table:
TEAM SOURCE STAGE TARGET OCT'22
------------------------------------------------------------------
1.Insta Acq Website TB Active / TB Inactive 9000
1.Insta Acq Website No Offer 3500
Since you're dealing with "special" characters that are probably Unicode, you must use the N prefix in your REPLACE call to indicate that these are Unicode string literals you're working with.
Try this:
REPLACE(COLUMN NAME, N'1.Insta Acq', N'1.Insta Acq')

Get everything after and before certain character in SQL [duplicate]

This question already has answers here:
A SQL Query to select a string between two known strings
(18 answers)
Closed 2 years ago.
I have a string like
https:\/\/www.abc.ca\/v3\/homes\/190135717\/responses\/bulk?page=5&per_page=100
I want everything after the equal sign = and before &, i.e the number 5 in SQL Server
Try this below logic using SUBSTRING and CHARINDEX:
Demo Here
DECLARE #T VARCHAR(200) = 'https:\/\/www.abc.ca\/v3\/homes\/190135717\/responses\/bulk?page=5&per_page=100'
SELECT SUBSTRING(#T,CHARINDEX('=',#T,0)+1,CHARINDEX('&',#T,0)-CHARINDEX('=',#T,0)-1)

How to replace multiple occurrence of comma in a single comma between two substring in SQL Server [duplicate]

This question already has answers here:
Replacing multiple commas with single comma in MS SQL
(8 answers)
Closed 2 years ago.
I have one input string 'abcdefg,,,,,,,ghijk,,,,,lmno'
and we want an output of 'abcdefg,ghijk,lmno' in SQL Server.
Based on this solution you can replace multiple , with a single , like this:
SELECT REPLACE(REPLACE(REPLACE('abcdefg,,,,,,,ghijk,,,,,lmno', ',', '<>'), '><', ''), '<>', ',')
demo on dbfiddle.uk

SQL Server: Group by First Character (i.e. Letter) [duplicate]

This question already has answers here:
group by first character
(7 answers)
Closed 9 years ago.
How do I a query in SQL Server using T-SQL to return the counts of the first letter/character in a column's values?
SELECT SUBSTRING(FileName, 1, 1) as first_letter, COUNT(FileId)
FROM Files
GROUP BY SUBSTRING(FileName,1,1)
ORDER BY first_letter
I modified an example for Oracle that can be found here:
group by first character

Trim leading 0s from string in T-Sql [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Removing leading zeroes from a field in a SQL statement
In a SQL Server 2008 database I have a string column which I need to select with the leading "0"s removed. E.g. 0023AFF should be returned as 23AFF.
Is this possible in T-Sql and how?
Use PatIndex
declare #s varchar(20)='0023aff'
select substring(#s,PATINDEX('%[^0]%',#s),LEN(#s))

Resources