Sql Server - Replace only the first characters in a string [duplicate] - sql

This question already has answers here:
Removing leading zeroes from a field in a SQL statement
(15 answers)
Closed 9 years ago.
How can I replace only the the first zeros in a string?
Example:
string : '0000089001232100'
result: '89001232100'
I can't use SQL Server REPLACE function since I don't want all my zeros to be replaced.
Thanks

select substring(ColumnName, patindex('%[^0]%',ColumnName), 10) should give you what you need.
Also look into the RIGHT and LEFT functions. They strip a string from the left or right side, respectively.
If you wanted to use RIGHT, you could get the index of the first 'wanted' character using CHARINDEX(http://msdn.microsoft.com/en-us/library/ms186323.aspx), then use that index as the point to strip from in your RIGHT function.

If the string always have only numbers, you could use this:
DECLARE #String VARCHAR(30)
SET #String = '0000089001232100'
SELECT CONVERT(VARCHAR(20),CONVERT(NUMERIC(20,0),#String))

Related

How to get only the first value before delimeter in SQL? [duplicate]

This question already has answers here:
How to get the 1st value before delimiter in sql server
(4 answers)
How to split string with delimiter and get the first value
(1 answer)
Split string on only first occurance of character/delimiter
(4 answers)
Split the string. get first value of split SQL Server 2005
(1 answer)
Closed 10 months ago.
I have value like
b10F|b3432R|b2134D
I need to get only the first value before the pipe:
b10F
How can I do that?
use mysql query with SUBSTRING_INDEX
SELECT SUBSTRING_INDEX("b10F|b3432R|b2134D", "|", 1)
Maybe use this query which uses CHARINDEX function to find the location of first pipe character and then uses LEFT to get data till that position -1
SELECT
LEFT(
'b10F|b3432R|b2134D',
CHARINDEX('|', 'b10F|b3432R|b2134D' ) -1
)AS Output;

MSSQL - substring a value between other values [duplicate]

This question already has answers here:
Turning a Comma Separated string into individual rows
(16 answers)
Closed 2 years ago.
NOIDEHOB_NOIDE1_4321-123
i want to create a querie that extract the following values from the example above:
NOIDEHOB
NOIDE1
4321-123
I need to base the query on the sign _. The values NOIDEHOB, NOIDE1 and 4321-123 are dynamic and the length will vary. There will never be any other _ sign in the string.
Any suggestions?
You can use string_split():
select s.value
from string_split('NOIDEHOB_NOIDE1_4321-123', '_') s

How to replace characters in SQL [duplicate]

This question already has an answer here:
Converting special character to plain text in oracle
(1 answer)
Closed 5 years ago.
I'm a new SQL user, and I'd like to know if there is a simple way of replacing non-English characters in Oracle SQl-developer. Let's Say, I want Hernán Nuñez to show up as Hernan Nunez, but without having to actually replace "Hernán" with "Hernan". What I need is to replace everything that contains "á" to the same thing, but with "a" instead, for example. Any suggestions?
US7ASCII will give you the right result. Example:
SELECT CONVERT('BESANÇON','US7ASCII')
FROM table;
CONVERT(
--------
BESANCON
1 row selected.

Return everything after specific value [duplicate]

This question already has an answer here:
Convert fraction to decimal [closed]
(1 answer)
Closed 9 years ago.
Working on 10g.
I tried to write a regexp that removes everything before the first dash(-).
However, I did not anticipate the complexity of the syntax.
I have this:
REGEXP_SUBSTR (myVal, '[^"]+')
This removes all the values after the first double quotation leaving me with data like:
10-3/4, 5-1/2, 7-5/8
I assumed that changing the double quotes to a dash and putting the carat after the dash would do it, but no luck.
If that's all you need to do, then you don't need to use REGEX, since you could just do it with other simpler functions:
SELECT SUBSTRING(myVal,INSTR(myVal,'-'),LENGTH(myVal))
FROM YourTable
If you want to do that with REGEXP_SUBSTR, try this:
SELECT REGEXP_SUBSTR(myValue, '\-.*$')
FROM myTable
But the SUBSTR answer from Lamak is just as effective. The third SUBSTR parameter defaults to the length of the string so you can leave it off:
SELECT SUBSTR(myValue, INSTR(myValue, '-'))
FROM myTable

Split Up Text In A Column [duplicate]

This question already has answers here:
How to split string using delimiter char using T-SQL?
(4 answers)
How to split a comma-separated value to columns
(38 answers)
Split values over multiple rows [duplicate]
(2 answers)
Closed 9 years ago.
I have this piece of text that is stored in our MS-SQL database (ignore the quotes and no, I can't redesign how this work specifically):
"TEST|00000298398293|EQ5|Patient"
Now, when I do a simple select, I get that result being returned. What I'd like to do is split that string based on the "|" character and return the individual strings associated with this string, so that I could have "TEST", "0000298398293", "EQ5" and "Patient" in different fields. How can I do this? In PHP, you can use the explode method, is there something like that in MS-SQL?
It's surely not the most elegant solution but i've used in in the past:
DECLARE #Sql varchar(50) = 'TEST|00000298398293|EQ5|Patient'
SELECT
PARSENAME(REPLACE(#sql,'|','.'),4),
PARSENAME(REPLACE(#sql,'|','.'),3),
PARSENAME(REPLACE(#sql,'|','.'),2),
PARSENAME(REPLACE(#sql,'|','.'),1)
Notice : This only works if you have 3 pipes, eventually consider to redesign your database in the future!