Correct use of case (error with then) - sql

Can anyone understand why I am getting this error for the below line of code? Please let me know!
Msg 156, Level 15, State 1, Line 52
Incorrect syntax near the keyword 'then'.
Slight modification of this post: How do I add hyphens to this line of code? For instance the format I am getting is PTEE032981. How do I get PT-EE-032981?
Please let me know if this can be answered in this post.
case when (MU.Number like '%co-load%' then SUBSTRING(UPPER(REPLACE(Mu.Number,'-','')) ,
PATINDEX('%[nw]t[A-Z][A-Z][0-9][0-9][0-9][0-9][0-9]%',UPPER(REPLACE(Mu.Number,'-',''))),10))
else ME.MovementReference end as [MovementReference],

No need for a starting parenthesis:
case
when MU.Number like '%co-load%'
then SUBSTRING(UPPER(REPLACE(Mu.Number, '-', '')), PATINDEX('%[nw]t[A-Z][A-Z][0-9][0-9][0-9][0-9][0-9]%', UPPER(REPLACE(Mu.Number, '-', ''))), 10)
else ME.MovementReference
end as [MovementReference],

Related

How to solve the error_syntax error near '< '

Msg 102, Level 15, State 1, Line 3 Incorrect syntax near '<'.
I got the above error message every time I tried to execute the below query.
UPDATE [dbo].[FM1]
SET [Datum] = <Datum, smalldatetime,>
,[Gesamtzeit] = <Gesamtzeit, nvarchar(5),>
You can try this no need to specify datatype here.
UPDATE [dbo].[FM1]
SET [Datum] = 'YourValue'
,[Gesamtzeit] = 'YourValue'
where ...

SQL Error near the word 'go'

Why does SQL Server report that this statement isn't correct?
use DIGITECH
go
select *
from kunde as k
left join adresse as a on k.FK_AdID = a.AdID
where Name = 'Dirk'
go
SQL displays this error (in German):
Meldung 102, Ebene 15, Status 1, Zeile 14
Falsche Syntax in der Nähe von 'go'.
Meldung 102, Ebene 15, Status 1, Zeile 14
Falsche Syntax in der Nähe von 'go'.
Translated to english:
Msg 102 , Level 15 , State 1, Line 14
Incorrect syntax near 'go' .
Msg 102 , Level 15 , State 1, Line 14
Incorrect syntax near 'go' .
As other have pointed out, GO is the default batch delimiter for tools like Management Studio or sqlcmd. SQL Server does not understand GO, the tools use it to separate batches and send individual batches to SQL Server. You probably took an entire .sql file and executed in your app.
You can use a library like DbUtilSqlCmd which understands the sqlcmd delimiters (GO), and other sqlcmd specific syntax like :setvar, and execute your .sql file through it.
Can you specify the database in the query and avoid the go statements? For example:
select * from DIGITECH.dbo.kunde as k
left join DIGITECH.dbo.adresse as a
on k.FK_AdID = a.AdID
where Name = 'Dirk'

Xquery syntax error in SQL server 2012

I want to return the character 'o' in my sql database while working with xml data so I wrote the following query:
use master
select song_type.query ('table/[where o.name like % o %]')
from xmldata
but the program returned an error saying:
Msg 9341, Level 16, State 1, Line 2
XQuery [xmldata.song_type.query()]: Syntax error near '[', expected a step expression.
please how do I fix this.
Try this ...
select *
from xmldata where cast(convert(varchar(max),song_type) as xml).value("o.name[0]","varchar(500)") like '% o %'

SQL Sending attach query in email as .csv

I am trying to send an query as an attached .CSV in a email. Here is what I have.
EXEC msdb.dbo.sp_send_dbmail #recipients='bdorner#fascinations.net',
#profile_name= 'MyMailProfile',
#subject='new Items',
#body='New Items',
#query= 'select itemlookupcode, * from Item where
(datecreated BETWEEN DATEADD(DAY, - 100, CURRENT_TIMESTAMP) AND DATEADD(day, - 0, CURRENT_TIMESTAMP))
',
#attach_query_result_as_file=1
#query_attachment_filename = 'test.csv'
I keep getting a error Msg 102, Level 15, State 1, Line 11
Incorrect syntax near '#query_attachment_filename'.
I do have an email that send out a query in a body of an email. If that code will help let me know.
Thank you for the Help,
Brian
You are missing a comma :
#attach_query_result_as_file=1**,**
#query_attachment_filename = 'test.csv'
When you get a syntax error, always look for syntax problems in your query!
You're missing a comma after the #attach_query_result_as_file. That's why you get a syntax error pointing at the next line after.

inserting multiple values into a single cell using sql 2005

I have the typical table:
LSRNbr BatchNbr
111 1212
111 1414
And the query should return:
LSRNbr BatchNbr
111 1212, 1414
I was browsing for a solution to this and I found these two:
Solution 1:
;WITH C AS
(
SELECT LSRNbr, BatchNbr FROM tblDTS_LSRBatch
)
SELECT Distinct LSRNbr,
STUFF((SELECT ';' + BatchNbr FROM tblDTS_LSRBatch WHERE LSRNbr = c.LSRNbr FOR XML PATH('')),1,1,'')
FROM C
error:
Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near ';'.
Msg 170, Level 15, State 1, Line 7
Line 7: Incorrect syntax near 'XML'.
Solution 2:
SELECT
[LSRNbr], REPLACE(RTRIM((SELECT [BatchNbr] + ' ' FROM tblDTS_LSRBatch WHERE (LSRNbr = Results.LSRNbr ) FOR XML PATH (''))),' ',', ') AS NameValues
FROM tblDTS_LSRBatch Results
GROUP BY LSRNbr
error:
Msg 170, Level 15, State 1, Line 2
Line 2: Incorrect syntax near 'XML'.
But none of them worked for me, see errors above please.
What could be the problem here?
I'm using Microsoft SQL Server 2005
These are syntax errors.
You'll learn more from figuring out the particular error yourself:
Take a look at the syntax tree
Take a look at some good examples of what you're trying to do
If you still have trouble, feel free to ask more questions