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

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

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

Using TSQL how to Group By a Value and create a delimited string of ID [duplicate]

This question already has answers here:
Simulating group_concat MySQL function in Microsoft SQL Server 2005?
(12 answers)
How to concatenate text from multiple rows into a single text string in SQL Server
(47 answers)
Closed 7 years ago.
Using TSQL Group by how to get from
Id value
1 a
2 b
3 b
To
Ids Value Quantity
1 a 1
2,3 b 2

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)

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