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

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 ','.

Related

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

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

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".

Msg 8115, Level 16, State 2, Line 2 Arithmetic overflow error converting expression to data type int

I am getting an error after firing this query.
select COUNT(*)
from [DB1].dbo.Transaction(nolock) t
join [DB2].dbo.visits (nolock) v
on t.V_ID=v.V_ID
where t.Ct_ID=11
and t.Timestamp>'06-08-2015'
and v.C_ID is null
Error:
Msg 8115, Level 16, State 2, Line 2 Arithmetic overflow error converting expression to data type int
Try using COUNT_BIG instead of COUNT.
Read this article for more information. Additionally, this shows the limits for the different types of INTs.

Max NVARCHAR length

I am waiting a stored procedure that creates a script more than 4000 characters in SQL Server 2000. I am using NVARCHAR (4000) but when I use NVARCHAR (MAX) I am getting this error.
What type can I use for this …. ?
Msg 170, Level 15, State 1, Procedure sp_Sxxxx, Line 19
Line 19: Incorrect syntax near 'MAX'.
Msg 137, Level 15, State 1, Procedure sp_ Sxxxx, Line 109
Must declare the variable '#ExecuteScript'.
Msg 137, Level 15, State 2, Procedure sp_ Sxxxx, Line 113
Must declare the variable '#ExecuteScript'.
The MAX keyword is new to SQL Server 2005 and above which is why you are receiving the syntax error. Since you are using SQL Server 2000 you will want to use text, ntext or image data types. Have a look at the docs:
http://msdn.microsoft.com/en-us/library/aa174534(v=sql.80).aspx

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