I have a SQL Server Procedure like this:
//Beginning of procedure
SELECT
CASE IDNumber
WHEN (DATALENGTH(IDNumber)>7)
THEN SUBSTRING(IDNumber,0,6)
WHEN (DATALENGTH(IDNumber) < 7)
THEN CONCAT((REPLICATE(0,7-LEN(IDNumber)),IDNumber)
END AS NID
//Rest of the procedure here
the code on execution throws the error Incorrect syntax near '>'. on line WHEN (DATALENGTH(IDNumber)>7) .
IDNumber is a nvarchar. I tried using LEN(IDNumber) but in vain.
I don't know what the error is!
Rewrite your query like that:
SELECT CASE
WHEN DATALENGTH(IDNumber) > 7 THEN SUBSTRING(IDNumber, 0, 6)
WHEN DATALENGTH(IDNumber) < 7 THEN CONCAT(REPLICATE(0, LEN(IDNumber)), IDNumber)
END AS NID;
When you write CASE Column, you have to compare it to direct values, not to an expression.
If you have case variable then you need to provide values to When part, not the condition
Related
I'm trying to update a table in sql server using the below command but I get the following error:
Msg 156, Level 15, State 1, Line 716
Incorrect syntax near the keyword 'LEFT'
Is LEFT not allowed when updating? What can be used instead? Thanks
UPDATE DI.DBO.MHS
SET (LEFT(BATCH_DATE_2, 1) + '0' + RIGHT(BATCH_DATE_2, 6))
WHERE LEFT(BATCH_DATE_2, 1) = 2
You must specify the column that you want to update:
UPDATE DI.DBO.MHS
SET BATCH_DATE_2 = LEFT(BATCH_DATE_2,1) + '0' + RIGHT(BATCH_DATE_2,6)
WHERE LEFT(BATCH_DATE_2,1) = 2
If it is not BATCH_DATE_2 the column that you want to update then use that column after SET.
When I ran the following query on our SQL Server, the query ran fine and returned the correct result.
select
LEFV.TextValue,
json_value (LEFV.TextValue, '$."st4TempSensorValue.1.1"') as TempValue
from
SolarWindsOrionLog.dbo.OrionLog_LogEntryFieldValue LEFV
where
LEFV.TextValue like '%st4TempSensorValue%'
But as soon as I join with another table, I get an error
Msg 13609, Level 16, State 2, Line 1
JSON text is not properly formatted. Unexpected character 'p' is found at position 0
select
LEFV.TextValue,
json_value (LEFV.TextValue, '$."st4TempSensorValue.1.1"') TempValue,
LE.LogEntryMessageSourceID
from
SolarWindsOrionLog.dbo.OrionLog_LogEntryFieldValue LEFV
inner join
SolarWindsOrionLog.dbo.OrionLog_LogEntry LE on LEFV.LogEntryID = LE.LogEntryID and LEFV.PartitionID = LE.PartitionID
where
LEFV.TextValue like '%st4TempSensorValue%'
The datatype of [TextValue] is [nvarchar](max).
Could anyone please help me debug this query? Thank you for you help!
You could check if the field is JSON before trying to search inside it. Something like this
select LEFV.TextValue
,CASE WHEN ISJSON(LEFV.TextValue)>0 THEN json_value(LEFV.TextValue,'$."st4TempSensorValue.1.1"') ELSE NULL END AS TempValue
,LE.LogEntryMessageSourceID
from SolarWindsOrionLog.dbo.OrionLog_LogEntryFieldValue LEFV
inner join SolarWindsOrionLog.dbo.OrionLog_LogEntry LE
ON LEFV.LogEntryID = LE.LogEntryID AND LEFV.PartitionID = LE.PartitionID
WHERE ISJSON(LEFV.TextValue)>0 AND LEFV.TextValue like '%st4TempSensorValue%'
It's probably trying to parse an invalid value to Json.
I am trying to change the value of the data that comes out of the SQL. Where it usually has the value of 4 in SAL_ClientTypeID, I would like it to say 'Private'. The error I am currently getting is
"Msg 156, Level 15, State 1, Line 3 Incorrect syntax near the keyword
'as'."
Initially I tried the following
CASE
WHEN SAL_ClientTypeID = 4 THEN 'Private'
ELSE SAL_ClientTypeID
END,
but I got this error
"Conversion failed when converting the varchar value 'Private' to data
type int."
SELECT SAL_Account_Ref AS 'Account Ref',
CASE
WHEN SAL_ClientTypeID = 4 THEN cast (sal_clienttypeID as nvarchar(20)) as 'Private'
ELSE SAL_ClientTypeID
END
FROM sales;
I expect the value of output where SAL_ClientTypeID = 4 to be 'Private'
SELECT SAL_Account_Ref AS 'Account Ref',
CASE
WHEN SAL_ClientTypeID = 4 THEN 'Private'
ELSE cast (sal_clienttypeID as nvarchar(20))
END
FROM sales;
There are two problems with your query:
Msg 156, Level 15, State 1, Line 3 Incorrect syntax near the keyword 'as'.
Comes from having as followed by a string literal, this should be followed by column name:
SELECT SAL_Account_Ref AS Account_Ref
The second error is because you return two datatypes on the case - string if it equals to 4, int otherwise, you should use casting to make sure it's always the same:
SELECT SAL_Account_Ref AS Account_Ref,
CASE
WHEN SAL_ClientTypeID = 4 THEN 'Private'
ELSE cast (sal_clienttypeID as nvarchar(20))
END
FROM sales;
You can't mix datatypes in a CASE... try
SELECT SAL_Account_Ref AS 'Account Ref',
CASE
WHEN SAL_ClientTypeID = 4 THEN 'Private'
ELSE CAST(SAL_ClientTypeID, as nvarchar(20))
END
FROM sales;
There is a datatype priority in tsql. Number types have higher priority than text types. Thus, when your case includes both text like 'Private' and number types like SAL_ClientTypeID, the number overrides. When the case of 'Private' comes, tsql tries to convert it to a number and throws the error you see.
If you are OK with having the SAL_ClientTypeID as text, use one of the other answers. Otherwise, you'll have to rethink your specification.
I want to use the sum function in the ELSE part of the CASE Statement. I need to give the SUM function conditions but I'm unable to do so.
I tried to use the GROUP BY Function within the sum function but it doesn't work.
CASE WHEN SectionCrossList = NULL THEN InstructorCredits ELSE InstructorCredits/Sum(Instructorcredits) GROUP BY (AcademicYear,SectionCrossList) END NumCreditSplit
Msg 156, Level 15, State 1, Line 220 Incorrect syntax near the keyword 'GROUP'.
Msg 102, Level 15, State 1, Line 221 Incorrect syntax near 'NumCreditSplit'.
You should use CTE or window function sum
SELECT AcademicYear,SectionCrossList,
CASE
WHEN SectionCrossList = NULL
THEN
InstructorCredits
ELSE
InstructorCredits/Sum(Instructorcredits)
end NumCreditSplit
FROM <yourTable>
group by AcademicYear,SectionCrossList
I create scalar function to count row from pass variable as below.
CREATE procedure [dbo].[CARFCN](#NBLC Varchar) returns int
as
Begin
Declare #CDLF INT
set #CDLF = (select count([*Downlink EARFCN]) from EutranInterNFreq$
where NodeB_Locell = #NBLC)
return #CDLF
End
question 1. This function cannot use in select. How to use?
select cell.[*eNodeB Name],cell.[*eNodeB ID],cell.[*Cell Name], cell.
[Downlink EARFCN], Cell.[Nodeb Locell],
[dbo].CARFCN(CELL.[*eNodeB Name]) as NUM
from ais.dbo.cell$ CELL
LEFT JOIN ais.dbo.EutranInterNFreq$ InterN
on cell.[Nodeb Locell]= interN.[NodeB_Locell];
error Msg 102, Level 15, State 1, Line 7
Incorrect syntax near 'dbo'. What I am wrong?
Question 2
Then I try to minimize problem I create sql to call as below.
select dbo.CARFCn('ABCRM_101') as numb; This return value 0 ????
but if i call query as below answer is 2
select count([*Downlink EARFCN]) from EutranInterNFreq$ where NodeB_Locell =
'ABCRM_101');
Why is that and how to fix?