Incorrect syntax near ',' SQL Server Error - sql

I am having the following error on my SQL Server Query I don't know how to overcome it Because I tried my best Please help me getting out of it:
CREATE TABLE d3 as SELECT sessionnumber, sessioncount, LEFT(timespent, 1) , COUNT
as cnt
FROM clusters
GROUP BY 1, 2, 3
The following error is generated:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ','.
Please help me! Thanks in advance

This is not SQL Server syntax. You want select into:
SELECT sessionnumber, sessioncount, LEFT(timespent, 1) as TimeSpentCode, COUNT(*) as cnt
into d3
FROM clusters
GROUP BY sessionnumber, sessioncount, LEFT(timespent, 1);
All the columns need to have names. So I added one for the third column.
And, group by does not accept positional indicators in SQL Server, so I replaced them with the appropriate expressions.

You have the word count instead of count(something).

Related

How to write a DB2 select query to include the string before the '-' delimiter

I have a column "Department" in my Table "College". Department is having data like
Commerce1-683877
Science2-678900
I need to write a select query which returns only Commerce1 and Science2.
I am completely new to DB2, please help me solve this.
select substring(department,0,CHARINDEX('-',department) as DEPT from College
Edit -1 : Thanks #Charles
I tried your solution :
select substring(department , 1, LOCATE('-',department) - 1) AS DEPT from College
but it's throwing me an error :
SQL Error [42815]: THE DATA TYPE, LENGTH, OR VALUE OF ARGUMENT 3 OF SUBSTRING IS INVALID. SQLCODE=-171, SQLSTATE=42815, DRIVER=4.9.78
Edit 2: You're right #Charles, all my rows doesn't contain '-'
I tried using the below query as well but getting the same error :
select substring(department,1, LOCATE('-',department || '-') - 1) as DEPT from College
What platform and version of Db2?
Did you look for an appropriate Db2 SQL Reference manual?
Note Db2 SQL string & arrays start at 1, not 0.
select substring(department,1, LOCATE('-',department) - 1) as DEPT from College
One method uses regexp_substr():
regexp_substr(department, '^[^-]+')

How to combine If statement with Substring command in SQL?

I have two types of entries in one column. One type starts with V2, the other starts with a digit. How do I write a query in SQL where I can extract different part of the string based on how it starts?
TextStringColumn
V2|T:GSK|1000000|S1:TES|S2:N/A|Q:24|S:0.5gx3|PD:2020-10-22|C:QQ
2000308|S1:BES|T:SKY|Q:16446G|BSI:BPKGVAXQOHZFWGE
I wrote
SELECT TextStringColumn, If(TextStringColumn like 'V2%',SUBSTRING(TextStringColumn ,10,7),SUBSTRING(TextStringColumn ,1,7)) As NumberCol
FROM TestTable
But I keep getting syntax errors.
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'If'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ','.
The desired result will be
TextStringColumn NumberCol
V2|T:GSK|1000000|S1:TES|S2:N/A|Q:24|S:0.5gx3|PD:2020-10-22|C:QQ 1000000
2000308|S1:BES|T:SKY|Q:16446G|BSI:BPKGVAXQOHZFWGE 2000308
You may use a CASE expression:
SELECT
CASE WHEN TextStringColumn LIKE 'V2%'
THEN SUBSTRING(TextStringColumn, 10, 7)
ELSE SUBSTRING(TextStringColumn, 1, 7) END AS NumberCol
FROM TestTable;
The above logic assumes the only two varieties of strings are those which start with V2, and those which do not start with V2.
If the strings are variable length ... consider a little JSON
Example
Declare #YourTable Table ([TextStringColumn] varchar(100)) Insert Into #YourTable Values
('V2|T:GSK|1000000|S1:TES|S2:N/A|Q:24|S:0.5gx3|PD:2020-10-22|C:QQ')
,('2000308|S1:BES|T:SKY|Q:16446G|BSI:BPKGVAXQOHZFWGE')
Select A.*
,Val = case when [TextStringColumn] like 'V2%'
then JSON_VALUE(S,'$[2]')
else JSON_VALUE(S,'$[0]') end
From #YourTable A
Cross Apply ( values ( '["'+replace([TextStringColumn],'|','","')+'"]' ) ) B(S)
Returns
TextStringColumn Val
V2|T:GSK|1000000|S1:TES|S2:N/A|Q:24|S:0.5gx3|PD:2020-10-22|C:QQ 1000000
2000308|S1:BES|T:SKY|Q:16446G|BSI:BPKGVAXQOHZFWGE 2000308

Error messages when I execute SQL Query in the Where Statement

I have created a SQL query using SQL Server Management Studio,
But, I faced errors in the Where statement.
Here WHERE clause of the SQL code:
where
(case when 'All' in (select Items from CDB.dbo.Split (#a,','))
then innt.code **IS** NOT NULL
else innt.code in (select Items from CDB.dbo.Split (#a,',')) end) and
I get these error messages:
Msg 156, Level 15, State 1, Line 90
Incorrect syntax near the keyword 'IS'.
Msg 156, Level 15, State 1, Line 92
Incorrect syntax near the keyword 'and'.
Can you guys please help me solve this?
Booleans are not a type in SQL Server. So a case cannot return a boolean expression.
So, just use regular comparisons
where ('All' in (select Items from CDB.dbo.Split(#a, ',') and innt.code IS NOT NULL) or
(innt.code in (select Items from CDB.dbo.Split(#a, ',') )

i try to combine rows in one column but there was an error in my syntax

select
Elm_EmployeeId as 'Badge',
Left(T_EmployeeLeave , Len(T_EmployeeLeave) - 1) As 'a'
from
(select
E2.Elm_EmployeeId as 'Badge2',
(select Elm_EmployeeId
from T_EmployeeLeave E1)
from
T_EmployeeLeave E2)
Error is:
Msg 102, Level 15, State 1, Line 8
Incorrect syntax near ')'.
1.you select Elm_EmployeeId and T_EmployeeLeave from a subquery but in the subquery u don't have these two columns what you have is badge2 and a non_named column
the select Elm_EmployeeId from T_EmployeeLeave E1 is meaningless
the query is miserable i can't even tell what excatly you want

PIVOT not working Incorrect syntax near ')'

T-SQL code:
SELECT iCarrierInvoiceDetailsID, [1],[2],[3]
FROM [GroundEDI].[dbo].[tblCarrierInvoiceDetails]
PIVOT(MAX(dTotalCharge) FOR iCarrierInvoiceHeaderID IN ([1],[2],[3]))AS P
Error:
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near ')'.
Any idea why I am getting this error?
It looks like you are trying to directly select the pivot columns from the table itself and not the pivot. You will need to do something like this:
SELECT p.[1],p.[2],p.[3]
FROM
(SELECT iCarrierInvoiceHeaderID
,dTotalCharge
FROM [GroundEDI].[dbo].[tblCarrierInvoiceDetails]) t
PIVOT(MAX(dTotalCharge) FOR iCarrierInvoiceHeaderID IN ([1],[2],[3])
)AS P;