How to extract Certain nth character from a string in SQL - sql

I have a field that returns the value as xxx-xxx-xxx-xxxxx-xx-x. How do i extract the 10th character from that code.

select substring('xxx-xxx-xxx-xxxxx-xx-x', 10, 1)
The documentation for the function on MSDN is here.
The SQL Fiddle demo is here (with different letters so you can clearly see which letter is extracted).

Use substring function
select substring('xxx-xxx-xax-xxxxx-xx-x', 10, 1)

you can use SUBSTRING, in your case use...
SELECT SUBSTRING(field, 10, 1)
field being the one that returns the value.

declare #x varchar(20) = 'xxx-xxx-xxx-xxxxx-xx-x'
select SUBSTRING(#x,10,CHARINDEX('-',#x)-4 )

Related

SQL server Rex fetch string in wildcard

For example, I have got a string with table name and the schema like:
[dbo].[statistical]
How can fetch just the table name statistical out from this string?
This is what PARSENAME is used for:
SELECT PARSENAME('[dbo].[statistical]', 1)
SELECT PARSENAME('[adventureworks].[dbo].[statistical]', 1)
SELECT PARSENAME('[adventureworks]..[statistical]', 1)
SELECT PARSENAME('[statistical]', 1)
SELECT PARSENAME('dbo.statistical', 1)
-- all examples return 'statistical'
You could alternatively try this:
declare #s varchar(100) = 'asd.stadfa';
select reverse(substring(s, 1, charindex('.', s) - 1)) from (
select reverse(#s) s
) a
charindex returns first occurence of character, so you reverse initial string to make last dot first. Then you just use substring to extract first part of reversed string, which is what you are looking for. Finally, you need to apply reverse one more time to reverse back extracted string :)

How to display first three characters in employee name in lower case and remaining characters in upper case in sql

I need an answer for the above question
You could use UPPER() & LOWER() function if you are working with Microsoft SQL Server
DECLARE #DATA NVARCHAR(MAX) = 'xxxyyy'
SELECT #DATA
SELECT CONCAT(UPPER(LEFT(#DATA, 3)), LOWER(SUBSTRING(#DATA, 4, LEN(#DATA))))
Result :
XXXyyy

How to replace nth character in sql server

I am trying to replace the nth character in SQL Server. I tried with it using replace():
SELECT REPLACE(ABC,0,1) FROM XXX
In above code all zeros will be replaced with one, but I only want to change it in a particular position and sometimes that position can change.
use stuff
The STUFF function inserts a string into another string. It deletes a specified length of characters in the first string at the start position and then inserts the second string into the first string at the start position.
select STUFF(ABC, starting_index, 1, 'X') from XXX
"Here your int position to replace" is the position no just replace with any int no and that position will be replaced
Note : (Thanks to pcnate for suggestion)
starting_index is your int position to replace.
You're looking for STUFF:
select STUFF(ABC, #n, 1, 'X') from XXX
This would replace the #nth character with an X.
Technically it seeks into the original string at column ABC starting at position #n, deletes 1 character, then inserts the string 'X' at that position.
You use STUFF for this:
SELECT STUFF(ABC, 5, 1, '1')
FROM XXX
This would replace the 5th character with a 1.
Use stuff():
select stuff(abc, 0, 1, 'a')
It is documented here.
Use Stuff.
STUFF(Column_Name,starting_index,
lenth_ofthestring_to_replace_from_starting_index, character_to_replce)
Example_
DECLARE #str varchar(100) = '123456789'
select #str
SELECT STUFF(#str,2,1, 'hello')
-- want to replece 1 charter starting from 2nd position with the string 'hello'
Check this.
SELECT STUFF(#str,2,25, 'hello'),len(#str)

How to remove the first character if it is a specific character in SQL

I currently have a table Telephone it has entries like the following:
9073456789101
+773456789101
0773456789101
What I want to do is remove only the 9 from the start of all the entries that have a 9 there but leave the others as they are.
any help would be greatly appreciated.
While all other answer are probably also working, I'd suggest to try and use STUFF function to easily replace a part of the string.
UPDATE Telephone
SET number = STUFF(number,1,1,'')
WHERE number LIKE '9%'
SQLFiddle DEMO
Here is the code and a SQLFiddle
SELECT CASE
WHEN substring(telephone_number, 1, 1) <> '9'
THEN telephone_number
ELSE substring(telephone_number, 2, LEN(telephone_number))
END
FROM Telephone
Update Telephone set number = RIGHT(number,LEN(number)-1) WHERE number LIKE '9%';
I recently solved a similar problem with a combination of RIGHT(), LEN() & PATINDEX(). PATINDEX will return the integer 1 when it finds a 9 as the first character and 0 otherwise. This method allows all records to be returned at once without a CASE WHEN statement.
SELECT
RIGHT(number, LEN(number) - PATINDEX('9%', number))
FROM Telephone
UPDATE dbo.Telephone
SET column_name = SUBSTRING(column_name, 2, 255)
WHERE column_name LIKE '9%';
Stuff is a great function for this. However, using it with an update statement with a where clause is great, but what if I was doing an insert, and I needed all of the rows inserted in one pass. The below will remove the first character if it is a period, does not use the slower case statement, and converts nulls to an empty string.
DECLARE #Attachment varchar(6) = '.GIF',
#Attachment2 varchar(6)
SELECT
#Attachment2 = ISNULL(ISNULL(NULLIF(LEFT(#Attachment, 1), '.'), '') + STUFF(#Attachment, 1, 1, ''), '')
SELECT
#Attachment2
DECLARE #STR nvarchar(200) = 'TEST'
SET #STR = STUFF(#STR,1,1,'')
PRINT #STR
Result will be "EST"
You can use replace in select statement instead of where or update
SELECT REPLACE(REPLACE('_'+number,'_9',''),'_','') FROM #tbl

T-SQL Substring - Last 3 Characters

Using T-SQL, how would I go about getting the last 3 characters of a varchar column?
So the column text is IDS_ENUM_Change_262147_190 and I need 190
SELECT RIGHT(column, 3)
That's all you need.
You can also do LEFT() in the same way.
Bear in mind if you are using this in a WHERE clause that the RIGHT() can't use any indexes.
You can use either way:
SELECT RIGHT(RTRIM(columnName), 3)
OR
SELECT SUBSTRING(columnName, LEN(columnName)-2, 3)
Because more ways to think about it are always good:
select reverse(substring(reverse(columnName), 1, 3))
declare #newdata varchar(30)
set #newdata='IDS_ENUM_Change_262147_190'
select REVERSE(substring(reverse(#newdata),0,charindex('_',reverse(#newdata))))
=== Explanation ===
I found it easier to read written like this:
SELECT
REVERSE( --4.
SUBSTRING( -- 3.
REVERSE(<field_name>),
0,
CHARINDEX( -- 2.
'<your char of choice>',
REVERSE(<field_name>) -- 1.
)
)
)
FROM
<table_name>
Reverse the text
Look for the first occurrence of a specif char (i.e. first occurrence FROM END of text). Gets the index of this char
Looks at the reversed text again. searches from index 0 to index of your char. This gives the string you are looking for, but in reverse
Reversed the reversed string to give you your desired substring
if you want to specifically find strings which ends with desired characters then this would help you...
select * from tablename where col_name like '%190'