Updating part of a SQL cell - sql

Trying to run this SQL script
update Promotions
set PromotionDiscountData = '<ArrayOfPromotionRuleBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><PromotionRuleBase xsi:type="StartDatePromotionRule"><StartDate>2013-11-11T00:00:00</StartDate></PromotionRuleBase><PromotionRuleBase xsi:type="ExpirationDatePromotionRule"><ExpirationDate>2014-01-12T00:00:00</ExpirationDate></PromotionRuleBase><PromotionRuleBase xsi:type="ExpirationNumberOfUsesPerCustomerPromotionRule"><NumberOfUsesAllowed>1</NumberOfUsesAllowed></PromotionRuleBase><PromotionRuleBase xsi:type="ProductIdPromotionRule"><ProductIds><int>55232</int></ProductIds><RequireQuantity>false</RequireQuantity><Quantity>1</Quantity><AndTogether>false</AndTogether></PromotionRuleBase></ArrayOfPromotionRuleBase>'
where PromotionDiscountData = '<ArrayOfPromotionRuleBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><PromotionRuleBase xsi:type="StartDatePromotionRule"><StartDate>2013-11-18T00:00:00</StartDate></PromotionRuleBase><PromotionRuleBase xsi:type="ExpirationDatePromotionRule"><ExpirationDate>2014-01-12T00:00:00</ExpirationDate></PromotionRuleBase><PromotionRuleBase xsi:type="ExpirationNumberOfUsesPerCustomerPromotionRule"><NumberOfUsesAllowed>1</NumberOfUsesAllowed></PromotionRuleBase><PromotionRuleBase xsi:type="ProductIdPromotionRule"><ProductIds><int>55232</int></ProductIds><RequireQuantity>false</RequireQuantity><Quantity>1</Quantity><AndTogether>false</AndTogether></PromotionRuleBase></ArrayOfPromotionRuleBase>'
but getting this error
Msg 402, Level 16, State 1, Line 1
The data types xml and varchar are incompatible in the equal to operator.
any idea how to fix this?
Basically within each cell I am trying to change the StartDate only

I suppose the type of PromotionDiscountData column is XML, so that's why I suggest you to use the following code snippet:
update Promotions
set PromotionDiscountData = '<ArrayOfPromotionRuleBase ...'
where CAST(PromotionDiscountData as NVARCHAR(MAX)) = '<ArrayOfPromotionRuleBase...'

Related

How to solve the error_syntax error near '< '

Msg 102, Level 15, State 1, Line 3 Incorrect syntax near '<'.
I got the above error message every time I tried to execute the below query.
UPDATE [dbo].[FM1]
SET [Datum] = <Datum, smalldatetime,>
,[Gesamtzeit] = <Gesamtzeit, nvarchar(5),>
You can try this no need to specify datatype here.
UPDATE [dbo].[FM1]
SET [Datum] = 'YourValue'
,[Gesamtzeit] = 'YourValue'
where ...

Edit column data at SQL

I am trying to update my retrieved data as described below:
UPDATE vw_public_task_priority
SET task_state = REPLACE(task_state, 'NULL', 'DONE')
After I execute it I get the next error:
Msg 4406, Level 16, State 1, Line 41
Update or insert of view or function 'vw_public_task_priority'
failed because it contains a derived or constant field.
Can you please advice to me what I'm doing wrong + there is a possibility to update the results at new column instead of edit "task_state" data?
Thanks!
Try this
UPDATE vw_public_task_priority SET task_state = 'DONE' where task_state is null

Rerun Error Message While Creating a Function

i am fairly new to SQL programming, and I am currently learning to create FUNCTIONS.
The problem that I am having, is creating the following FUNCTION.
create function CreatePI
(
)
returns decimal(10,6)
with returns null on null input
as
begin
declare #P as decimal(10,6)
set #P = 4*(1-(1/3)+(1/5)-(1/7)+(1/9)-(1/11)+(1/13)-(1/15)
return #P
end
go
The above function is supposed to replicate the number PI. But the problem that I am having is:
Msg 156, Level 15, State 1, Procedure CreatePI, Line 11
Incorrect syntax near the keyword 'return'.
If anyone could help me out with why I am getting this problem, it would be greatly appriciatead.
You are missing a closing paren on the set line:
set #P = 4*(1-(1/3)+(1/5)-(1/7)+(1/9)-(1/11)+(1/13)-(1/15))
----------------------------------------------------------^

SQL Replace and where CAST(PromotionRuleData as NVARCHAR(MAX))

Can't seem to edit my old post but I am trying to execute this SQL script
UPDATE Promotions
set Description = '£5 Off £25 Spend',
UsageText = '£5 Off £25 Spend',
EmailText = '£5 Off £25 Spend',
PromotionRuleData= '<ArrayOfPromotionRuleBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><PromotionRuleBase xsi:type="StartDatePromotionRule"><StartDate>2013-11-18T00:00:00</StartDate></PromotionRuleBase><PromotionRuleBase xsi:type="ExpirationDatePromotionRule"><ExpirationDate>2014-01-13T00:00:00</ExpirationDate></PromotionRuleBase><PromotionRuleBase xsi:type="ExpirationNumberOfUsesPerCustomerPromotionRule"><NumberOfUsesAllowed>1</NumberOfUsesAllowed> </PromotionRuleBase><PromotionRuleBase xsi:type="MinimumCartAmountPromotionRule"><CartAmount>24.99</CartAmount></PromotionRuleBase></ArrayOfPromotionRuleBase>',
PromotionDiscountData = '<ArrayOfPromotionDiscountBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><PromotionDiscountBase xsi:type="OrderPromotionDiscount"><DiscountType>Fixed</DiscountType><DiscountAmount>5.00</DiscountAmount></PromotionDiscountBase></ArrayOfPromotionDiscountBase>'
where Name = 'test1,test2,etc...'
It comes back with this error
Msg 402, Level 16, State 1, Line 1
The data types varchar and text are incompatible in the equal to operator.
I try to use where CAST(PromotionRuleData as NVARCHAR(MAX))
So the line reads as
CAST(PromotionRuleData as NVARCHAR(MAX)) = '<ArrayOfPromotionRuleBase ...
but no luck.
You cannot compare a string literal against a text column in SQL Server.
Which column is of datatype text ? Your name column that you use in the WHERE clause by any chance?
If so, use this WHERE instead:
WHERE CAST(Name AS VARCHAR(MAX)) = 'test1,test2,etc...'
or better yet: convert your column to a more appropriate datatype like VARCHAR(n) (unless you really need 2 GB = 2 billion characters - if so, then use VARCHAR(MAX))

SQL+Openxml+how to store NVARCHAR data

hello i am using the following stored procedure to insert the xml data in a table.
PROCEDURE [dbo].[ExpertSystem_SET_QuestionData]
#XmlQue NVARCHAR(MAX)
AS
set nocount on
DECLARE #XmlHdl INT
BEGIN
SET #XmlQue=N''+#XmlQue
EXEC sp_xml_preparedocument #XmlHdl OUTPUT,#XmlQue
INSERT INTO ExpertSystem_Master
SELECT QIdx,#TemplateId,QDetailsx,IsYesx,IsNox,Eligibleyesx,Eligiblenox,0,0
FROM
OPENXML(#XmlHdl,'/Template/QueInfo',1)
WITH
( QIdx INT '#QId',
QDetailsx Ntext '#QDetails',
IsYesx Ntext '#IsYes',
IsNox Ntext '#IsNo',
Eligibleyesx CHAR(1) '#Eligibleyes',
Eligiblenox CHAR(1) '#Eligibleno'
)
END
In this case i am passing the values to parameter as
ExpertSystem_SET_QuestionData '<?xml version="1.0" encoding="UTF-16"?><Template TId="1"><QueInfo QId="1" QDetails="Are you " IsYes="Yes" IsNo="No" Eligibleyes="Y" Eligibleno="N"/><QueInfo QId="2" QDetails=" राज्य " IsYes="Yes" IsNo="No" Eligibleyes="Y" Eligibleno="N"/><QueAns QId="1" isMsgFlag="N" Msg="you are not Eligible"/><QueAns QId="2" isMsgFlag="N" Msg="you are not eligible"/></Template>'
The data is inserted , but it is inserted as ????? instead of ** "राज्य "**.
When i am calling the procedure as
ExpertSystem_SET_QuestionData 'N<?xml version="1.0" encoding="UTF-16"?><Template TId="1"><QueInfo QId="1" QDetails="Are you Domicile of maharashtra" IsYes="Yes" IsNo="No" Eligibleyes="Y" Eligibleno="N"/><QueInfo QId="2" QDetails="Have you passed Secondary School 10th Examination?महाराष्ट्र राज्य मार्ग परिवहन महामंडळ " IsYes="Yes" IsNo="No" Eligibleyes="Y" Eligibleno="N"/><QueAns QId="1" isMsgFlag="N" Msg="you are not Eligible"/><QueAns QId="2" isMsgFlag="N" Msg="you are not eligible"/></Template>'
i get the error as "The XML parse error 0xc00ce556 occurred on line number 1, near the XML text "N "
can you please help me out with the issue,as the procedure is called at run time so any data can be passed to the #XmlQue variable.
so how am i supposed to append "N" with the #XmlQue variable ???
Thanks in advance.
The N should come before the xml value.
ExpertSystem_SET_QuestionData N'<?xml version="1.0" ...