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

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))

Related

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 returns question mark for some unicode chars [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 3 years ago.
In a nvarchar(512) field if i store unicode chars like this:
UPDATE MYTABLE SET UNICODEFIELD = 'TレEホSᅯTル'
when i query it i get
T?E?S?T?
It looks like the "unusual" chars are not considered as unicode, i would expect the "?" behavior in case of varchar, while with nvarchar it should work fine, i am expecting
TレEホSᅯTル
as output, instead of
T?E?S?T?
Does anyone have an idea about this?
Because you're using a varchar, not an nvarchar. 'TレEホSᅯTル' = 'T?E?S?T?' as characters like レ can't be stored in a varchar.
Use a literal nvarchar:
UPDATE MYTABLE SET UNICODEFIELD = N'TレEホSᅯTル';

How to take out everything before dash(-) in SQL Server? [duplicate]

This question already has answers here:
getting all chars before space in SQL SERVER
(2 answers)
Closed 8 years ago.
I want to extract a certain part of a row. if there is something like this ..." abcd - productA", " zas1234 - productC", how do I extract everything before the dash(-)?
I tried a left(column name,'-') it didnt work. I am on SQL Server 2012.
The column from which I am trying to extract is a varchar column.
It would be great if someone could help me.
Try this,
select SUBSTRING(column_name, 1, CHARINDEX('-', column_name)-1) from table_name;
DECLARE #varStr VARCHAR(MAX)
SET #varStr='abcd - productA'
SELECT CHARINDEX('-',#varStr)
SELECT LEFT(#varStr,CHARINDEX('-',#varStr)-1)

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

Resources