sql⾃定义函数⽰例_SQLServerChoose()函数介绍和⽰例sql ⾃定义函数 ⽰例
In the article, a , we explored one of the important logical expressions – the Case statement in SQL Server to return the values based on the specified condition. Today, we will talk about another useful logical Choose() function in SQL Server 2012 onwards and its comparison with the Case statement.
在本⽂中, 的探讨了重要的逻辑表达式之⼀-SQL Server中的Case语句,⽤于根据指定条件返回值。 今天,我们将讨论SQL Server 2012及更⾼版本中另⼀个有⽤的逻辑Choose()函数及其与Case语句的⽐较。
SQL中的CASE语句概述 (An Overview of the CASE statement in SQL )
Before we proceed with the Choose function, let’s have a quick overview of the Case statement in SQL
在继续执⾏Choose函数之前,让我们快速了解⼀下SQL中的Case语句
It is a logical function, and we define conditions ( in When clause) and actions followed by Then clause
这是⼀个逻辑函数,我们定义条件(在When⼦句中)和随后的Then⼦句的操作
Once the expression or value satisfies in the when clause, it returns corresponding value or expression in the output
表达式或值在when⼦句中满⾜后,它将在输出中返回相应的值或表达式
If no conditions are satisfied, it returns the value specified in the Else clause
如果不满⾜任何条件,则返回Else⼦句中指定的值
In the below example, we specify product id 1 in the variable, and it returns the output ‘Bread and Biscuits’.
在下⾯的⽰例中,我们在变量中指定产品ID 1,并返回输出“⾯包和饼⼲”。
SQL Server Choose()函数简介 (Introduction to SQL Server Choose() function)
Suppose you need to specify multiple conditions in the case statement. In this case, the overall code will be lengthy. Sometimes, it is difficult to examine more extended code, and we always look for functions that could do similar work with minimum efforts and without any performance impact. Choose function does the same work for us and can be used as a replacement of Case statement.
假设您需要在case语句中指定多个条件。 在这种情况下,整个代码将很长。 有时,很难检查更多扩展的代码,并且我们总是寻能够以最⼩的努⼒完成类似⼯作并且不影响性能的函数。 选择功能为我们完成了相同的⼯作,可以⽤作Case语句的替代。
选择功能的语法 (Syntax of Choose function)
We use Choose () function to return an item at a specific index position from the list of items.
我们使⽤Choose()函数从项⽬列表中的特定索引位置返回项⽬。
Syntax of Choose function: CHOOSE ( index, value[1], value[2] ,….. value[N] )
选择函数的语法 : CHOOSE(index,value [1],value [2],….. value [N])
Index: It is an integer that specifies the index position of the element we want in the output. Choose function does not use a zero-based indexing method. In this function, the first item starts at first, the second element at the 2nd index position, and so on. If we do not use an integer in this argument, SQL converts that into integer else returns a NULL value Index :它是⼀个整数,指定我们想要的元素在输出中的索引位置。 选择函数不使⽤基于零的索引⽅法。 在此功能中,第⼀个项⽬⾸先开始,第⼆个元素在第⼆个索引位置,依此类推。 如果在此参数中不使⽤整数,SQL会将其转换为整数,否则返回NULL值Items: It is a comma-separated list of any type. Choose function picks the items as per the index defined in the first argument Items :它是任何类型的逗号分隔列表。 选择函数根据第⼀个参数中定义的索引选择项⽬
For the index, choose function returns value[1] for index position 1. Let’s understand choose function in SQL using various examples.
对于索引,select函数返回索引位置1的value [1]。让我们使⽤各种⽰例来了解SQL中的select函数。
⽰例1:带⽂字值SQL Server CHOOSE()函数 (Example 1: SQL Server CHOOSE() function with literal values)
In this example, we use choose() function to return values from different index positions. The first sel
ect statement returns NULL because this function does not use a zero indexing method.
在此⽰例中,我们使⽤choose()函数从不同的索引位置返回值。 第⼀条选择语句返回NULL,因为此函数未使⽤零索引⽅法。
SELECT CHOOSE(0, 'Apple', 'Banana', 'Mango', 'Grapes', 'Strawberry') AS 'Zero Index value';
SELECT CHOOSE(1, 'Apple', 'Banana', 'Mango', 'Grapes', 'Strawberry') AS 'First Index value';
SELECT CHOOSE(2, 'Apple', 'Banana', 'Mango', 'Grapes', 'Strawberry') AS 'Second Index value';
SELECT CHOOSE(3, 'Apple', 'Banana', 'Mango', 'Grapes', 'Strawberry') AS 'Third Index value';
Similarly, if we choose out of range index value, it returns NULL in that case as well. For example, we have five records in the above list. Let’s specify the index position as six.
同样,如果我们选择超出范围的索引值,则在这种情况下它也会返回NULL。 例如,上⾯的列表中有五个记录。 让我们将索引位置指定为六个。
SELECT CHOOSE(6, 'Apple', 'Banana', 'Mango', 'Grapes', 'Strawberry') AS 'Sixth Index value';
⽰例2:具有⼗进制索引值SQL Server CHOOSE()函数 (Example 2: SQL Server CHOOSE() function with decimal index values)
As specified earlier, we use integer data type in the first argument. Suppose you specify the index position as 2.5, what would be the output?
如前所述,我们在第⼀个参数中使⽤整数数据类型。 假设您将索引位置指定为2.5,输出是什么?
In the below, we specified multiple values in index argument having decimals. We get the same output from all the below queries. Choose() function rounds the decimal value towards lower value. I
n this case, all index values convert to 2 and return banana as output.
在下⾯,我们在索引参数中指定了多个带有⼩数的值。 我们从以下所有查询中获得相同的输出。 Choose()函数将⼗进制值四舍五⼊到较低的值。 在这种情况下,所有索引值都将转换为2并返回⾹蕉作为输出。
SELECT CHOOSE(2.10, 'Apple', 'Banana', 'Mango', 'Grapes', 'Strawberry') AS '2.1 Index value';
SELECT CHOOSE(2.40, 'Apple', 'Banana', 'Mango', 'Grapes', 'Strawberry') AS '2.4 Index value';
SELECT CHOOSE(2.59, 'Apple', 'Banana', 'Mango', 'Grapes', 'Strawberry') AS '2.5 Index value';
SELECT CHOOSE(2.99, 'Apple', 'Banana', 'Mango', 'Grapes', 'Strawberry') AS '2.99 Index value';
⽰例3:SQL Server Choose()函数的索引参数中的字符串值< (Example 3: String values in the index argument of SQL Server Choose() function<)
In this example, we specified index values in single quotes. It makes index argument as string values.
在此⽰例中,我们在单引号中指定了索引值。 它使索引参数成为字符串值。
SELECT CHOOSE('1', 'Apple', 'Banana', 'Mango', 'Grapes', 'Strawberry') AS 'Index value'
SELECT CHOOSE('5', 'Apple', 'Banana', 'Mango', 'Grapes', 'Strawberry') AS 'Index value';
SQL Server converts these string values into integers, and it works similar to specifying integer values as shown below. SQL Server将这些字符串值转换为整数,并且其⼯作⽅式类似于指定整数值,如下所⽰。
However, if we specify strings such as ‘two’ in index argument, It results in an error message due to data type conversion failure.
但是,如果我们在索引参数中指定诸如“ two”之类的字符串,由于数据类型转换失败,将导致错误消息。sql自学难吗
⽰例4:使⽤SQL Server Choose()函数和数据类型优先级 (Example 4: Use SQL Server Choose() function and data type precedence)
Look at the following SQL code. In this, we specified integer and float data type values.
看下⾯SQL代码。 在此,我们指定了整数和浮点数据类型值。
SELECT CHOOSE(2, 5,10,3.69, 29) as [Output]
Choose() function returns the highest precedence data type in the output. A float or decimal value ha
s high precedence over integer, so we get output in the high precedence data type as shown below.
Choose()函数在输出中返回最⾼优先级的数据类型。 浮点或⼗进制值的优先级⾼于整数,因此我们以⾼优先级的数据类型获取输出,如下所⽰。
Let’s add a string ‘abc’, and we get the same output of Choose() function.
让我们添加⼀个字符串“ abc”,我们得到与Choose()函数相同的输出。
SELECT CHOOSE(2, 5,10,3.69, 29,'ABC') as [Output]
If we specify index 5 that contains ‘abc’ you get an error message.
如果我们指定的索引5包含“ abc”,则会收到⼀条错误消息。
SELECT CHOOSE(5, 5,10,3.69, 29,'abc') as [Output]
We get this error because the SQL server is unable to change varchar data type to numeric data type having high precedence. You can refer to for more details.
我们收到此错误是因为SQL Server⽆法将varchar数据类型更改为具有较⾼优先级的数字数据类型。 您可以以获取更多详细信息。
⽰例5:将SQL Server Choose()函数与选择SQL语句⼀起使⽤ (Example 5: Use SQL Server Choose() function with Select SQL Statements)
In the previous examples, we understood the functionality of the SQL Choose() function. In this example, we use it in the select statement to retrieve records from the [AdventureWorks] database. It is a sample database for SQL Server, and you can download a backup copy from .
在前⾯的⽰例中,我们了解了SQL Choose()函数的功能。 在此⽰例中,我们在select语句中使⽤它从[ AdventureWorks ]数据库中检索记录。 它是SQL Server的⽰例数据库,您可以从下载备份副本。
SELECT top 10 [NationalIDNumber]
,[JobTitle]
,[HireDate]
,[MaritalStatus]
FROM [AdventureWorks].[HumanResources].[Employee]
We get employees’ records along with their hire date using the above query.
使⽤上⾯的查询,我们可以获得员⼯的记录以及他们的雇⽤⽇期。
Now, suppose we want to know the month for each employee. We can use the MONTH() function to return the month component from the date. In the below query, we specify month names and Choose() function returns the month from the list as per specified month in the index argument.
现在,假设我们想知道每个员⼯的⽉份。 我们可以使⽤MONTH()函数从⽇期返回⽉份组成部分。 在下⾯的查询中,我们指定⽉份名称,并且Choose()函数按照index参数中指定的⽉份从列表中返回⽉份。
SELECT top 10 [NationalIDNumber]
,[JobTitle]
,[HireDate]
,CHOOSE(MONTH([HireDate]),'January','February','March','April','May','June',
'July','August','September','October','November','December') As [HireMonth]
,[MaritalStatus]
FROM [AdventureWorks].[HumanResources].[Employee]
⽰例5:对JSON数据使⽤SQL Server Choose()函数 (Example 5: Use SQL Server Choose() function with JSON data)
We can use the Choose() function to work with JSON data as well. You can understand JSON and its different functions using the SQLShack language category.
我们也可以使⽤Choose()函数来处理JSON数据。 您可以使⽤SQLShack 语⾔类别来了解JSON及其不同功能。
Here, we use a table-valued OPENJSON function. It returns the data type of the value in JSON data
in the [type] column. In the below query, we use [type]+1 value in the index argument to retrieve the corresponding value from the list. We use +1 because [type] returns 0 for the NULL value but zero index position is not available in SQL Choose() function.
在这⾥,我们使⽤表值的OPENJSON函数。 它在[type]列中返回JSON数据中值的数据类型。 在下⾯的查询中,我们使⽤index参数中的[type] +1值从列表中检索相应的值。 我们使⽤+1是因为[type]对于NULL值返回0,但是SQL Choose()函数中不提供零索引位置。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论