How to get number Reverse using while loop in SQL Server - sql-server-2008

I have a question about SQL Server: how to get reverse of the number using while loop?
Input: number : 452833861
Expected output : 168338254
I tried using reverse function
select reverse('452833861') reversno
using reverse works, but I need to implement it using a while loop.
Can you please tell me how to achieve this task in SQL Server?

Try this:
drop table #test
create table #test (id varchar(50))
insert into #test values ('452833861')
declare #i int = 0
declare #j int = (select len(id) from #test)
while (#i < #j)
BEGIN
select SUBSTRING(id,#j,1) from #test
set #j=#j-1
END

Related

SELECT INTO with EXECUTE() in SQL Server

This works:
drop table #A
select v = getdate()
into #A
select * from #A
But not this (no result):
drop table #A
create table #A (v varchar(30))
declare #x varchar(30) = 'select v = getdate() into #A'
execute(#x)
select * from #A
I need to be able to do this above one to address a scenario. Must be simple and silly, but just trying to understand why this just doesn't work like first one?
Please advise. Thanks experts!
I can propose workaround and move all you script to #x variable.
So your script will be as:
declare #x nvarchar(MAX) = '
drop table IF EXISTS #A
select v=getdate() into #A
select * from #A'
PRINT(#x)
EXEC(#x)
MSDN says
SELECT…INTO creates a new table in the default filegroup and inserts the resulting rows from the query into it.
So if you create table prior to select into, you cannot use select into, you need to use insert into

SQL Server T-SQL breaking a string into a temp table for a join

We have a SQL Server Scalar Function and part of the process is to take one of the input values and do the following
'inputvalue'
Create a table variable and populate with the following rows
inputvalue
inputvalu
inputval
inputva
inputv
input
inpu
inp
Then this table is joined to a query, ordered by len of the inputvalue desc and returns the top 1. The actual code is here
DECLARE #Result NVARCHAR(20);
DECLARE #tempDialCodes TABLE (tempDialCode NVARCHAR(20));
DECLARE #counter INT = LEN(#PhoneNumber);
WHILE #counter > 2
BEGIN
INSERT INTO #tempDialCodes(tempDialCode) VALUES(#PhoneNumber);
SET #PhoneNumber = SUBSTRING(#PhoneNumber, 1, #counter - 1);
SET #counter = #counter - 1;
END
SET #Result = (SELECT TOP 1 [DialCodeID]
FROM DialCodes dc JOIN #tempDialCodes s
ON dc.DialCode = s.tempDialCode
ORDER BY LEN(DialCode) DESC);
RETURN #Result
It works fine but I am asking if there is a way to replace the while loop and somehow joining to the inputvalue to get the same result. When I say it works fine, it's too dam slow but it does work.
I'm stumped on how to break up this string without using a loop and to a table variable but my warning light tells me this is not efficient for running against a table with a million rows.
Are you familiar with tally tables? The speed difference can be incredible. I try to replace every loop with a tally table if possible. The only time I haven't been able to so far is when calling a proc from within a cursor. If using this solution I would recommend a permanent dbo.Tally table with a sufficiently large size rather than recreating every time in the function. You will find other uses for it!
declare #PhoneNumber nvarchar(20) = 'inputvalue';
declare #tempDialCodes table (tempDialCode nvarchar(20));
--create and populate tally table if you don't already a permanent one
--arbitrary 1000 rows for demo...you should figure out if that is enough
--this a 1-based tally table - you will need to tweak if you make it 0-based
declare #Tally table (N int primary key);
insert #Tally
select top (1000) row_number() over (order by o1.object_id) from sys.columns o1, sys.columns o2 order by 1;
--select * from #Tally order by N;
insert #tempDialCodes
select substring(#PhoneNumber, 1, t.N)
from #Tally t
where t.N between 3 and len(#PhoneNumber)
order by t.N desc;
select *
from #tempDialCodes
order by len(tempDialCode) desc;

can we create create statement inside cursor loop in oracle

Can we do CTAS inside a cursor in oracle
I am trying below code
declare
l_email_string varchar2(100);
cursor c1 is
select * from EMAIL_OBS where rownum < 2;
begin
for rec in C1
loop
create table ABC_TEST
(
row_id ,
email_string
)
as
select
rowid ,
jasbk
from EMAIL_OBS ;
end loop ;
end ;
/
but it is showing error while if I remove CTAS then it is working fine
Please suggest
Thanks ,
Abhimpi
You cannot perform DDL in PL/SQL like this (CTAS is DDL). You will need to use Dynamic SQL. Look up 'EXECUTE IMMEDIATE' for examples.

How to delete specific word from string in mysql?

I have string "this is my test string" and I want to remove only word "my".
The problem is word "my" will select from another query as
select word from table
I tried this if I know the word
set string = REPLACE(string,'my','');
So, any help
I want to do something like
set string = REPLACE(string,select word from table,'');
my query is
set #test = 'this is my test query';
DROP TABLE IF EXISTS `test`;
CREATE TEMPORARY TABLE IF NOT EXISTS test(word longtext );
insert into test values('my');
insert into test values('test');
select replace(#test,(select word from test),'');
I have got error of sub query should returns 1 row
select replace(#test,word,'') from test;
...
select #test:=replace(#test,word,'') from test;
However keep in mind that you have multiple entries in the test table, #test will end up with all the words in table test replaced with ''. If that is what you want then all okay.
If you want a list output with #test with different words replaced use the first query and parse it in whatever you are passing the data to.
Maybe there's a better way but try this procedure
CREATE PROCEDURE myProc(IN teststring VARCHAR(255),
OUT teststring_new varchar(255))
BEGIN
DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
SELECT COUNT(*) FROM test INTO n;
SET teststring_new = teststring;
SET i=0;
WHILE i<n DO
SELECT replace(teststring_new,(SELECT word FROM test LIMIT 1 OFFSET i),'') INTO teststring_new;
SET i = i + 1;
END WHILE;
SELECT teststring_new;
END//
to call this
set #test = 'this is my test query';
call myProc(#test, #outvalue)
I think the following will work in MySQL:
select #test = replace(#test, t.word, '')
from test t;
Do note that this works for a string in a variable. It doesn't readily generalize to a column of such strings in a table.

How to retrieve return value and resultset from Stored Proc using Linq To SQL?

Consider a stored procedure like this:
create procedure p1 as
begin
declare #n1 int;
select #n1=count(*)
from Employee;
select top 100 *
from Employee;
return #n1;
end
How can I capture #n1 as well as the Employee resultset using Linq To SQL in C#?
You need an OUTPUT parameter
create procedure p1(#n1 int =0 OUTPUT ) as
begin
select
#n1=count(*)
from
Employee
select top 100
*
from
Employee
end
--And remember, you have to indicate a parameter is for output when you execute the proc
Declare #val int
exec p1 #val OUTPUT
SOLVED:
//I can access the resultset as following:
var emps = dataContext1.p1;
//I can get to #n1 by this:
int i1=int(emps).ReturnValue;