Open In App

Reverse Statement Word by Word in SQL server

Last Updated : 01 Oct, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

To reverse any statement Word by Word in SQL server we could use the SUBSTRING function which allows us to extract and display the part of a string.

Pre-requisite :SUBSTRING function

Approach :

  • Declared three variables (@Input, @Output, @Length) using the DECLARE statement.
  • Use the WHILE Loop to iterate every character present in the @Input. For the condition inside the SQL Server while loop, use the SQL LEN Function to check whether the length of the Input is greater than 0 or not.
  • Within the while loop, use the SUBSTRING Function to set the @Output value with three parameters, and they are Input, start point, endpoint. Here we assigned the Input as @Input, use the start point as 0.
  • Use the CHARINDEX Function to find the ‘ ‘, so that the endpoint will be before the space.
  • Now, adding this to @Output, and which is empty at the starting of the loop.
  • To update the @Input variable, use the SUBSTRING Function. Here we assigned the Input as @Input.
  • Next, use the SQL CHARINDEX Function to find the empty space, and then we added 1 so that the starting point will be after space.
  • Then, use the SQL LEN Function to specify the end value.
  • At SET @Output it is extracting the word starting at 0 indexes and up to empty space. That will be the first word.
  • Once it got its first word, it will remove that word from @Input using the SET @Input code.

Input :
Welcome to SQL Server Tutorial on Geeksforgeeks.

Example –

DECLARE @Input VARCHAR(MAX)
DECLARE @Output VARCHAR(MAX)
DECLARE @Length INT 

SET @Input = 'Welcome to SQL Server 
             Tutorial on Geeksforgeeks.'

SET @Output = ''
WHILE LEN(@Input) > 0

BEGIN
IF CHARINDEX(' ', @Input) > 0

BEGIN
SET @Output = SUBSTRING
              (@Input, 0, CHARINDEX(' ', @Input))
               + ' ' + @Output
SET @Input = LTRIM
             (RTRIM
             (SUBSTRING
             (@Input, CHARINDEX 
             (' ', @Input) + 1, LEN(@Input))))
END

ELSE

BEGIN
SET @Output = @Input + ' ' + @Output
SET @Input = ''
END
END
SELECT @Output

Output :

Geeksforgeeks. on Tutorial Server SQL to Welcome

Input and Output using SQL Server Management Studio :


Next Article
Select statement in MS SQL Server

Similar Reads

Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
7 min read
Select statement in MS SQL Server
In this article, the most basic statement used in SQL is being discussed. Basic statement: select There are large amounts of data available everywhere. The data is stored in a sequential order in form of a table. Each table consists of a row which represents a unique record and column represents a field. Schemas are used to arrange the tables in a
2 min read
Insert statement in MS SQL Server
A database contains of many tables which has data stored in an order. To add up the rows, the user needs to use insert statement. Syntax : insert into table_name(column_list) values(values_list) For better understanding, an example is given below. Example - A table named student must have values inserted into it. It has to be done as follows: inser
2 min read
Insert Into Select statement in MS SQL Server
Prerequisites - Insert statement in MS SQL Server, Select statement in MS SQL Server Consider a university database. There are two tables namely Student and Marks. In case, the marks of a few students has to be transferred from the marks table to the marks table, there are many ways to do so. One can use subqueries (query nested in another query) b
2 min read
Multi-Statement Table Valued Function in SQL Server
In SQL Server, a multi-statement table-valued function (TVF) is a user-defined function that returns a table of rows and columns. Unlike a scalar function, which returns a single value, a TVF can return multiple rows and columns. Multi-statement function is very much similar to inline functions only difference is that in multi-statement function we
4 min read
How to Update Two Tables in One Statement in SQL Server?
To update two tables in one statement in SQL Server, use the BEGIN TRANSACTION clause and the COMMIT clause. The individual UPDATE clauses are written in between the former ones to execute both updates simultaneously. Here, we will learn how to update two tables in a single statement in SQL Server. SyntaxUpdating two tables in one statement in SQL
3 min read
DELETE Statement in MS SQL Server
The DELETE statement in MS SQL Server deletes specified records from the table. SyntaxMS SQL Server DELETE statement syntax is: DELETE FROM table_name WHERE condition; Note: Always use the DELETE statement with WHERE clause. The WHERE clause specifies which record(s) need to be deleted. If you exclude the WHERE clause, all records in the table will
1 min read
REVERSE() Function in SQL Server
REVERSE() function : This function in SQL Server is used to reverse the stated string and return the output. Features : This function is used to reverse the stated string. This function accepts strings as parameter. This function always returns string. This function can also take set of integers and reverse it. This function can even reverse float
1 min read
SQL SERVER – Input and Output Parameter For Dynamic SQL
An Input Parameter can influence the subset of rows it returns from a select statement within it. A calling script can get the value of an output parameter. An aggregate function or any computational expression within the stored process can be used to determine the value of the output parameter. A parameter whose value is given into a stored proced
3 min read
SQL Server | Convert tables in T-SQL into XML
In this, we will focus on how tables will be converted in T-SQL into XML in SQL server. And you will be able to understand how you can convert it with the help of command. Let's discuss it one by one. Overview :XML (Extensible Markup Language) is a markup language similar to HTML which was designed to share information between different platforms.
2 min read
Article Tags :
  • DBMS-SQL
  • SQL-Server
  • SQL

