How to print diamon in sql - sql

enter image description here
I wanted to draw a dimaond like this in Sql. Can you help me please

I have find this code in queryexamples.com there are to many examples like this
Declare #i int = 0,#max int = 11,#c int = 1
While (#i<#max)
Begin
Print space(abs((#max-#c)/2))+ Replicate('X',#c)
Set #i = #i + 1
if(#i>(#max/2))
Set #c -= 2
else
Set #c += 2
End

Related

How to convert syntax from Foxpro to SQL Server

LOCAL in_txt
in_txt='mohammad'
txt_len=LEN(in_txt)
rev_txt=''
FOR ii=1 TO txt_len
w_chr=SUBSTR(in_txt,ii,1)
w_asc=IIF(ASC(w_chr)=32,32,ASC(w_chr)+1)
* ?' #'+w_chr+'='+CHR(w_asc)+'*'+STR(ASC(w_chr))+'>'+STR(ASC(w_chr)+1)+'*'+CHR(ASC(w_chr))+'>'+CHR(ASC(w_chr)+1)
rev_txt=rev_txt+ CHR(w_asc)
ENDFOR
return rev_txt
I understand solve my problem * character is using comment in foxpro.
Here is the equivalent SQL
DECLARE #in_txt varchar(100)
DECLARE #txt_Len int
DECLARE #rev_txt varchar(100)=''
DECLARE #i int=0
DECLARE #w_chr char(1)
DECLARE #w_asc int
SET #in_txt='mohammad'
SET #txt_len=LEN(#in_txt)
WHILE #i < #txt_len
BEGIN
SET #i = #i + 1
SET #w_chr=SUBSTRING(#in_txt,#i,1)
SET #w_asc = ASCII(#w_chr)
IF #w_asc <> 32 SET #w_asc=#w_asc+1
PRINT '#'+#w_chr+'='+CHAR(#w_asc)+'*'+STR(ASCII(#w_chr))+'>'+STR(ASCII(#w_chr)+1)+'*'+CHAR(ASCII(#w_chr))+'>'+CHAR(ASCII(#w_chr)+1)
SET #rev_txt = #rev_txt + CHAR(#w_asc)
END
PRINT #rev_Txt
It appears to be a very simple encryption of the text. The Print statement in the loop (which is the equivalent of the ? in Foxpro) is the debugger statement
It is a badly written piece of VFP code. * marks the line as a comment and basically it is replacing all the characters in the string except the char 32 (SPACE) with the next in ASCII chart. That is not something you could easily convert in MS SQL though, because in VFP a string could have any character in it including char(0). Assuming you don't have char 0:
Declare #in_txt varbinary(MAX);
DECLARE #rev_txt varbinary(MAX);
set #in_txt = CAST('mohammad' AS VARBINARY(MAX));
declare #i int;
declare #w_char int;
set #i = 1;
SET #rev_txt = CAST('' AS VARBINARY(MAX));
WHILE #i <= Len(#in_txt)
Begin
set #w_char = ASCII(SUBSTRING(#in_txt,#i,1));
SET #rev_txt = #rev_txt +
CAST(#w_char + case when #w_char = 32 then 0 else 1 end AS BINARY(1));
SET #i = #i +1;
END
SELECT #rev_txt;

How to compare two strings based on percent match in SQL

I want to post a solution to a interesting problem I was facing in T-SQL.
The problem:
Compare two string fields based on a percent match.
In addition, the two strings may have the words in them translocated.
For example: "Joni Bravo" and "Bravo Joni". These two strings should return a match of 100%, which means that position is not relevant. Few more things worth noting are that this code is made to compare strings that have space as delimiter in them. If the first string doesnt have space the match is set to 100% without actual check. This was not developed, because the strings this function is ment to compare always contain two or more words. Also, it is written on MS SQL Server 2017 if that mathers.
So here is the solution, hope this helps anyone :)
gl
/****** Object: UserDefinedFunction [dbo].[STRCOMP] Script Date: 29/03/2018 15:31:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[STRCOMP] (
-- Add the parameters for the function here
#name_1 varchar(255),#name_2 varchar(255)
)
RETURNS float
AS
BEGIN
-- Declare the return variable and any needed variable here
declare #p int = 0;
declare #c int = 0;
declare #br int = 0;
declare #p_temp int = 0;
declare #emergency_stop int = 0;
declare #fixer int = 0;
declare #table1_temp table (
row_id int identity(1,1),
str1 varchar (255));
declare #table2_temp table (
row_Id int identity(1,1),
str2 varchar (255));
declare #n int = 1;
declare #count int = 1;
declare #result int = 0;
declare #total_result float = 0;
declare #result_temp int = 0;
declare #variable float = 0.0;
--clean the two strings from unwanted symbols and numbers
set #name_1 = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(#name_1,'!',''),' ',' '),'1',''),'2',''),'3',''),'4',''),'5',''),'0',''),'6',''),'7',''),'8',''),'9','');
set #name_2 = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(#name_2,'!',''),' ',' '),'1',''),'2',''),'3',''),'4',''),'5',''),'0',''),'6',''),'7',''),'8',''),'9','');
--check if the first string has more than one words inside. If the string does
--not have more than one words, return 100%
set #c = charindex(' ',substring(#name_1,#p,len(#name_1)));
IF(#c = 0)
BEGIN
RETURN 100.00
END;
--main logic of the operation. This is based on sound indexing and comparing the
--outcome. This loops through the string whole words and determines their soundex
--code and then compares it one against the other to produce a definitive number --showing the raw match between the two strings #name_1 and #name_2.
WHILE (#br != 2 or #emergency_stop = 20)
BEGIN
insert into #table1_temp(str1)
select substring (#name_1,#p,#c);
set #p = len(substring (#name_1,#p,#c))+2;
set #p = #p + #p_temp - #fixer;
set #p_temp = #p;
set #c = CASE WHEN charindex(' ',substring(#name_1,#p,len(#name_1))) = 0 THEN len(#name_1) ELSE charindex(' ',substring(#name_1,#p,len(#name_1))) END;
set #fixer = 1;
set #br = CASE WHEN charindex(' ',substring(#name_1,#p,len(#name_1))) = 0 THEN #br + 1 ELSE 0 END;
set #emergency_stop = #emergency_stop +1;
END;
set #p = 0;
set #br = 0;
set #emergency_stop = 0;
set #fixer = 0;
set #p_temp = 0;
set #c = charindex(' ',substring(#name_2,#p,len(#name_2)));
WHILE (#br != 2 or #emergency_stop = 20)
BEGIN
insert into #table2_temp(str2)
select substring (#name_2,#p,#c);
set #p = len(substring (#name_2,#p,#c))+2;
set #p = #p + #p_temp - #fixer;
set #p_temp = #p;
set #c = CASE WHEN charindex(' ',substring(#name_2,#p,len(#name_2))) = 0 THEN len(#name_2) ELSE charindex(' ',substring(#name_2,#p,len(#name_2))) END;
set #fixer = 1;
set #br = CASE WHEN charindex(' ',substring(#name_2,#p,len(#name_2))) = 0 THEN #br + 1 ELSE 0 END;
set #emergency_stop = #emergency_stop +1;
END;
WHILE((select str1 from #table1_temp where row_id = #n) is not null)
BEGIN
set #count = 1;
set #result = 0;
WHILE((select str2 from #table2_temp where row_id = #count) is not null)
BEGIN
set #result_temp = DIFFERENCE((select str1 from #table1_temp where row_id = #n),(select str2 from #table2_temp where row_id = #count));
IF(#result_temp > #result)
BEGIN
set #result = #result_temp;
END;
set #count = #count + 1;
END;
set #total_result = #total_result + #result;
set #n = #n + 1;
END;
--gather the results and transform them in a percent match.
set #variable = (select #total_result / (select max(row_count) from (
select max(row_id) as row_count from #table1_temp
union
select max(row_id) as row_count from #table2_temp) a));
RETURN #variable/4 * 100;
END
GO
PS: I decided to write it in a user-defined function just for the needs of my project.

SQL: How to display rhombus shape based on given length of characters?

My requirement is to build a rhombus using ‘X’ symbol based on below conditions.
If the length is 2, then it should display
X
XXX
X
If the length is 3, then it should display
X
XXX
XXXXX
XXX
X
If the length is 5, then it should display 5 lines above and 5 lines below with spaces.
I tried writing code, but I’m unable to get lower half part and spaces. Can someone help me here?
declare #X nvarchar(1) = 'X'
declare #chars int = 5
declare #W nvarchar(100) = 1
while (#chars > 0)
begin
print replicate(#X,#W)
set #chars = #chars - 1
set #W = #W + 2
end
Output:
X
XXX
XXXXX
XXXXXXX
XXXXXXXXX
You can get spaces using SPACE function. I have used your approach and added some code which returns lower part as well.
DECLARE #X NVARCHAR(1) = 'X'
DECLARE #chars INT = 5
DECLARE #LowerPart INT = #chars
DECLARE #W NVARCHAR(100) = 1
WHILE (#chars > 0)
BEGIN
PRINT SPACE(#chars)+REPLICATE(#X,#W)
SET #chars = #chars - 1
SET #W = #W + 2
END
WHILE (#chars <= #LowerPart)
BEGIN
PRINT SPACE(#chars)+REPLICATE(#X,#W)
SET #chars = #chars + 1
SET #W = #W - 2
END
I've considered your example rhombus for 3. So, for a given length 5, there will be only 4 lines above and 4 lines below with my solution.
declare #displayCharacter nvarchar(1) = 'X'
declare #rhombusLength int = 5
declare #repetition int = 1
declare #topRow int = 1
declare #bottomRow int = 1
--Top rows
while (#rhombusLength > #topRow)
begin
print REPLICATE(' ', #rhombusLength - #topRow) + REPLICATE(#displayCharacter, #repetition)
set #topRow = #topRow + 1
set #repetition = #repetition + 2
end
--Middle row
print REPLICATE(#displayCharacter, #repetition)
--Bottom rows
while (#rhombusLength > #bottomRow)
begin
set #repetition = #repetition - 2
print REPLICATE(' ', #bottomRow) + REPLICATE(#displayCharacter, #repetition)
set #bottomRow = #bottomRow + 1
end

How to write this Array in SQL

My code in VBA Access is:
Dim K(10)
for j=1 to 5
K(j+1)= k(j)*2
next
How I write this in SQL?
Declare #k int = 10
Declare #j int = 1
While (#j<=5)
Begin
Set #k = #k * 2
print #k
Set #j = #j+1
End

Function to check the input number of words

Need help to create a function that returns TRUE or FALSE. TRUE - if type 1 or 3 words (like '__hello_', '_hello', '_hello my frend' - spaces should be cut), if the condition is not fulfilled FALSE
CREATE FUNCTION dbo.nazvFac(#f nvarchar(30))
RETURNS bit
AS
BEGIN
DECLARE #l int = 1, #s nvarchar(30), #i int = 0, #b bit
WHILE LTRIM(RTRIM(LEN(#f))) >= #l --error here, but I do not know how to fix it
BEGIN
SET #s = SUBSTRING(#f, #l, 1)
IF #s BETWEEN 'А' AND 'я'
SET #l += 1
ELSE IF #s = ' '
BEGIN
SET #l -= 1
SET #s = SUBSTRING(#f, #l, 1)
SET #s = RTRIM(#s)
SET #l += 2
SET #i += 1
END
ELSE
BREAK
END
IF #i = 0 OR #i = 2
SET #b = 'TRUE'
ELSE
SET #b = 'FALSE'
RETURN #b
END
GO
WHILE LTRIM(RTRIM(LEN(#f))) >= #l --error here, but I do not know how to fix it
LEN() returns an int, which you are then passing to a string function: RTRIM().
You want to return TRUE only if there are one or three words? This should do it:
CREATE FUNCTION dbo.nazvFac(#f NVARCHAR(30))
RETURNS BIT
AS
BEGIN
DECLARE #l INT, #b BIT
SET #l = LEN(#f) - LEN(REPLACE(#f, ' ', '')) + 1
IF #l == 1 OR #l == 3
SET #b = 'TRUE'
ELSE
SET #b = 'FALSE'
RETURN #b
END
Also, JC. is right about the len() error.
You should trim the string and then check the length.
CREATE FUNCTION dbo.nazvFac(#f NVARCHAR(30))
RETURNS BIT
AS
BEGIN
DECLARE #l INT = 1, #s NVARCHAR(30), #i INT = 0, #b BIT
WHILE LEN(LTRIM(RTRIM(#f))) >= #l --I think youn need to trim the string and then check length
BEGIN
SET #s = SUBSTRING(#f, #l, 1)
IF #s BETWEEN 'А' AND 'я'
SET #l += 1
ELSE IF #s = ' '
BEGIN
SET #l -= 1
SET #s = SUBSTRING(#f, #l, 1)
SET #s = RTRIM(#s)
SET #l += 2
SET #i += 1
END
ELSE
BREAK
END
IF #i = 0 OR #i = 2
SET #b = 'TRUE'
ELSE
SET #b = 'FALSE'
RETURN #b
END
GO