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

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

Related

Choose string between two chars that repeat multiple times in a string

I have a string '"&"abc1"#"1"&""abc2"#"2"&""abc3"#"3"&""abc1"#"4
I just want to extract each int into separate variables. Currently I have
note endtext = "#"
and endnum = "&"
WHILE #N < LEN(#TextField)
BEGIN
SET #read1 = (
SELECT SUBSTRING(#TextFieled,
CHARINDEX(#endtext, #TextFieled) + #N --Increment 2 so skips to next "
, CHARINDEX(#endnum,#TextFieled) - CHARINDEX(#endtext, #TextFieled)) --MANY CHARS TO GO FOR
)
SELECT #read1
SET #N = #N + CHARINDEX(#read1, #TextFieled) --next strating point
END
But it is not returning only numbers. I think the problem lies with the fact that the nums I want to pick are located in repeated strings, so I'm not sure how to alter my loop to skip.
For readability and debug-ability use variable to store CHARINDEX
DECLARE
#N int = 2,
#TextField varchar(100) = '"&"abc1"#"1"&""abc2"#"2"&""abc3"#"3"&""abc1"#"4',
#read1 varchar(10),
#endtext varchar(3) = '"#"',
#endnum varchar(3) = '"&"',
#Start int,
#Finish int
WHILE #N < LEN(#TextField)
BEGIN
SET #Start = CHARINDEX(#endtext, #TextField, #N)
SET #Finish =CHARINDEX(#endnum, #TextField, #N)
IF #Finish = 0 SET #Finish = LEN(#TextField) + 1
SET #read1 = (SELECT SUBSTRING(#TextField, #Start + 3, #Finish - #Start - 3))
SELECT #read1 xRead
SET #N = #Finish + 1 --next strating point
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

search in a string creditcard numeric value

I want to find a credit card numeric value in a sql string.
for example;
DECLARE #value1 NVARCHAR(MAX) = 'The payment is the place 1234567812345678'
DECLARE #value2 NVARCHAR(MAX) = 'The payment is the place 123456aa7812345678'
DECLARE #value3 NVARCHAR(MAX) = 'The payment1234567812345678is the place'
The result should be :
#value1Result 1234567812345678
#value2Result NULL
#value3Result 1234567812345678
16 digits must be together without space.
How to do this in a sql script or a function?
edit :
if I want to find these 2 credit card value.
#value4 = 'card 1 is : 4034349183539301 and the other one is 3456123485697865'
how should I implement the scripts?
You can use PathIndex as
PATINDEX('%[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]%', yourStr)
if the result is 0 then it doesnt containg 16 digits other was it contains.
It can be used withing a Where statement or Select statement based on your needs
You can write as:
SELECT case when Len(LEFT(subsrt, PATINDEX('%[^0-9]%', subsrt + 't') - 1)) = 16
then LEFT(subsrt, PATINDEX('%[^0-9]%', subsrt + 't') - 1)
else ''
end
FROM (
SELECT subsrt = SUBSTRING(string, pos, LEN(string))
FROM (
SELECT string, pos = PATINDEX('%[0-9]%', string)
FROM table1
) d
) t
Demo
DECLARE #value1 NVARCHAR(MAX) = 'card 1 is : 4034349183539301 and the other one is 3456123485697865'
DECLARE #Lenght INT
,#Count INT
,#Candidate CHAR
,#cNum INT
,#result VARCHAR(16)
SELECT #Count = 1
SELECT #cNum = 0
SELECT #result = ''
SELECT #Lenght = LEN(#value1)
WHILE #Count <= #Lenght
BEGIN
SELECT #Candidate = SUBSTRING(#value1, #Count, 1)
IF #Candidate != ' '
AND ISNUMERIC(#Candidate) = 1
BEGIN
SET #cNum = #cNum + 1
SET #result = #result + #Candidate
END
ELSE
BEGIN
SET #cNum = 1
SET #result = ''
END
IF #cNum > 16
BEGIN
SELECT #result 'Credit Number'
END
SET #Count = #Count + 1
END
There you go kind sir.
DECLARE
#value3 NVARCHAR(MAX) = 'The payment1234567812345678is the place',
#MaxCount int,
#Count int,
#Numbers NVARCHAR(100)
SELECT #Count = 1
SELECT #Numbers = ''
SELECT #MaxCount = LEN(#value3)
WHILE #Count <= #MaxCount
BEGIN
IF (UNICODE(SUBSTRING(#value3,#Count,1)) >= 48 AND UNICODE(SUBSTRING(#value3,#Count,1)) <=57)
SELECT #Numbers = #Numbers + SUBSTRING(#value3,#Count,1)
SELECT #Count = #Count + 1
END
PRINT #Numbers
You can make this as a function if you are planning to use it a lot.

SQL Sum of each character in a number

I want to sum each character of an integer in SQL
For instance. I have 16273481 as INT
But now (Without to complicated methods) sum
1 + 6 + 2 + 7 + 3 + 4 + 8 + 1 = 32
DECLARE #someInt INT = 16273481
-- you could put this all into a function
-- and then it would be reusable...
--
-- like... SELECT SumOfIndividualIntegers(16273481)
DECLARE #count INT = LEN(#someInt),
#counter INT = 1
DECLARE #Sum INT = 0
WHILE #counter <= #count
BEGIN
SELECT #sum += CAST(SUBSTRING(CAST(#someInt AS VARCHAR), #counter, 1) AS int)
SELECT #counter += 1
END
SELECT #sum --32
-- and then you would RETURN #sum instead
Would using the remainder operator be suitable for your situation with a loop?
Pseuodo code:
x = 16273481;
sum = 0;
Loop:
sum = sum + (x % 10);
x = (x / 10);
Something along those lines?

SQL Server concatenation varchar and int

I have two variables:
One is varchar and one is int, I am not sure how to write a while loop using casting so that it will show the following: for example if the while loop is 5 then the result should be
Meter 1
Meter 2
Meter 3
Meter 4
Meter 5.
I have this code, but it is not running (cannot convert varchar to int) even when i do the casting it does not work.
DECLARE #Name varchar (20) = 'Meter',
#MeterNumber int = 1
WHILE (#MeterNumber < 5)
BEGIN
PRINT #Name + ' ' + #MeterNumber
SET #MeterNumber = #MeterNumber + 1
END
This should do the trick: cast the MeterNumber to a varchar when you print it.
DECLARE #Name varchar (20) = 'Meter',
#MeterNumber int = 1
WHILE (#MeterNumber <= 5)
BEGIN
PRINT #Name + ' ' + cast(#MeterNumber as varchar(20))
SET #MeterNumber = #MeterNumber + 1
END
EDIT:
For example I want to declare a third variable that would store each
iteration. Meter 1, Meter 2... but I am not sure where to place it!
DECLARE #Name varchar (20) = 'Meter',
#MeterNumber int = 1 ,
#OutPut varchar(max) =''; -- this can get biiiig.
WHILE (#MeterNumber <= 5)
BEGIN
SET #Output = #Output + #Name + ' ' + cast(#MeterNumber as varchar(20)) + ','
PRINT LEFT(#Output, len(#Output) - 1)
SET #MeterNumber = #MeterNumber + 1
END
Try this one (without LOOP) -
DECLARE
#Name VARCHAR(20) = 'Meter'
, #MeterNumber INT = 5
, #OutPut VARCHAR(MAX) = ''
SELECT #OutPut = STUFF((
SELECT CHAR(13) + #Name + ' ' + CAST(sv.number AS VARCHAR(5))
FROM [master].dbo.spt_values sv
WHERE sv.[type] = 'p'
AND sv.number BETWEEN 1 AND #MeterNumber
FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)'), 1, 1, '')
PRINT #OutPut