Summary of common operations on SQL strings and numbers

--Truncate a character from a character in the string, and then insert another string here
select stuff('hi,world!',4,4,'****') --return value hel ****orld! --Returns
a string with a specified length starting from a specified position
select substring('Hello,World!',2,10) --Return value ello,World --Replace
a certain character in the string with The specified string
select replace('hi,world!','ll','aa') --return value heaao,world! --remove
the left space in the string
select ltrim('hi,world!') - -Return value hi,world! --Remove
the left space in the string
select ltrim('hi,world! ') --Return value hi,world! --Remove
the left and right space in the string
select ltrim(' hi ,world! ') --return value hi,world! --replace
NULL value with specified character
select isnull('a',null) --return value a --convert
data type
select cast('2007-10-11 ' as datetime) -- return value 2007-10-11 00:00:00.000
select convert(datetime,' 2007-10-11') -- return value 2007-10-11 00:00:00.000
--Get the length of the string
select len('hi,world!') --Return value 12
--Get the first 3 characters of the string
select left('hi,world!',3) --Return value hel
-- Get the last 3 characters of the string
select right('hi,world!',3) --Return value ld! --Remove
the first 3 characters of the string
select right('hi,world!',(len(' hi,world!')-3)) --return value lo,world! --remove
the last 3 characters of the string
select left('hi,world!',(len('hi,world!')-3 )) --return value hi,wor --get
the position of a string in the string (return number)
select charindex('e','hi,world!') --return value 2 --return
from the first The first 4 characters from the beginning of the second character
select left(right('[hahahaha]aaa',len('[hahahaha]aaa')-1),4) --return value hahahaha
--return the lowercase form of the character
select lower('HELLO,WORLD!') --return value hi,world!
--return the uppercase form of the character
select UPPER('hi,world!' ) --return value HELLO,WORLD!
--replace 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 nvarchar data type, it returns nvarchar; otherwise, it returns varchar. If any of the parameters is NULL, it returns NULL.)
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 --Copy
the character expression with the value of the right parameter
select REPLICATE('Hello,World!',4) --Return value Hello,World!Hello,World!Hello,World!Hello,World!
-- Return the reversed string
select REVERSE('Hello, World!') --Return value!dlroW,olleH --When
DIFFERENCE is used, the more similar the two strings are pronounced (limited to English-book deeds), the more the return value is Large (return value is between 0-4)
DIFFERENCE('sun','san') --return value 4
DIFFERENCE('sun','safdsdf') --return value 3
DIFFERENCE('sun','dgffgfdg' ) --return value 0 --convert
the numeric type with a decimal point to a rounded string with a settable length and settable decimal places
SELECT STR(123.34584, 7, 3) --return value 123.346
--When the set length value is less than the length of the integer part, the string will return the set length *
SELECT STR(123333.34584, 5, 4) --Return value *****

========== ===================================================== =================================
Numerical Operations Summary =============== ========= --Returns

the largest integer of the specified number
select floor(123456.1234) --Returns the value 123456 --Returns
the smallest number without a fractional part and not less than the value of its argument. If the parameter is an empty sequence, return an empty sequence
select ceiling(123.010) --return 124
select ceiling(null) --return NULL --return
the rounded value closest to the value
select round(126.018,2) -- Returns 126.12
-- returns a random number of type FLoat between 0-1
select rand() -- returns 0.94170703697981
-- returns the value of PI
SELECT PI() -- returns 3.14159265358979

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326728570&siteId=291194637