Analog TrimLeft C# function [duplicate] - sql

This question already has answers here:
Better techniques for trimming leading zeros in SQL Server?
(19 answers)
Closed 3 years ago.
How can I remove leading zeros from a string such as '0097619896'?

Numerical data types don't contain leading zeroes, so you can convert to a int/bigint/decimal/etc and it'll strip them off:
SELECT CONVERT(int,'0097619896'), CONVERT(decimal(10,0),'0097619896');
If the values aren't numerical values, you can use PATINDEX to find the first non-zero character and STUFF to remove them:
SELECT STUFF(V.YourString,1,PATINDEX('%[^0]%',V.YourString)-1,'')
FROM (VALUES('0097619896-abc'))V(YourString);

Related

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

Need help targeting specific characters in a column in SQL [duplicate]

This question already has answers here:
How to extract Certain nth character from a string in SQL
(4 answers)
Closed 4 years ago.
I am trying to select a column using SQL but with a condition that would ignore all values in the column that have the 4th character of the value as 0.
For example,
my Column is called promos, and the stored values are W080045678, I want this value to be ignore since the 4th character is a 0 in the string.
What is the condition that I can add?
I have tried
AND promos <> '__'0%'
But no luck.
You are looking for NOT LIKE:
and promos not like '___0%'

How to remove leading 0 from string in Oracle SQL? [duplicate]

This question already has answers here:
Removing leading zeros from varchar sql developer
(5 answers)
Closed 4 years ago.
I have a string looks like this.
00000000004000000
00000000001100000
00000001432000000
00000000167700000
I want to remove all leading leading 0 from that column, how can I achieve that?
You can use the TRIM function:
SELECT TRIM(LEADING '0' FROM col)
this will work:
select ltrim(colname,'0') from table_name;

SQL Server: select the first character that is not a digit 0-9 or / [duplicate]

This question already has answers here:
SQL to find first non-numeric character in a string
(3 answers)
Closed 5 years ago.
I have a table called Person, and a NVarChar column called Notes.
The Notes column has a lot of text in it, but always begins with a number of some kind, with /'s inserted throughout.
For example:
1/23 some text
45/678/9%*&^%$##
02/468/ some other text
I need to select the first character position that isn't a digit or /.
I don't care whether the position is 0-based or 1-based; I can accommodate that after the fact.
In this example, if I'm using 1-based character positions, the selection should produce the following:
5
9
8
So you're looking for an index that matches some sort of pattern, say a pattern index. If we're whimsical, we might abbreviate it to PATINDEX.
SELECT PATINDEX('%[^0-9/]%', Notes)
FROM Person

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!