SQL常用函数

Inner join vs left join

当您只想返回两边都有对的记录时,您将使用 INNER JOIN,当您需要“左”表中的所有记录时,您将使用 LEFT JOIN,无论它们是否在“右”表中有对或不。如果您需要两个表中的所有记录,无论它们是否成对,您都需要使用 CROSS JOIN(或使用 LEFT JOIN 和 UNION 模拟它)

性能

LEFT JOIN 绝对不比 INNER JOIN 快。事实上,它更慢;根据定义,外连接(LEFT JOIN 或 RIGHT JOIN)必须完成 INNER JOIN 的所有工作加上空扩展结果的额外工作。它还会返回更多的行,进一步增加总执行时间只是因为结果集的大小更大。

即使 LEFT JOIN 在特定情况下由于一些难以想象的因素汇合而更快,它在功能上也不等同于 INNER JOIN,因此您不能简单地用另一个替换一个的所有实例!

SQL server 常用函数

1PATINDEX(%pattern%, string):返回一个模式在字符串中的位置。

Parameter

Description

%pattern%

Required. The pattern to find. It MUST be surrounded by %. Other wildcards can be used in pattern, such as:

  • % - Match any string of any length (including 0 length)
  • _ - Match one single character
  • [] - Match any characters in the brackets, e.g. [xyz]
  • [^] - Match any character not in the brackets, e.g. [^xyz]

string

Required. The string to be searched

Eg:

SELECT PATINDEX('%schools%', 'W3Schools.com');  return 3

2STUFF(string, start, length, new_string): 删除字符串的一部分,然后从指定位置开始将另一部分插入到字符串中

Parameter

Description

string

Required. The string to be modified

start

Required. The position in string to start to delete some characters

length

Required. The number of characters to delete from string

new_string

Required. The new string to insert into string at the start position

Eg:

SELECT STUFF('SQL Tutorial', 1, 3, 'HTML');  return HTML Tutorial  

3、LTRIM(string):删除字符串中的前导空格

eg:

SELECT LTRIM('     SQL Tutorial') AS LeftTrimmedString;  return

LeftTrimmedString

SQL Tutorial

4、RTRIM(string):删除字符串中的后导空格

5、CONVERT(data_type(length), expression, style):将任意类型的值转换为指定的数据类型。

Value

Description

data_type

Required. The datatype to convert expression to. Can be one of the following: bigint, int, smallint, tinyint, bit, decimal, numeric, money, smallmoney, float, real, datetime, smalldatetime, char, varchar, text, nchar, nvarchar, ntext, binary, varbinary, or image

(length)

Optional. The length of the resulting data type (for char, varchar, nchar, nvarchar, binary and varbinary)

expression

Required. The value to convert to another data type

style

Optional. The format used to convert between data types, such as a date or string format. Can be one of the following values:

Converting datetime to character:

Without century

With century

Input/Output

Standard

0

100

mon dd yyyy hh:miAM/PM

Default

1

101

mm/dd/yyyy

US

2

102

 yyyy.mm.dd

ANSI

3

103

 dd/mm/yyyy

British/French

4

104

dd.mm.yyyy

German

5

105

 dd-mm-yyyy

Italian

6

106

dd mon yyyy

-

7

107

Mon dd, yyyy

-

8

108

hh:mm:ss

-

9

109

mon dd yyyy hh:mi:ss:mmmAM (or PM)

Default + millisec

10

110

mm-dd-yyyy

USA

11

111

 yyyy/mm/dd

Japan

12

112

 yyyymmdd

ISO

13

113

dd mon yyyy hh:mi:ss:mmm

Europe (24 hour clock)>

14

114

hh:mi:ss:mmm

24 hour clock

20

120

yyyy-mm-dd hh:mi:ss

ODBC canonical (24 hour clock)

21

121

yyyy-mm-dd hh:mi:ss.mmm

ODBC canonical (24 hour clock)

126

yyyy-mm-ddThh:mi:ss.mmm

ISO8601

127

yyyy-mm-ddThh:mi:ss.mmmZ

ISO8601 (with time zone Z)

130

dd mon yyyy hh:mi:ss:mmmAM

Hijiri

131

dd/mm/yy hh:mi:ss:mmmAM

Hijiri

Converting float to real:

Value

Explanation

0

Maximum 6 digits (default)

1

8 digits

2

16 digits

Converting money to character:

Value

Explanation

0

No comma delimiters, 2 digits to the right of decimal

1

Comma delimiters, 2 digits to the right of decimal

2

 No comma delimiters, 4 digits to the right of decimal

eg:

SELECT CONVERT(datetime, '2017-08-25');  return 2017-08-25 00:00:00.000

相关推荐

  1. SQL函数

    2024-03-13 00:36:02       39 阅读
  2. hive sql函数

    2024-03-13 00:36:02       52 阅读
  3. SQL函数收藏

    2024-03-13 00:36:02       38 阅读
  4. SQL时间处理函数总结

    2024-03-13 00:36:02       57 阅读
  5. SQL字符串函数

    2024-03-13 00:36:02       47 阅读
  6. sql sqlserver日期函数

    2024-03-13 00:36:02       48 阅读
  7. sql函数积累(非窗口函数

    2024-03-13 00:36:02       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-13 00:36:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-13 00:36:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-13 00:36:02       82 阅读
  4. Python语言-面向对象

    2024-03-13 00:36:02       91 阅读

热门阅读

  1. 【Node.js从基础到高级运用】七、基本的网络编程

    2024-03-13 00:36:02       46 阅读
  2. 全栈开发的必备利器 Next.js

    2024-03-13 00:36:02       43 阅读
  3. Linux应用程序对异步通知的处理

    2024-03-13 00:36:02       40 阅读
  4. 框架和函数库的区别

    2024-03-13 00:36:02       41 阅读
  5. android pdf框架-5,生成pdf

    2024-03-13 00:36:02       41 阅读
  6. 深入理解Nginx日志级别

    2024-03-13 00:36:02       39 阅读
  7. 库表设计基本字段

    2024-03-13 00:36:02       38 阅读
  8. LLM(大语言模型)常用评测指标-MAP@R

    2024-03-13 00:36:02       42 阅读
  9. 使用Docker部署debezium来监控MySQL数据库

    2024-03-13 00:36:02       46 阅读