SQL strings and Numbers are commonly used to summarize operations

  • 2020-05-19 06:02:36
  • OfStack

-- intercepts one character from one character in a string, and inserts another string here
select stuff (' hi world! ',4,4,'****') -- return value hel****orld!
Returns a string of the specified length from the specified position
select substring (' Hello World! ',2,10) -- return values ello,World
Replaces a character in a string with a specified string
select replace (' hi world! ','ll','aa') -- return value heaao,world!
-- removes the space on the left side of the string
select ltrim (' hi world! ') -- return value hi,world!
-- removes the space on the left side of the string
select ltrim (' hi world! ') -- return value hi,world!
- removes the Spaces on the left and right side of the string
select ltrim (' hi world! ') -- return value hi,world!
Replaces the NULL value with the specified character
select isnull('a',null) -- return value a
-- convert data types
select cast('2007-10-11' as datetime) -- return value 2007-10-11 00:00.000
select convert(datetime,'2007-10-11') -- return value 2007-10-11 00:00.000
Gets the length of the string
select len (' hi world! ') -- return value 12
Gets the first three characters of the string
select left (' hi world! ',3) -- return value hel
Gets the last three characters of the string
select right (' hi world! ',3) -- return value ld!
-- removes the first three characters of a string
select right (' hi world! '(len (' hi world! ')-3)) -- return value lo,world!
-- removes the last three characters from the string
select left (' hi world! '(len (' hi world! ')-3)) -- return values hi,wor
-- gets the position of a string in the string (returns the number)
select charindex (' e ', 'hi world! ') -- return value 2
-- returns the first four characters from the second character
select left(right('[hahahaha]aaa',len('[hahahaha]aaa')-1),4) -- return value hahahaha
-- returns the lowercase form of a character
select lower (' HELLO WORLD! ') -- return value hi,world!
Returns the uppercase form of a character
select UPPER (' hi world! ') -- return value HELLO,WORLD!
-- replaces all occurrences of the second specified string expression in the first string expression with the third expression
(if one of the input parameters is of type nvarchar, nvarchar is returned; Otherwise return varchar. If any one of the arguments is NULL, NULL is returned.
SELECT REPLACE (' Hello World! ','l','a') -- return value Heaao,Worad!
SELECT REPLACE (' Hello World! ','l',') -- return value Heo,Word!
SELECT REPLACE (' Hello World! ','l',null) -- return value NULL
-- copies the character expression several times with the value of the parameter on the right
select REPLICATE (' Hello World! ',4) -- return value Hello,World! Hello World! Hello World! Hello World!
Returns the reversed string
select REVERSE (' Hello World! ') -- return value! dlroW, olleH
-- when using DIFFERENCE, the more similar the two strings sound (only English title characters), the larger the return value (between 0 and 4)
DIFFERENCE('sun','san') -- returns a value of 4
DIFFERENCE('sun','safdsdf') -- return value 3
DIFFERENCE('sun','dgffgfdg') -- returns the value 0
Converts the number type with a decimal point to a string with a length of 4 rounds and a decimal place of 5
SELECT STR(123.34584, 7, 3) -- return value 123.346
-- when the set length value is less than the integer part length, the string returns the set length *
SELECT STR(123333.34584, 5, 4) -- return value *****

===================================== ===============================
= = = = = = = = = = = = = = = = = = = = = = = = = digital operation summary = = = = = = = = = = = = = = = = = = = = = = =

Returns the largest integer of the specified number
select floor(123456.1234) -- return value 123456
Returns the smallest number with no decimal part and no less than the value of its argument. Returns an empty sequence if the parameter is an empty sequence
select ceiling(123.010) -- return 124
select ceiling(null) -- return NULL
-- returns the value closest to the value after 4 rounds and 5 entries
select round(126.018,2) -- returns 126.12
-- returns a random number of type FLoat between 0 and 1
select rand() -- return 0.94170703697981
-- returns the value of PI
SELECT PI() -- return 3.14159265358979

Related articles: