検索
ホームページデータベースmysql チュートリアルSQL里面用自定义Split()完成个性化需求

为了满足需求自定义Split()在SQL中实现,代码很整洁,感兴趣的朋友可以参考下,或许对你学习sql语句有所帮助

代码如下:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE function [dbo].[SplitString]
(
@Input nvarchar(max),
@Separator nvarchar(max)=',',
@RemoveEmptyEntries bit=1
)
returns @TABLE table
(
[Id] int identity(1,1),
[Value] nvarchar(max)
)
as
begin
declare @Index int, @Entry nvarchar(max)
set @Index = charindex(@Separator,@Input)
while (@Index>0)
begin
set @Entry=ltrim(rtrim(substring(@Input, 1, @Index-1)))
if (@RemoveEmptyEntries=0) or (@RemoveEmptyEntries=1 and @Entry'')
begin
insert into @TABLE([Value]) Values(@Entry)
end
set @Input = substring(@Input, @Index+datalength(@Separator)/2, len(@Input))
set @Index = charindex(@Separator, @Input)
end
set @Entry=ltrim(rtrim(@Input))
if (@RemoveEmptyEntries=0) or (@RemoveEmptyEntries=1 and @Entry'')
begin
insert into @TABLE([Value]) Values(@Entry)
end
return
end

函数、表都建好了,下面调用测试一下吧:
代码如下:
declare @str1 varchar(max), @str2 varchar(max), @str3 varchar(max)
set @str1 = '1,2,3'
set @str2 = '1###2###3'
set @str3 = '1###2###3###'
select [Value] from [dbo].[SplitString](@str1, ',', 1)
select [Value] from [dbo].[SplitString](@str2, '###', 1)
select [Value] from [dbo].[SplitString](@str3, '###', 0)

结果,截个图来看一下:
声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
MySQLでビューを使用することの限界は何ですか?MySQLでビューを使用することの限界は何ですか?May 14, 2025 am 12:10 AM

mysqlviewshavelimitations:1)supportallsqloperations、制限、dataManipulationswithjoinsorubqueries.2)それらは、特にパフォーマンス、特にパルフェクソルラージャターセット

MySQLデータベースのセキュリティ:ユーザーの追加と特権の付与MySQLデータベースのセキュリティ:ユーザーの追加と特権の付与May 14, 2025 am 12:09 AM

reperusermanmanagementInmysqliscialforenhancingsecurationsinginuring databaseaperation.1)usecreateusertoaddusers、指定connectionsourcewith@'localhost'or@'% '。

MySQLで使用できるトリガーの数にどのような要因がありますか?MySQLで使用できるトリガーの数にどのような要因がありますか?May 14, 2025 am 12:08 AM

mysqldoes notimposeahardlimitontriggers、しかしpracticalfactorsdeTerminetheireffectiveuse:1)serverconufigurationStriggermanagement; 2)complentiggersincreaseSystemload;

mysql:Blobを保管しても安全ですか?mysql:Blobを保管しても安全ですか?May 14, 2025 am 12:07 AM

はい、それはssafetostoreblobdatainmysql、butonsiderheSeCactors:1)Storagespace:blobscanconsumesificantspace.2)パフォーマンス:パフォーマンス:大規模なドゥエットブロブスメイズ階下3)backupandrecized recized recized recize

MySQL:PHP Webインターフェイスを介してユーザーを追加しますMySQL:PHP Webインターフェイスを介してユーザーを追加しますMay 14, 2025 am 12:04 AM

PHP Webインターフェイスを介してMySQLユーザーを追加すると、MySQLI拡張機能を使用できます。手順は次のとおりです。1。MySQLデータベースに接続し、MySQLI拡張機能を使用します。 2。ユーザーを作成し、CreateUserステートメントを使用し、パスワード()関数を使用してパスワードを暗号化します。 3. SQLインジェクションを防ぎ、MySQLI_REAL_ESCAPE_STRING()関数を使用してユーザー入力を処理します。 4.新しいユーザーに権限を割り当て、助成金ステートメントを使用します。

MySQL:BLOBおよびその他のNO-SQLストレージ、違いは何ですか?MySQL:BLOBおよびその他のNO-SQLストレージ、違いは何ですか?May 13, 2025 am 12:14 AM

mysql'sblobissuitable forstoringbinarydatawithinarationaldatabase、whileenosqloptionslikemongodb、redis、andcassandraofferferulesions forunstructureddata.blobissimplerbutcanslowdowdowd withwithdata

MySQLユーザーの追加:構文、オプション、セキュリティのベストプラクティスMySQLユーザーの追加:構文、オプション、セキュリティのベストプラクティスMay 13, 2025 am 12:12 AM

toaddauserinmysql、使用:createuser'username '@' host'identifidedby'password '; here'showtodoitsely:1)chosehostcarefilytoconを選択しますTrolaccess.2)setResourcelimitslikemax_queries_per_hour.3)usestrong、uniquasswords.4)endforcessl/tlsconnectionswith

MySQL:文字列データ型の一般的な間違いを回避する方法MySQL:文字列データ型の一般的な間違いを回避する方法May 13, 2025 am 12:09 AM

toavoidcommonMonmistakeswithStringDatatypesinmysql、undultingStringTypenuste、choosetherightType、andManageEncodingandCollat​​ionsEttingtingive.1)U​​secharforfixed-LengthStrings、Varcharforaible Length、AndText/Blobforlardata.2)setCurrectCherts

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

MantisBT

MantisBT

Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

EditPlus 中国語クラック版

EditPlus 中国語クラック版

サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

Dreamweaver Mac版

Dreamweaver Mac版

ビジュアル Web 開発ツール

AtomエディタMac版ダウンロード

AtomエディタMac版ダウンロード

最も人気のあるオープンソースエディター

WebStorm Mac版

WebStorm Mac版

便利なJavaScript開発ツール