Sql server® 2012 t-sql fundamentals



Download 10,93 Mb.
Pdf ko'rish
bet111/443
Sana26.01.2022
Hajmi10,93 Mb.
#411755
1   ...   107   108   109   110   111   112   113   114   ...   443
Bog'liq
BookSQL

The 

PATINDEX

 Function

The PATINDEX function returns the position of the first occurrence of a pattern within a string.



Syntax

PATINDEX(patternstring)

The argument pattern uses similar patterns to those used by the LIKE predicate in T-SQL. I’ll explain 

patterns and the LIKE predicate later in this chapter, in “The LIKE Predicate.” Even though I haven’t 

explained yet how patterns are expressed in T-SQL, I include the following example here to show how 

to find the position of the first occurrence of a digit within a string.

SELECT PATINDEX('%[0-9]%', 'abcd123efgh');

This code returns the output 5.



The 

REPLACE

 Function

The REPLACE function replaces all occurrences of a substring with another.



Syntax

REPLACE(stringsubstring1substring2)

The function replaces all occurrences of substring1 in string with substring2. For example, the fol-

lowing code substitutes all occurrences of a dash in the input string with a colon.

SELECT REPLACE('1-a 2-b', '-', ':');

This code returns the output: ‘1:a 2:b’.

You can use the REPLACE function to count the number of occurrences of a character within a 

string. To do this, you replace all occurrences of the character with an empty string (zero characters) 

and calculate the original length of the string minus the new length. For example, the following query 

returns, for each employee, the number of times the character e appears in the lastname attribute.

SELECT empid, lastname, 

  LEN(lastname) - LEN(REPLACE(lastname, 'e', '')) AS numoccur 

FROM HR.Employees;

This query generates the following output.

empid       lastname             numoccur 

----------- -------------------- ----------- 

5           Buck                 0 

8           Cameron              1 

1           Davis                0 

9           Dolgopyatova         0 

2           Funk                 0 

7           King                 0 

3           Lew                  1 

4           Peled                2 

6           Suurs                0

www.it-ebooks.info





Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   107   108   109   110   111   112   113   114   ...   443




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish