Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Declare #Random int = 1, #Bool bit = 0;
WHILE (#Bool = 0)
BEGIN
SET #Random = ROUND(RAND()*(SELECT MAX(CharID) FROM SRO_VT_SHARD_INIT.dbo._Char where LastLogout < DATEADD(DAY, -3, CURRENT_TIMESTAMP),0)
IF exists (SELECT CharID FROM SRO_VT_SHARD_INIT.dbo._Char WHERE CharID = #Random)
BEGIN
SET #Bool = 1 /*true*/
END
END
print #Random
It gives and error after the CURRENT_TIMESTAMP it says that there is an syntax error near the comma. If I remove the ,0 then the ROUND function doesn't have enough arguments. Someone?
Change
CURRENT_TIMESTAMP),0)
to
CURRENT_TIMESTAMP)),0)
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
My number needs to be 8 digits long, however If its less than 8 digits long I need to add trailing zeros to the it.
Example: 1234
Desired result: 12340000
I tried this at first:
DECLARE #YourNumber VARCHAR(8)=1234567;
SELECT DISTINCT
LEFT('00000000'+CAST(ISNULL(#YourNumber,0) AS VARCHAR),8)
However the result is: 00000000
I have the same read as #Hogan +1. I just tend to opt for concat(). No need to test for nulls or even care if the value is a string or int
Example
Select IfInt = left(concat(1234 ,'00000000'),8)
,IfStr = left(concat('1234','00000000'),8)
,IfNull= left(concat(null ,'00000000'),8)
Results
IfInt IfStr IfNull
12340000 12340000 00000000
If what you are asking for is actually what you want then try this:
DECLARE #YourNumber VARCHAR(8)='1234567';
SELECT DISTINCT
LEFT(CAST(ISNULL(#YourNumber,0) AS VARCHAR)+'00000000',8)
Since you are starting with a "number" in a string (DECLARE #YourNumber VARCHAR(8)=1234567;) there is no need to use cast. You can simply add the required number of zeroes:
DECLARE #YourNumber VARCHAR(8)= '1234567'; -- Using a string rather than an Int literal.
select #YourNumber + Replicate( '0', 8 - Len( #YourNumber ) ) as PaddedString;
Aside: It is a best practice to always specify the length of strings, i.e. CAST(ISNULL(#YourNumber,0) AS VARCHAR(8)).
All you have to do is move the "+ '00000000'" portion of the code to the right side of the Cast Function.
DECLARE #YourNumber VARCHAR(8)=1234567;
SELECT DISTINCT
LEFT(CAST(ISNULL(#YourNumber,0) AS VARCHAR)+'00000000',8)
Resulting in final value of: 12345670
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I'm getting the following error for this simple piece of SQL code. Any ideas why? Any BEGIN ... END block causes this error to occur
/* SQL Error (156): Incorrect syntax near the keyword 'END'. */
DECLARE #PROCESSEDCOUNT INT = 0
WHILE (#PROCESSEDCOUNT < 10)
BEGIN
SET #PROCESSEDCOUNT +=1
IF 1=1
BEGIN
-- Stuff happens here
END
END
This is running on SQL Server - MSSQL15
You must put something in between the BEGIN and END block
For example:
DECLARE #PROCESSEDCOUNT INT= 1
WHILE (#PROCESSEDCOUNT < 10)
BEGIN
SET #PROCESSEDCOUNT +=1
IF 1=1
BEGIN
PRINT '1'
END
END
You need more than a comment in a begin/end block.
Try the following
IF 1=1
BEGIN
SELECT #PROCESSEDCOUNT
END
You don't have any condition for the inner BEGIN...END. You can remove it.
DECLARE #PROCESSEDCOUNT INT = 0
WHILE (#PROCESSEDCOUNT < 10)
BEGIN
SET #PROCESSEDCOUNT +=1
-- Stuff happens here
END
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.
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.
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;