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.
Related
I am working in SQL Server 2016 and trying to update the element value in my XML column for numerous records and getting a syntax error but can't figure out where the problem is. I've googled and tried a number of fixes but to no avail. Any help/direction would be appreciated. Thanks. Here are the details:
Here is my XML:
<IW>
<form name="INSTALLMENT LATE BILLING" version="2" blkflg="0">
<MonthlyPayment>260.34</MonthlyPayment>
<BillingFee>7.91</BillingFee>
<LateFee>13.00</LateFee>
<BillDate>12/23/2021</BillDate>
<DueDate>12/23/2021</DueDate>
<LateDate>12/24/2021</LateDate>
<CancelDate>01/04/2022</CancelDate>
</form>
</IW>
Here is my SQL code:
UPDATE dt
SET dt.DocumentData.modify('replace value of (/IW/form/CancelDate/text())[1] with "01/09/2022"')
, dt.CancelDate = '2022-01-09'
--SELECT dt.CancelDate AS DocTransCancellationDate
-- , p.CancelDate AS PolicyCancellationDate
-- , p.PolicyNo
-- , dt.DocumentData
-- , dt.*
FROM dbo.AUT_DocumentTransaction dt
JOIN dbo.AUT_Policy p ON dt.PolicySysID = p.PolicySysID
WHERE dt.DocumentTransactionSysID IN (45601342
,45601349
,45601354
,45601356
,45601357
,45601359
,45601360
,45601363
,45601364
,45601365
,45601366)
Here is the error I'm getting:
Msg 102, Level 15, State 1, Line 377
Incorrect syntax near 'modify'.
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?
Ok, I am using SQL Server 2016 and I am trying to figure out how to update the phone numbers from the Customers table. Which look like this "1234567890", and when I run this query:
USE ProductsDatabase2;
UPDATE Customers
SET PhoneNumber = '(' + SUBSTRING(PhoneNumber, 1, 3) + ') ' +
SUBSTRING(PhoneNumber, 4, 3) + '-' + SUBSTRING(PhoneNumber, 7, 4)
I get this error message:
Msg 8152, Level 16, State 13, Line 3
String or binary data would be truncated.
The statement has been terminated.
Here is the Customers table:
How do I fix this problem?
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
I have 2 SQL servers .
dstest\mssql2008 <--I'm currently at this instance ( server)
dstrn <-- another server
Both servers has the same table called :
EServices_Pages_Content
Goal : I need to update data on dstest from dstrn
On the current server (dstest) I have :
I can(!) access dstrn from dstest :
SELECT * FROM dstrn.weberp.dbo.EServices_Pages_Content WHERE pageid=80
OKay.
So where is the problem ?
As I told , I need to update data on dstest(current db) from a far server (dstrn) so I do :
UPDATE EServices_Pages_Content
SET [Content] = a.Content
FROM [dstrn].weberp.dbo.EServices_Pages_Content a
WHERE PageID = a.pageID
AND MasterEntityID = a.masterEntityid
AND LanguageID = a.LanguageID
AND PageID = 80
But I get an error :
Msg 209, Level 16, State 1, Line 4
Ambiguous column name 'PageID'.
Msg 209, Level 16, State 1, Line 5
Ambiguous column name 'MasterEntityID'.
Msg 209, Level 16, State 1, Line 6
Ambiguous column name 'LanguageID'.
Msg 209, Level 16, State 1, Line 7
Ambiguous column name 'PageID'.
I don't understand , I did use aliases , why does it tell me Ambiguous columns? How can I fix it ?
Edit : I found a way to make it work( see my answer) - but still can't understand why I need full table prefix in the where clause. - and can't use aliases to prevent Ambiguity
Try this:
UPDATE Dest
SET [Content] = a.Content
FROM EServices_Pages_Content Dest
INNER JOIN [dstrn].weberp.dbo.EServices_Pages_Content a
ON Dest.PageID = a.pageID
AND Dest.MasterEntityID = a.masterEntityid
AND Dest.LanguageID = a.LanguageID
WHERE a.PageID = 80
Explanation:
Using an alias on just one of the tables is not enough.
Since both of the tables have the same names and the same column names,
you need to specify an alias to the destination table as well, and use a multi-part name for it's columns.
Also, you are using an implicit join. Since explicit joins more readable, I would recommend to never use implicit joins.
I have created 2 test tables ( on the same server) :
Table testA
Table testB
Now , for simplicity - let's say I want to update tableA from tableB but only only for pageId=2 :
So first I tried :
UPDATE testA a
SET a.[Content] = b.Content
FROM testB b
WHERE a.PageID = b.pageID
AND a.PageID = 2
But I got an error :
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near 'a'.
So then I tried :
UPDATE testA
SET [Content] = b.Content
FROM testB b
WHERE testA.PageID = b.pageID -- notice prefix
AND testA.PageID = 2 -- notice prefix
And Then :
(1 row(s) affected)
And now you can see it's ok :
You need some join here:
UPDATE d
SET [Content] = a.Content
FROM EServices_Pages_Content d
JOIN [dstrn].weberp.dbo.EServices_Pages_Content s ON d.PageID = s.pageID
AND d.MasterEntityID = s.masterEntityid
AND d.LanguageID = s.LanguageID
AND d.PageID = 80