SQL Server Built-in Functions: String, Math, Date, and Wildcards, Lecture notes of Introduction to Database Management Systems

An overview of various built-in functions in SQL Server, including string functions for manipulating text, math functions for calculations, date functions for working with dates, and wildcards for partial string matches. String functions cover concatenation, substring extraction, replacement, and length determination. Math functions include arithmetic, PI, absolute value, square root, and raising to a power. Date functions enable extracting parts of a date, finding differences between dates, and obtaining current date. Wildcards allow filtering results based on partial string matches.

Typology: Lecture notes

2021/2022

Uploaded on 06/19/2022

nguyen-ly-6
nguyen-ly-6 ๐Ÿ‡ป๐Ÿ‡ณ

4.8

(10)

43 documents

1 / 35

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Built-in Functions
Functions and Wildcards
in SQL Server
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23

Partial preview of the text

Download SQL Server Built-in Functions: String, Math, Date, and Wildcards and more Lecture notes Introduction to Database Management Systems in PDF only on Docsity!

Built-in Functions

Functions and Wildcards in SQL Server

1. Function Overview

2. String Functions

3. Math Functions

4. Date Functions

5. Other Useful Functions

6. Wildcards

Table of Contents

  • String Functions โ€“ for manipulating text
    • Either Table values or user input
    • E.g. concatenate column values
  • Math Functions โ€“ calculations and working with aggregate

data

  • E.g. perform geometry and currency operations
  • Date and Time Functions
  • E.g. find length of timespan
  • Miscellaneous Functions SQL Functions

STRING FUNCTIONS

CONCAT, SUBSTRING, Etc.

  • SUBSTRING โ€“ extract part of a string
  • Example: get short summary of article String Functions (2) SUBSTRING( String , StartIndex , Length ) Index is 1-based! SELECT ArticleId, Author, Content, SUBSTRING(Content, 1, 200) + '...' AS Summary FROM Articles SUBSTRING('SoftSchool', 5, 6) School
  • REPLACE โ€“ replace specific string with another
  • Example: censor the word blood from album names String Functions (3) REPLACE( String , Pattern , Replacement ) SELECT REPLACE(Title, 'blood', '*****') AS Title FROM Album REPLACE('SoftSchool', 'Soft', 'Hard') HardSchool
  • LEFT & RIGHT โ€“ get characters from beginning or end of

string

  • Example: name shorthand (first 3 letters) String Functions (5) LEFT( String , Count ) RIGHT( String , Count ) SELECT Id, Start, LEFT(Name, 3) AS Shorthand FROM Games
  • Our database contains credit card details for customers
  • Provide a summary without revealing the serial numbers Problem: Obfuscate CC Numbers ID FirstName LastName PaymentNumber 1 Guy Gilbert 5645322227179083 2 Kevin Brown 4417937746396076 โ€ฆ โ€ฆ โ€ฆ ID FirstName LastName PaymentNumber 1 Guy Gilbert 564532 ********** 2 Kevin Brown 441793********** โ€ฆ โ€ฆ โ€ฆ
  • LOWER & UPPER โ€“ change letter casing
  • REVERSE โ€“ reverse order of all characters in string
  • REPLICATE โ€“ repeat string String Functions (6) LOWER( String ) UPPER( String ) REVERSE( String ) REPLICATE( String, Count )
  • CHARINDEX โ€“ locate specific pattern (substring) in string
  • STUFF โ€“ insert substring at specific position String Functions (7) CHARINDEX( Pattern, String, [ StartIndex ]) STUFF( String, StartIndex, Length, Substring ) Optional, begins at 1 Number of chars to delete
  • SQL Server supports basic arithmetic operations
    • Addition, subtraction, etc.
  • Example: find area of triangles by given side and height Math Functions SELECT Id, (AH)/2 AS Area FROM Triangles*
  • PI โ€“ get the value of Pi as float (15 โ€“ digit precision)
  • ABS โ€“ absolute value
  • SQRT โ€“ square root (result will be float)
  • SQUARE โ€“ raise to power of two Math Functions (2) SELECT PI() --3. ABS( Value ) SQRT( Value ) SQUARE( Value )
  • POWER โ€“ raise value to desired exponent
  • ROUND โ€“ obtain desired precision
    • Negative precision rounds characters before decimal point
  • FLOOR & CEILING โ€“ return the nearest integer Math Functions (3) ROUND( Value , Precision ) POWER( Value , Exponent ) FLOOR( Value ) CEILING( Value )
  • Calculate the required number of pallets to ship each item
    • BoxCapacity specifies how many items can fit in one box
    • PalletCapacity specifies how many boxes can fit in a pallet Problem: Pallets