Splitting Strings then Creating Segments out of those. T-SQL [closed] - sql-server

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
How do I split a csv string into this format in SQL Server?
Initial String value (A, B, C, D) into :
A-B
B-C
C-D

You can try using string_split in conjunction with lead()
select value + '-' + lead(value) over (order by value) new_value
from string_split('A,B,C,D',',')
SQL FIDDLE:
http://sqlfiddle.com/#!18/0a28f/2607

Grab a copy of NGrams8K then you could simply do this.
DECLARE #string VARCHAR(100) = 'A, B, C, D';
SELECT TheString = CONCAT(ng.Token,'-',ng.Nxt)
FROM
(
SELECT ng.Token, Nxt = LEAD(ng.Token,1) OVER (ORDER BY ng.Position)
FROM dbo.ngrams8k(#string,1) AS ng
WHERE ng.Token LIKE '%[a-z]%'
) AS ng
WHERE ng.Nxt IS NOT NULL;
Returns:
TheString
---------------------
A-B
B-C
C-D
Order is guaranteed without a sort in the execution plan.

Related

Sum value of col 3 based on col 1 and col 2 value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to sum value of col3 based on col1 and col2
here is the link of dummy data sheet in which I have shown sample table according to which I want sum of col 3 please guys check the sheet and suggest me some solution for it.
https://docs.google.com/spreadsheets/d/1_MeiySJHI8OD84BPDOj_z57My-TXbs2ey4AOkQP3zug/edit?usp=sharing
use QUERY:
=QUERY(A:C; "select A,B,sum(C) where A is not null group by A,B label sum(C)''")
update:
={"result"; INDEX(IF(COUNTIFS(A2:A&" "&B2:B, A2:A&" "&B2:B, ROW(A2:A), "<="&ROW(A2:A))=1,
IFNA(VLOOKUP(A2:A&" "&B2:B,
QUERY({A2:A&" "&B2:B, C2:C},
"select Col1,sum(Col2) where Col1 is not null group by Col1 label sum(Col2)''"), 2, 0)), ))}

ISNULL Not working when I am calculating the sum of two columns in the expression part of the function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am trying to use ISNULL (), but in the expression part of the function, instead of having a column name as the argument,I have the sum of four columns, and it should return a 0 when all of the value in the column is NULL, else the sum. for e.g., if three columns have values and corresponding value in the other column is Null, it should still return the sum of the three values. This is how I have written my query:
Select ISNULL([FY18 P1]+[FY18 P2]+[FY18 P3]+[FY18 P4],0) as [Previous YTD]
from TableA
This calculated column inside the ISNULL function is not working. Can anybody help me rewrite this expression so that it will work. What i mean when it is not working is that, it is returning a NULL when only one column is NULL but the rest of the columns have a value. Basically it should return the sum and not NULL in this case.
If any column in your concatenation IS NULL then the result will be NULL.
You need to wrap each column in IS NULL to make this column value 0 so that your addition doesn't return NULL.
SELECT ISNULL([FY18 P1],0) +ISNULL([FY18 P2],0) + ISNULL([FY18 P3],0) + ISNULL([FY18 P4],0)
This is because anything + NULL returns NULL
select 1 + 2 + 3 + NULL --returns `NULL`

Filtering conditional in WHERE clause [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have the PEN_TIPO column of a table, this column can have values 0 and 2, and in the report depending on the filter I apply the condition as follows:
declare #PEN_TIPO int = 0
(A.PEN_TIPO = #PEN_TIPO OR #PEN_TIPO = 0)
However, it will have a condition that I do not need to filter this field, ie I have to get 0 and 2 from the PEN_TIPO column.
How can I apply this filter?
It sounds like you're describing, what is commonly referred to as an optional parameter.
If the user enters a parameter value, they want to filter based on that, if not, ignore it altogether.
It would typically look something like this:
DECLARE #PEN_TIPO INT;
(A.PEN_TIPO = #PEN_TIPO OR #PEN_TIPO IS NULL)
OPTION(RECOMPILE);
Please note that I added "OPTION(RECOMPILE)" to the end of the query.
You'll want to add this to you query too, so that the optimizer can create an optimized plan based on the chosen parameter value.
Are you trying to do this ?
DECLARE #PEN_TIPO INT = NULL
SELECT *
FROM TableName
WHERE
A.PEN_TIPO = ISNULL(#PEN_TIPO, PEN_TIPO)
When #PEN_TIPO = NULL then A.PEN_TIPO = A.PEN_TIPO which will bring everything.
This is usually handled with a similar conditional where clause using NULL, but you code would if you defaulted the value to 1 or another value.
In the below proc, we default the parameter to NULL. It will remain NULL if your report / application doesn't pass in a value.
If it remains null, all rows are returned.
If a value is passed in that is 0 or 2, the filter is applied.
If a value is passed in that isn't 0 or 2, an error is raised.
Here's the proc.
create proc myProc (#PEN_TIPO int = NULL)
as
if (#PEN_TIPO IS NOT NULL) or (#PEN_TIPO NOT IN (0,2))
begin
raiserror('Invalid parameter',16,1)
end
SELECT A.*
FROM SomeTable A
WHERE (A.PEN_TIPO = #PEN_TIPO OR #PEN_TIPO IS NULL)
Aaron Bertrand has an in-depth post on these Kitchen Sink type queries.

Priority Queue in c linked list [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have created this program for priority queue and I am having a problem. I am getting the wrong output.
Here is the input:
Insert 10000 2
Insert 10000 2
Insert 10000 3
Insert 19444 9
Pop
Insert 10331 3
Pop
Pop
Pop
Pop
Pop
Here's what the output should be:
19444
10000
10331
10000
10000
-1
Here's the output which I get:
19444
10000
10000
10000
10331
-1
SOLVED !
I believe your priority checking logic is incorrect:
while (queue->next != NULL && queue->next->prior >= /* not <= */ priorty)
or better yet
while (queue->next != NULL && priorty <= queue->next->prior)
Not sure how you intend to handle the case where two elements have the same priority, but since your insert uses "greater than" to replace the head of the queue you probably want to keep the same logic.
You loop for inserting the node is incorrect, it should read:
while (queue->next != NULL && queue->next->prior >= priorty)
queue = queue->next;

Indexing Computed Column of 2 largest integers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
According to Sql Server, Any computed column can be indexed only when it is deterministic.
ie., consider columns a,b, and c are of INT datatype and c = a+b. Now column C can be indexed but **when column a or b holds the largest Integer value it will throw the Arithmetic Error, is there any solution ?
Thanks.
create table TA (
ID int not null primary key,
a int not null,
b int not null,
c as a+b
)
go
create index IX_TA_c on TA (c)
go
insert into TA(ID,a,b) values (1,1,2147483647)
The insert gets:
Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type int.
The statement has been terminated.
If you want the calculation to always work, change the calculation so that at least one of the columns is forced to be a bigint (and so the maths and the resultant type of c are all bigint also):
c as CAST(a as bigint)+b

Resources