Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

Bungkus teks di SQL server menggunakan fungsi

Fungsi 1

Create FUNCTION [dbo].[fn_BraekTextInLines]
(
    -- Add the parameters for the function here
    @InString varchar(max), 
    @LineLength int
)
RETURNS nvarchar(max)
AS
BEGIN


if @LineLength <=0 or @LineLength> LEN(@InString)
return @InString

declare @tmp varchar(max)
declare @result varchar(max)
DECLARE @word varchar (max);
declare @addedInResult bit
DECLARE c CURSOR LOCAL STATIC READ_ONLY FORWARD_ONLY
FOR SELECT s FROM SplitMax(@InString,'');

OPEN c;

FETCH NEXT FROM c INTO @word;
--set @tmp [email protected]
WHILE @@FETCH_STATUS = 0
BEGIN


  if LEN(@tmp + ' ' + @word) < @LineLength
  begin
    set @tmp = @tmp + ' ' + @word   
    set @addedInResult = 0
  end
  else
  begin

    set @result =  isnull(@result, ' ') + CHAR(13) +  RTRIM(LTRIM( @tmp))
    set @tmp = @word
    set @addedInResult = 1

  end

    FETCH NEXT FROM c INTO @word;

    if @@FETCH_STATUS <> 0
    begin
     set @result =  isnull(@result, ' ') + CHAR(13) + RTRIM(LTRIM( @tmp))
     set @addedInResult = 1
    end
END

CLOSE c;
DEALLOCATE c;

if @addedInResult=0
begin
    set @result =  isnull(@result, ' ') + CHAR(13) +  RTRIM(LTRIM( @tmp))
end

return @result

END

Fungsi 2

   Create FUNCTION [dbo].[fn_WrapString]
(
    -- Add the parameters for the function here
    @InString varchar(max), 
    @LineLength int
)
RETURNS nvarchar(max)
AS
BEGIN

declare @result varchar(max)
declare @tmp varchar(max)
DECLARE @Line varchar (max);

DECLARE c CURSOR LOCAL STATIC READ_ONLY FORWARD_ONLY
FOR SELECT s FROM SplitMax(@InString,CHAR(13));

OPEN c;

FETCH NEXT FROM c INTO @Line;

WHILE @@FETCH_STATUS = 0
BEGIN

  set @tmp = dbo.fn_BraekTextInLines(@Line,@LineLength)
  set @result = isnull(@result,' ') + @tmp

  FETCH NEXT FROM c INTO @Line;
END

CLOSE c;
DEALLOCATE c;

return Rtrim(Ltrim(@result))

END

Fungsi 3

ALTER FUNCTION [dbo].[SplitMax](@String VARCHAR(max), @Delimiter CHAR(1))       
RETURNS @temptable TABLE (s VARCHAR(max))       
AS       
BEGIN       
DECLARE @idx INT       
DECLARE @slice VARCHAR(max)        
SELECT @idx = 1       
IF len(@String)<1 OR @String IS NULL  RETURN       
while @idx!= 0       
BEGIN       
SET @idx = charindex(@Delimiter,@String)       
IF @idx!=0       
SET @slice = LEFT(@String,@idx - 1)       
ELSE       
SET @slice = @String       
IF(len(@slice)>0)  
INSERT INTO @temptable(s) VALUES(@slice)       
SET @String = RIGHT(@String,len(@String) - @idx)       
IF len(@String) = 0 break       
END   
RETURN       
END

Memanggil Fungsi fn_WrapString untuk membungkus teks

declare @name varchar(max)
set @name = 'Ine was King of Wessex from 688 to 726. He was'+ CHAR(13) +'unable to retain the territorial gains of his predecessor, Cædwalla, who had brought much of southern England under his'
print dbo.fn_WrapString(@name,60)

Keluaran :

Ine was King of Wessex from 688 to 726. He was 
unable to retain the territorial gains of his predecessor,
Cædwalla, who had brought much of southern England under
his



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Migrasi SQL Server 2000 ke 2008 - ORDER BY Issue saat menggunakan DISTINCT

  2. Msg 4834 Anda tidak memiliki izin untuk menggunakan pernyataan beban massal

  3. Menggabungkan nilai baris T-SQL

  4. 4 Kegiatan Pemantauan Basis Data Utama Yang Harus Diketahui Setiap DBA

  5. Gunakan Excel 2010 untuk membaca/menulis ke database SQL Server 2008 menggunakan prosedur tersimpan