SQL Server INSERT statements [duplicate] - sql-server

This question already has answers here:
Insert error, Incorrect syntax near '2' [closed]
(2 answers)
Closed 8 years ago.
This is my INSERT statement:
INSERT INTO Exercise 2 (Exercise, [Calories Burned])
VALUES ('Swimming', 500)
This is the error message that I receive:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '2'.

You have an invalid table name... you need to put brackets around it if there is a a space. Example: [Exercise 2]

Try this
INSERT INTO [Exercise 2] (Exercise, [Calories Burned]) VALUES ('Swimming', 500)

Related

Arithmetic overflow error for select 2678400*1393 in SQL

select 2678400 * 1393
When I run this query, I'm getting this error in SQL Server:
Msg 8115, Level 16, State 2, Line 23
Arithmetic overflow error converting expression to data type int.
Please help me to resolve this issue.
Since this result overflows the capacity of INT - use BIGINT instead:
SELECT CAST(2678400 AS BIGINT) * 1393

SQL-Server: Store Ω (Omega) in a nvarchar field [duplicate]

This question already has an answer here:
How to insert greek characters into sqlserver table
(1 answer)
Closed 6 years ago.
If I run
create table t (c nvarchar(10));
insert into t (c)
values ('Ω');
select * from t
the output is O
O
instead of Omega
Ω
I thought I could store almost every character in a nvarchar field.
You have to use N before the character:
insert into t (c)
values (N'Ω');

T-SQL IF statement embedded in a sum() function

I'm attempting to convert a MySQL query to a T-SQL query and the IF statement that's enclosed within a SUM statement is tripping me up. Any suggestions?
SELECT
CMTS_RQ.[Dated],
CMTS_RQ.CMTS_Name,
Count(CMTS_RQ.CMTS_Name) AS emat_count,
Sum(if(CMTS_RQ.US_Pwr>=37 and CMTS_RQ.US_Pwr<=49)) AS us_pwr_good
FROM
CMTS_RQ
GROUP BY
CMTS_RQ.CMTS_Name,
CMTS_RQ.[Dated]
But I get an error:
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'if'.
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ')'.
T-SQL doesn't have a "inline" IF statement - use a CASE instead:
SELECT
CMTS_RQ.[Dated],
CMTS_RQ.CMTS_Name,
Count(CMTS_RQ.CMTS_Name) AS emat_count,
Sum(CASE
WHEN CMTS_RQ.US_Pwr >=37 AND CMTS_RQ.US_Pwr <= 49
THEN 1
ELSE 0
END) AS us_pwr_good
FROM
CMTS_RQ
GROUP BY
CMTS_RQ.CMTS_Name,
CMTS_RQ.[Dated]
So if the value of CMTS_RQ.US_Pwr is >= 37 AND <= 49 then add 1 to the SUM - otherwise 0. Does that give you what you're looking for?
In SQL Server 2012 and newer, you can use the new IIF function:
SUM(IIF(CMTS_RQ.US_Pwr >= 37 AND CMTS_RQ.US_Pwr <= 49, 1, 0)) AS us_pwr_good

Indexing Computed Column of 2 largest integers [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 8 years ago.
Improve this question
According to Sql Server, Any computed column can be indexed only when it is deterministic.
ie., consider columns a,b, and c are of INT datatype and c = a+b. Now column C can be indexed but **when column a or b holds the largest Integer value it will throw the Arithmetic Error, is there any solution ?
Thanks.
create table TA (
ID int not null primary key,
a int not null,
b int not null,
c as a+b
)
go
create index IX_TA_c on TA (c)
go
insert into TA(ID,a,b) values (1,1,2147483647)
The insert gets:
Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type int.
The statement has been terminated.
If you want the calculation to always work, change the calculation so that at least one of the columns is forced to be a bigint (and so the maths and the resultant type of c are all bigint also):
c as CAST(a as bigint)+b

Nonsense Error in SQL Server Management Studio

I have this query that spawns the following error:
SELECT * FROM Quota
WHERE LEFT(QtLabel, LEN(QtLabel)-2) IN (
'1032',
'3300',
'9682'
)
Msg 536, Level 16, State 5, Line 1
Invalid length parameter passed to the SUBSTRING function.
Am I doing something wrong? It tends to show up when I use the LEN() function. Might it be a datatype issue?
Is it possible that LEN(QtLabel) <= 2 ?
Are you sure that each QtLabel field is longer than 2 characters?
LEFT probably uses SUBSTRING internally. What happens if the length of QtLabel is <= 2?
It's because some QtLabel values don't contain > 2 characters, so you end up trying to do a LEFT() with a negative value as the number to restrict to.
In your scenario, you're assuming all QtLabel values are 6 characters, so uou should do:
SELECT * FROM Quota
WHERE LEN(QtLabel) = 6
AND LEFT(QtLabel, LEN(QtLabel)-2) IN (
'1032',
'3300',
'9682'
)
SELECT LEFT('MyText', -2)
will throw the same error

Resources