两个鬼故事刀客家族的女人冷水烫猪起名笔画多好吗叶无道徐灵儿小说全文免费最新于特岛惨案清山变我的2007给孩子起名那个字好极限竞速5www.xdowns.com无锡一医院号召捐献粪便救人哀悼词名字怎么起好听校花的贴身狂少姓潘起名的女孩子做反派的心头肉(快穿)052d导弹驱逐舰描写秋天的词语远大前程电视剧崔姓起名属鼠机械设备公司起名三字大全起名吉祥测试打分男孩起名字大全免费佛山车管所翁姓100分起名字鄂姓男孩起名初犬漫画挺田字男孩起名用书起名字费起名网少年生前被连续抽血16次?多部门介入两大学生合买彩票中奖一人不认账让美丽中国“从细节出发”淀粉肠小王子日销售额涨超10倍高中生被打伤下体休学 邯郸通报单亲妈妈陷入热恋 14岁儿子报警何赛飞追着代拍打雅江山火三名扑火人员牺牲系谣言张家界的山上“长”满了韩国人?男孩8年未见母亲被告知被遗忘中国拥有亿元资产的家庭达13.3万户19岁小伙救下5人后溺亡 多方发声315晚会后胖东来又人满为患了张立群任西安交通大学校长“重生之我在北大当嫡校长”男子被猫抓伤后确诊“猫抓病”测试车高速逃费 小米:已补缴周杰伦一审败诉网易网友洛杉矶偶遇贾玲今日春分倪萍分享减重40斤方法七年后宇文玥被薅头发捞上岸许家印被限制高消费萧美琴窜访捷克 外交部回应联合利华开始重组专访95后高颜值猪保姆胖东来员工每周单休无小长假男子被流浪猫绊倒 投喂者赔24万小米汽车超级工厂正式揭幕黑马情侣提车了西双版纳热带植物园回应蜉蝣大爆发当地回应沈阳致3死车祸车主疑毒驾恒大被罚41.75亿到底怎么缴妈妈回应孩子在校撞护栏坠楼外国人感慨凌晨的中国很安全杨倩无缘巴黎奥运校方回应护栏损坏小学生课间坠楼房客欠租失踪 房东直发愁专家建议不必谈骨泥色变王树国卸任西安交大校长 师生送别手机成瘾是影响睡眠质量重要因素国产伟哥去年销售近13亿阿根廷将发行1万与2万面值的纸币兔狲“狲大娘”因病死亡遭遇山火的松茸之乡“开封王婆”爆火:促成四五十对奥巴马现身唐宁街 黑色着装引猜测考生莫言也上北大硕士复试名单了德国打算提及普京时仅用姓名天水麻辣烫把捣辣椒大爷累坏了

两个鬼故事 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化