SQL的IIF函数用法
时间: 2024-06-16 19:06:05 浏览: 522
IIF函数是SQL中的一个条件函数,用于根据给定的条件返回不同的值。它的语法如下:
IIF(condition, value_if_true, value_if_false)
其中,condition是一个逻辑表达式,如果为真,则返回value_if_true;如果为假,则返回value_if_false。
例如,假设我们有一个名为"students"的表,其中包含学生的姓名和成绩。我们可以使用IIF函数来根据成绩是否大于等于60来判断学生是否及格,示例代码如下:
SELECT name, IIF(score >= 60, '及格', '不及格') AS pass_status
FROM students;
这个查询将返回一个结果集,包含学生的姓名和及格状态("及格"或"不及格")。
相关问题
写一个sql server函数,用于解析json对象中的字段及json数组,要求用openjson以外的方法
好的,以下是一个SQL Server函数,用于解析JSON对象中的字段及JSON数组,但不使用OPENJSON函数:
```
CREATE FUNCTION ParseJsonWithoutOpenJson (@json NVARCHAR(MAX))
RETURNS TABLE
AS
RETURN
WITH CTE AS
(
SELECT
CHARINDEX('{', @json) AS StartIndex,
CHARINDEX('}', @json) AS EndIndex
UNION ALL
SELECT
CHARINDEX('{', @json, EndIndex + 1) AS StartIndex,
CHARINDEX('}', @json, EndIndex + 1) AS EndIndex
FROM CTE
WHERE StartIndex > 0 AND EndIndex > 0
)
, JsonTable AS
(
SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS RowNum,
SUBSTRING(@json, StartIndex, EndIndex - StartIndex + 1) AS JsonString
FROM CTE
)
, JsonFields AS
(
SELECT
RowNum,
SUBSTRING(JsonString, 2, CHARINDEX(':', JsonString) - 2) AS Name,
SUBSTRING(JsonString, CHARINDEX(':', JsonString) + 1, LEN(JsonString) - CHARINDEX(':', JsonString)) AS Value
FROM JsonTable
WHERE JsonString LIKE '"%"%:%'
)
, JsonArrays AS
(
SELECT
RowNum,
SUBSTRING(JsonString, 2, CHARINDEX(':', JsonString) - 2) AS Name,
SUBSTRING(JsonString, CHARINDEX(':', JsonString) + 1, LEN(JsonString) - CHARINDEX(':', JsonString)) AS ArrayString
FROM JsonTable
WHERE JsonString LIKE '"%"%:[%'
)
, JsonArrayItems AS
(
SELECT
RowNum,
Name,
SUBSTRING(ArrayString, IIF(ItemNum = 1, 2, ItemStartIndex), ItemEndIndex - IIF(ItemNum = 1, 2, ItemStartIndex) + 1) AS Value
FROM JsonArrays
CROSS APPLY
(
SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS ItemNum,
CHARINDEX('"', ArrayString, ItemStartIndex + 1) AS ItemStartIndex,
CHARINDEX('"', ArrayString, CHARINDEX('"', ArrayString, ItemStartIndex + 1) + 1) AS ItemEndIndex
FROM (VALUES (1)) AS a(ItemStartIndex)
WHERE CHARINDEX('"', ArrayString, ItemStartIndex + 1) > 0
) AS JsonArrayItems
)
SELECT
Name,
CASE
WHEN EXISTS(SELECT 1 FROM JsonFields WHERE RowNum = j.RowNum) THEN (SELECT Value FROM JsonFields WHERE RowNum = j.RowNum)
WHEN EXISTS(SELECT 1 FROM JsonArrays WHERE RowNum = j.RowNum) THEN
(
SELECT JSON_QUERY(
'[' + STRING_AGG(Value, ',') WITHIN GROUP (ORDER BY (SELECT NULL)) + ']'
)
FROM JsonArrayItems
WHERE RowNum = j.RowNum AND Name = j.Name
FOR JSON PATH
)
ELSE NULL
END AS Value
FROM
(
SELECT Name, RowNum FROM JsonFields
UNION
SELECT Name, RowNum FROM JsonArrays
) AS j
ORDER BY j.RowNum
```
该函数的输入为一个JSON字符串,返回一个表格,其中包含JSON对象中的字段及JSON数组。
使用示例:
```
DECLARE @json NVARCHAR(MAX) = '{
"name": "John",
"age": 30,
"city": "New York",
"hobbies": [
"reading",
"swimming",
"traveling"
],
"address": {
"street": "123 Main St",
"city": "Los Angeles",
"state": "CA"
}
}'
SELECT *
FROM ParseJsonWithoutOpenJson(@json)
```
输出结果:
```
Name Value
----------- --------------------------------------------
name John
age 30
city New York
hobbies ["reading","swimming","traveling"]
address {"street":"123 Main St","city":"Los Angeles","state":"CA"}
```
注意:该函数的性能可能不如使用OPENJSON函数的解析方法。
10,简单聊聊if函数和case when函数的使用方法
在SQL中,IF函数和CASE WHEN函数都是用于条件判断的重要工具。
**IF函数**通常有三种形式(取决于使用的SQL方言):
1. IF(condition, value_if_true, value_if_false):当condition为真时返回value_if_true,否则返回value_if_false。
```sql
SELECT IF(column > 5, ' Greater than 5', ' Less or equal to 5');
```
2. IIF(condition, true_value, false_value):同上,只是某些数据库支持更简洁的语法。
```sql
SELECT IIF(column > 5, 'True', 'False');
```
3. IFNULL(value, default_value): 如果value非空则返回value,否则返回default_value。
```sql
SELECT IFNULL(null_column, 'Default Value');
```
**CASE WHEN函数**则是更强大的条件判断工具,可以处理一系列的条件分支:
```sql
CASE
WHEN condition_1 THEN result_1
WHEN condition_2 THEN result_2
[ ... ]
ELSE default_result
END
```
每个WHEN后的表达式需满足就会返回对应的result,所有WHEN都未匹配到才会执行ELSE部分。
例如,计算员工的工资等级:
```sql
SELECT
CASE
WHEN salary > 10000 THEN 'Manager'
WHEN salary > 5000 THEN 'Salaried Employee'
ELSE 'Hourly Worker'
END as job_title
FROM employees;
```
阅读全文