SQL query is throwing this error Msg 102, Level 15, State 1, Line 1 Incorrect syntax near ' ' - sql

Query is :
select * from Test.dbo.Test_Spoc where spocNo = 54986
In SQL 2008 its throwing error Incorrect syntax near ' '. IF I remove the space before Where and again give the space its working.
In SQL 2012 this query is showing red sign also before Where that problem is in this space.
I want to know what can be reason that earlier space is not working?

select * from Test.dbo.Test_Spoc where spocNo = '54986'
Add single quotes, I think this might be the issue; you check the code

Related

Simple conversion of SQL date field to mm/dd/yyy

I'm having a problem converting three columns to mm/dd/yyyy format in MS SQL Server.
There are a number of methods I've looked up here but nothing that quite worked when I tried to implement it. It's a very basic set of columns I'm pulling.
SELECT
[Incident_Number]
,[Corporate_ID]
,[Assignee_Login_ID]
,select *, cast ([Submit_Date] as date) <TIME_CREATED> from SERVICEMGMT where date = 'mm/dd/yyyy'
,[Last_Resolved_Date] as TIME_RESOLVED
,[Closed_Date] as TIME_CLOSED
,[Description]
FROM [SERVICEMGMT].[dbo].[Help_Desk]
Where (corporate_id LIKE ('b%') AND TEMPLATE_NAME = ('PC: Local Build/Deployment') AND STATUS > 3);
I'm getting a basic incorrect syntax error. The actual error messages are:
Msg 156, Level 15, State 1, Line 5 Incorrect syntax near the keyword 'select'.
Msg 102, Level 15, State 1, Line 6 Incorrect syntax near ','.

Msg 156, Level 15, State 1, Line 4 Incorrect syntax near the keyword 'between'

I keep getting a error message when I try to use the between operator in SQL Server 2012.
select *
from Sales.Store
where SalesPersonID>283
order by Name between 'g' and'j' desc;
Error:
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'between'.
I'm not sure what I'm doing wrong. Has anyone seen this? Any suggestions
SQL Server doesn't allow you to order by a Boolean you can use
select *
from Sales.Store
where SalesPersonID>283
order by IIF(Name between 'g' and'j', 0,1);
But between probably doesn't have the semantics you want anyway.
You could use IIF(Name LIKE '[g-j]%',0,1) instead if you want to include all names beginning with j in the group ordered first.
But after your comment this is what you need
select *
from Sales.Store
where SalesPersonID>283 AND Name LIKE '[g-j]%';
You should not use between as that would only include names that were exactly "j" not all those beginning with "j".

SQL Casting Error (Updated Post)

New Question I have researched the web on using the cast feature in SQL. I followed same format as the examples, but still doesn't work. Any suggestions? It gives me the same error messages as below.
This is what I am trying to do:
update dbo.TableName
set cast(columnName1 as decimal (9,1)) = 10,
cast(columnName2 as decimal (9,1)) = 11
where ... etc
Previous Post:
When compiling my code, I get this error message: "Microsoft.Rules.Data: Data loss might occur when casting from Integer to Decimal(9,1)." So I put a basic cast in there (see below) and for some reason it doesn't work. Any suggestions? I get the following error messages:
-- 1). SQL 80001: Incorrect syntax near'0'. Expecting '(', or SELECT.
-- 2). SQL 80001: Incorrect syntax near '9'. Expecting '(', or SELECT.
-- 3). SQL 80001: Incorrect syntax near ';'. Expecting CONVERSION.
-- 4). SQL 46010: Incorrect syntax near 0.
exec dbo.Name
#variable1,
cast((0) as decimal(9,1)),
#variable2;
You don't need to cast it. If you use
0.0
instead of
0
then you will already have a decimal number representing zero.

SQL Case statement error Msg 8114

Trying to build a simple case statement using SQL Server 2008 and running across an issue giving me the following error message:
Msg 8114, Level 16, State 5, Line 6
Error converting data type varchar to numeric.
I've isolated the script causing the issue to the following:
CASE WHEN tPersonLocationHist.LocationCode = 'DC'
AND (tPersonJobHist.FullTimeEquivalent = 1)
AND (MONTH(tPersonStatusHist.LatestHireDate) < 10)
THEN tPersonStatusHist.NormalHoursPerWeek / 5
The tPersonStatusHist.NormalHoursPerWeek is formatted as decimal; however, I can't get it to calculate.
Any tips? The resulting calculation needs to be in decimal form (to two decimal digits).
Even if I change the THEN statement to just '7.5', it then returns:
Msg 245, Level 16, State 1, Line 6
Conversion failed when converting the varchar value '7.5' to data type int.
I've tried using CONVERT(decimal(4,2),tPersonJobHist.NormalHoursPerWeek * 7.5), but no luck on that either.
Only thing that's working is to do it as:
CONVERT(int,tPersonJobHist.NormalHoursPerWeek * 7.5), but then it is dropping the decimals and just giving the whole integer.
As you can probably tell, I'm new into SQL and still learning the rope, so any help you can give is much appreciated.
Are your other cases/else returning varchar? They should all return the same datatype. You may want to cast/convert them to be sure.
CAST(CONVERT(decimal(4,2),tPersonJobHist.NormalHoursPerWeek * 7.5) AS varchar(50))
First convert to decimal then do the division:
THEN CONVERT(decimal(4,2), tPersonStatusHist.NormalHoursPerWeek) / 5
Or cast is an option:
THEN CAST(tPersonStatusHist.NormalHoursPerWeek AS decimal(4,2)) / 5

Incorrect syntax near '.' - very simple query

I have very simple query that calls a UDF which splits a field by comma. The query is
select top 10 * FROM Emails e WHERE EXISTS(SELECT TOP 1 1 FROM dbo.fn_Split(e.committees,','))
When I run/parse it, I get:
Msg 170, Level 15, State 1, Line 4
Line 4: Incorrect syntax near '.'.
I think it must have something to do with SQL 2000. If you switch out e.committees for something hardcoded (i.e., 'A,B,C,D') it works fine.
SQL 2000 doesn't support passing column names to TVFs. That was brought in in SQL2005 along with CROSS APPLY
I'm not really sure what you are needing to do here. Is e.committees a non 1NF list of numeric committee ids? If so
select top 10 <column-list>
FROM Emails e
WHERE e.committees
like '%[0-9]%'
ORDER BY ...
Might work but a better solution would be to store these in a normalised form in a table with columns email_id,committee_id or whatever. This will likely make your queries easier!