我们如何从sql server中的给定结果集中反向获取所有记录? [英] How do we fetch all the records as an reverse manner from the given result set in sql server ?

查看:80
本文介绍了我们如何从sql server中的给定结果集中反向获取所有记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表格包含以下数据



COLA

---------------- ---------

今天是星期天

你好,怎么样?
你好我是bharadwaj

你好我很好

帮帮我





我需要输出如下



COLA

-------------------------

今天是星期天

你是多么喜欢

Bharadwaj我是你好

很好我喜欢

帮我解决

解决方案

不要。

SQL有字符串处理 - 最好 - 很差。



用你的演示语言做到这一点,这是微不足道的。让SQL尽其所能 - 存储和检索数据 - 并用专为它设计的语言进行字符串操作!



例如,在C#中,它是三个代码行!



  string  [] words = lineOfText.Split (' '); 
Array.Reverse(words);
string reversed = string .Join( ,words);


OriginalGriff - 基本上 - 是对的。请注意,即使性能很差(或接近差),也是可能的......;)



如果你想要反转字符串中的单词,试试这个: SQL SERVER - 按字词反向字符串 [ ^ ]


试试这个!..



  DECLARE   @ source   VARCHAR (MAX)
DECLARE @ dest VARCHAR (MAX)
DECLARE @ lenght INT

SET @ source = ' 今天是星期天'
SET @ dest = ' '

WHILE LEN( @ source )> 0
BEGIN
IF CHARINDEX(' ' @ source )> 0
BEGIN
SET @ dest = SUBSTRING( @ source 0 ,CHARINDEX(' ' @ source ))+ ' ' + @ dest
SET @ source = LTRIM(RTRIM(SUBSTRING( @ source ,CHARINDEX(' ' @ source )+ 1,LEN( @ source ))))
结束
ELSE
BEGIN
SET @ dest = @ source + ' ' + @ dest
SET @ source = ' '
END
END
SELECT @ dest


My table contains data as follows

COLA
-------------------------
Today is sunday
Hi how are you
Hello i am bharadwaj
Hi i am fine
Help me out


I Need output like as follows

COLA
-------------------------
Sunday is today
You are how hi
Bharadwaj am i hello
Fine am i hi
Out me help

解决方案

Don't.
SQL has string handling which is - at best - poor.

Do that in your presentation language, where it's trivial. Leave SQL to do what it's best at - storing and retrieving data- and do string manipulation in languages which are designed for it!

For example, in C#, it's three lines of code!

string[] words = lineOfText.Split(' ');
Array.Reverse(words);
string reversed = string.Join(" ", words);


OriginalGriff - basically - is right. Note that it's possible, even when the performance is poor (or nearly to poor)... ;)

If you want to reverse words in string, try this: SQL SERVER – Reverse String Word By Word[^]


try this!..

DECLARE @source VARCHAR(MAX)
DECLARE @dest VARCHAR(MAX)
DECLARE @lenght INT

SET @source = 'Today is sunday'
SET @dest = ''

WHILE LEN(@source) > 0
BEGIN
    IF CHARINDEX(' ', @source) > 0
    BEGIN
        SET @dest = SUBSTRING(@source,0,CHARINDEX(' ', @source)) + ' ' + @dest
        SET @source = LTRIM(RTRIM(SUBSTRING(@source,CHARINDEX(' ', @source)+1,LEN(@source))))
    END
    ELSE
    BEGIN
        SET @dest = @source + ' ' + @dest
        SET @source = ''
    END
END
SELECT @dest


这篇关于我们如何从sql server中的给定结果集中反向获取所有记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