How can i Count Char in sql server 2008 [duplicate] - sql

This question already has answers here:
Number of times a particular character appears in a string
(9 answers)
Closed 4 years ago.
If i have string 'ABCDAAARSTRLAMA ' how can i count 'A' this char from the given string in sql server 2008

use len and Replace function
declare #myvar varchar(20)
set #myvar = 'ABCDAAARSTRLAMA'
select len(#myvar) - len(replace(#myvar,'A',''))
http://sqlfiddle.com/#!18/063d8/1

SELECT LENGTH( 'ABCDAAARSTRLAMA' ) - LENGTH( replace( 'ABCDAAARSTRLAMA', 'A', '' ) );
Length Function
The Length function in SQL is used to get the length of a string. This function has a different name for different databases:
MySQL: LENGTH( )
Oracle: LENGTH( )
SQL Server: LEN( )
Syntax
The syntax for the Length function is as follows:
LENGTH(str)

Try this one
SELECT (LENGTH('ABCDAAARSTRLAMA') - LENGTH(REPLACE('ABCDAAARSTRLAMA', 'A', '')));

select len('ABCDAAARSTRLAMA') - len(replace('ABCDAAARSTRLAMA', 'A', ''))
This would give the count of 'A' from the string

Related

What is alternative function in sql REGEXP_COUNT in SQL Server

Below is the variable in Oracle I need to convert in SQL Server please suggest.
V_BAD_EMAIL_CNT NUMBER := REGEXP_COUNT(pv_bad_emails,',') + 1;
This will return the number of comma's in the string + 1 in SQL
declare #pv_bad_emails varchar(1000);
set #pv_bad_emails = 'a,b,c,d';
select len(#pv_bad_emails) - len(replace(#pv_bad_emails, ',', '')) + 1
For more reference
How do you count the number of occurrences of a certain substring in a SQL varchar?

how to get the string before first occurrence of a special character [duplicate]

This question already has answers here:
How to Select a substring in Oracle SQL up to a specific character?
(8 answers)
Closed 1 year ago.
i have a column containing hostnames in the format of :
oraclehost.server.region.company.net
How to extract the oraclehost part from the hostname i.e the string before the first ..
Please sugges.Thanks.
SELECT REGEXP_SUBSTR(HOSTNAMES, '[^.]+', 1, 1) FROM MYTABLE;
Alternatively, substr + instr combination which would probably perform better for large data sets:
substr(hostnames, 1, instr(hostnames, '.') - 1)
For example:
SQL> with mytable (hostnames) as
2 (select 'oraclehost.server.region.company.net' from dual)
3 select substr(hostnames, 1, instr(hostnames, '.') - 1) result
4 from mytable;
RESULT
----------
oraclehost
SQL>

How to count a character in SQL Server? [duplicate]

This question already has answers here:
How to count instances of character in SQL Column
(17 answers)
Closed 3 years ago.
Please help me - I have problem with counting how many 'a' (character) there are in a row and column.
This is my query :
declare #zz as varchar(10) = '123a123a12'
select #zz
What function in SQL Server 2008 R2 can I use to count how many 'a' are in there?
How can I combine charindex with len?
Thanks
The old school trick here is to replace the 'a's with empty spaces ('') and compare the resulting string's length to the length of the original:
declare #zz as varchar(10) = '123a123a12'
declare #zz_without_a varchar(10)=replace(#zz,'a','')
declare #a_in_zz int=len(#zz)-len(zz_without_a)
I would use REPLACE and DATALENGTH instead of LEN, because of trailing spaces that could appear after replace.
Example:
DECLARE #zz as varchar(20) = '123a123a12 a'
SELECT DATALENGTH(#zz) - DATALENGTH(REPLACE(#zz, 'a', '')),
LEN(#zz) - LEN(REPLACE(#zz, 'a', ''))
The output is 3 and 9.
If #zz is NVARCHAR, not VARCHAR, you will have to divide by 2.
From MSDN:
DATALENGTH function returns the number of bytes used to represent any
expression
One possibe approach is to use REPLACE() and LEN() functions.:
DECLARE #zz varchar(10) = '123a123a12'
SELECT LEN(#zz) - LEN(REPLACE(#zz, 'a', '')) AS CharCount
Output:
CharCount
2
Another possible approach, if you want to count more than one character, is to use recursion:
DECLARE #zz varchar(10) = 'aa3a123a12'
;WITH cte AS (
SELECT 1 AS N
UNION ALL
SELECT N + 1
FROM cte
WHERE N < LEN(#zz)
)
SELECT COUNT(*) AS CharCount
FROM cte
WHERE SUBSTRING(#zz, N, 1) IN ('a', '1')

extract word from string in sql server [duplicate]

This question already has answers here:
Find a specific substring using Transact-SQL
(6 answers)
Closed 5 years ago.
I need to extract part of a string in sql server. Lets say I have this string in a column...
Name1=Bill Gates&Name2=Microsoft&Address1=The streetadress
How can I extract the text that is equal to Name2 eg Microsoft?
declare #string varchar(100) = 'Name1=Bill Gates&Name2=Microsoft&Address1=The streetadress'
select replace(PARSENAME (replace(#string, '&', '.'), 2), 'Name2=', '');
One old fashioned way of handling this is to just use basic string functions like CHARINDEX and SUBSTRING.
SELECT
SUBSTRING(col,
CHARINDEX('Name2', col) + 6,
CHARINDEX('&', col, CHARINDEX('Name2', col)) -
CHARINDEX('Name2', col) - 6) AS name
FROM yourTable
Note that this solution assumes that the key value pairs are fixed in the order you showed us. My query uses the ambersand after the second name as a termination marker. If this be not present, e.g. if Name2 could possibly the last key in the string, then my query would have to be updated.
Demo here:
Rextester

Query to pad left of a field with 0's [duplicate]

This question already has answers here:
Formatting Numbers by padding with leading zeros in SQL Server
(14 answers)
Closed 7 years ago.
I have been working on a query (in sql Server TSQL) which fills left of a number with 0's so output is always 5 digit.
So:
Select MuNumber From Mytable
for data 11,011,2132,1111
Creates output like
00011
02134
01111
I tried Lpad Function but numer of 0's can be different.
if Munumber is 1 we need 0000 and If MyNumber is 34 we need 000
Assuming that MuNumber is VARCHAR simply use RIGHT
SELECT RIGHT('00000' + MuNumber, 5)
FROM Mytable
Otherwise you need to convert it first
SELECT RIGHT('00000' + CONVERT(VARCHAR(5), MuNumber), 5)
FROM Mytable
And in general you can use this pattern:
DECLARE #num INT = 10;
SELECT RIGHT(REPLICATE('0', #num) + CONVERT(VARCHAR(5), MuNumber), #num)
FROM Mytable
Try this
select right('00000'+cast(col as varchar(5)),5) from table
You can use the user defined function udfLeftSQLPadding where you can find the source codes at SQL Pad Leading Zeros
After you create the function on your database, you can use it as follows
select
dbo.udfLeftSQLPadding(MuNumber,5,'0')
from dbo.Mytable
Another option:
declare #n int = 6
select stuff(replicate('0', #n), 6-len(n), len(n), n)
from (values('123'), ('2493'), ('35')) as x(n)