ORDERBY 'Incorrect Syntax' in Declare Statement [closed] - sql

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
I'm trying to do such a simple task, yet it's driving me crazy how it keeps failing.
DECLARE #neighbour HierarchyId = (SELECT [stPath] as tmpPath
FROM [DEV].[tmp].[StrategyTable] t
WHERE ParentCode = 'TOP')
ORDER BY t.stKey DESC;
I keep getting an error saying
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'ORDER'.
At the same time, it works for something WITHOUT a where clause; like
DECLARE #parent HierarchyId = (SELECT
[stPath] AS tmpPath
FROM [DEV].[tmp].[StrategyTable] t WHERE [ParentCode] = 'TOP')
WTF.......

DECLARE #neighbour HierarchyId = (SELECT TOP 1 [stPath] as tmpPath
FROM [DEV].[tmp].[StrategyTable] t
WHERE ParentCode = 'TOP'
ORDER BY t.stKey DESC);
This should accomplish what you want.

Related

How to fix error in sql "Execution finished with errors. Result: near "(": syntax error"? [duplicate]

This question already has an answer here:
Replacement for Left command in SQLite SQL
(1 answer)
Closed 7 months ago.
How to remove all characters from / to &.
UPDATE bal SET Perevod=substr(Perevod, INSTR(Perevod, '/.+&')-1)
WHERE INSTR(Perevod, '/.+&')>0;
You can use this because I believe there is no LEFT function in SQLite:
UPDATE bal
SET Perevod = substr(Perevod, 1, INSTR(Perevod, '/')-1)
WHERE INSTR(Perevod, '/')>0;
DEMO

Incorrect syntax near 'C' [duplicate]

This question already has answers here:
Why can't I use an alias in a DELETE statement?
(2 answers)
Closed 2 years ago.
Below is the section of code that is getting an error. I am not sure why I am getting an error.
Delete from TblProcessCurrency C
where Not Exists (
Select PRCSchedule
from tblProcess P
where P.PrcSchedule = C.PCXSchedule and P.PRCOlsn = C.PCXOlsn and P.PRCRelease = C.PCXRelease
);
and here is the error I am getting
Msg 102, Level 15, State 1, Line 32
Incorrect syntax near 'C'.
You want:
DELETE C
FROM TblProcessCurrency C
WHERE Not Exists...
You can't alias the table that is the target for the DELETE, unless you state you want to DELETE from said alias.

I get error Conversion failed when converting date and/or time from character string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have search in others topics related and i couldn't get it.
I have this query :
SELECT *
FROM
(SELECT TOP 10
[ID] AS [DOCUMENTID],
REPLACE([ImagePath], '\', '/') AS [FILENAME],
9 AS TEMPLATEID,
NULL AS ENCODEDRESULT,
CASE
WHEN QA.DBValue IS NULL THEN QA.Value
ELSE QA.DBValue
END AS RESULT,
'Finished' AS [STATUS],
QA.X AS [RLEFT],
QA.Y AS [RTOP],
QA.W AS [RWIDTH],
QA.H AS [RHEIGHT],
1 AS [QASTATUS],
QA.[ASDTYPE] AS [ASDTYPE],
QA.[DNNVALUE] AS [DNNRESULT],
QA.[DNNSCORE] AS [DNNSCORE],
CAST(QA.[ITEMDATE] AS DATE) AS [SCANDATE]
FROM
[QA].[QAItems] QA WITH(NOLOCK)
WHERE
QA.[PROCESSORTYPE] = 'F09'
AND (QA.[DBVALUE] IS NOT NULL OR QA.[VALUE] IS NOT NULL)
AND (QA.[DBVALUE] != 'null' OR QA.[VALUE] != 'null')
AND QA.[DBVALUEPROCESSED] = 1) sub
WHERE
CAST(sub.RESULT as date) >= '2019-06-01'
I get an error
Conversion failed when converting date and/or time from character string.
I think is in the cast execution but I can't figure why or how to solve it.
The data type for Result is nvarchar. I think i have done the wrong condition.
I think the problem with the case expression :
CASE WHEN QA.DBValue IS NULL
THEN QA.Value
ELSE QA.DBValue
END AS RESULT
CASE expression would return single type data. So, QA.Value & QA.DBValue should have a same type data.
However, you can simply transit it with COALESCE() :
COALESCE(QA.DBValue, QA.Value)
Try:
where convert(date,sub.RESULT) = convert(date,'2019-06-01')

Can't join and select in Sequel -- PG::SyntaxError [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 last month.
Improve this question
I'm trying to rename a column at join:
# ...............
result = DB[:my_items1].join(:my_items2, id: :my_item2_id).
select([Sequel[:my_items2][:name].as(:my_items_name),
Sequel[:my_items2][:amount].as(:my_item2_amount)
])
# ...............
Exception:
Sequel::DatabaseError - PG::SyntaxError: ERROR: syntax error at or near "AS"
LINE 1: SELECT ("my_items2"."name" AS "my_item2_name", "...
^
:
Dataset#select takes multiple arguments, not a single array:
result = DB[:my_items1].join(:my_items2, id: :my_item2_id).
select(Sequel[:my_items2][:name].as(:my_items_name),
Sequel[:my_items2][:amount].as(:my_item2_amount)
)

update the sql table with some condition [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want to update my whole sql table except some rows. Is it possible?
I am trying with this query but not work.
UPDATE `boon_rise`.`users`
SET `zip` = '0'
WHERE `users`.`id`
!IN ('5','6','7')
It seems like you meant to use the not in operator:
UPDATE `boon_rise`.`users`
SET `zip` = '0'
WHERE `users`.`id` NOT IN ('5', '6', '7')
-- Here ------------^
Use NOT IN instead of !IN
UPDATE `boon_rise`.`users`
SET `zip` = '0'
WHERE `users`.`id`
NOT IN('5','6','7')
Yes it is possible :
Update 'boon_rize'.'users'
Set 'zip'='0'
Where 'users'.'id'
Not In ('5','6','7')
Good Luck.