Sql update statement says an error - sql

Iam updating an temporary table from the original table
but it is having some issues can anyone help me for this problem
here is my code and the error
The below given statement says Incorrect syntax near the keyword 'group'.
Update #tmpODU set Tn1Cnt=1,TnPay=isnull(sum(Amt),0)
From Table1
where Code COLLATE DATABASE_DEFAULT=TcCode
COLLATE DATABASE_DEFAULT and Del='R' and Date<=dateadd(M,#n1D,#dDate)
group by Code
can anyone help me...
thank you.....

i got the answer using below statment
and it is working as i expected
Update #tmpODU
set Tn1Cnt=1,
Tn1Pay=isnull((select sum(Amt)
From table1
where Code COLLATE DATABASE_DEFAULT=TcCode COLLATE DATABASE_DEFAULT
and Del='R'
and Date<=dateadd(M,#n1D,#dDate) group by Code),0)

Related

Cannot resolve the collation conflict in Update query

I got several answers for this error but nothing works in my case.I have a simple update query
update students set studentID ='001093' where studentID ='1578093'
when i am trying to update in my sql db getting this collation error:
Cannot resolve the collation conflict between
"SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_CI_AI" in the equal
to operation.
what i tried
update students set studentID COLLATE SQL_Latin1_General_CP1_CI_AS ='001093' where studentID ='1578093' COLLATE SQL_Latin1_General_CP1_CI_AS
please help on this problem..i know we have enough duplicates but none of the solution is working for me
The collation should be applied to the string value, not the column:
update students
set studentID = '001093' COLLATE Latin1_General_CI_AI
where studentID = '1578093' COLLATE Latin1_General_CI_AI;
Good day, beginer_programer.
Try to apply the COLLATE DATABASE_DEFAULT:
Update students
SET studentID ='001093' collate database default
WHERE studentID = studentID ='1578093' collate database default

"SQL_Latin1_General_CP1_CI_AS" in the equal to operation

Hi i am new to SQL queries
My query is
ALTER TABLE ValidIBAN NOCHECK CONSTRAINT FK_ValidIBAN_Countries
UPDATE t
SET t.CountryID = s.Corrected
from #TempNewCountryID s,Countries t
where
s.Existing = t.CountryID
but after running this query i am getting
Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AI" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation.
Error Please suggest.
Depends on which logic is required (Accent sensitive or insensitive), use COLLATE on both sides of your equal sign: WHERE s.Existing COLLATE SQL_Latin1_General_CP1_CI_AI = t.CountryID COLLATE SQL_Latin1_General_CP1_CI_AI
If you update collate on table, or database(if needed), even in the server(if needed), then you never such an error.
ALTER TABLE tbl1 CONVERT TO CHARACTER SET utf8mb4 COLLATE SQL_Latin1_General_CP1_CI_AI;
I think it is better to update collate on the table rather than using it on queries.

T-SQL Job fail with Incorrect syntax near ' '. [SQLSTATE 42000] (Error 102)

Just wondering if some can help me figure out why this T-SQL script doesn't work. My experience in SQL is novice at best.
DELETE FROM ActiveUsers
WHERE HostName + CAST(HostId AS CHAR) IN (SELECT HostName + CAST(HostId AS CHAR)
     FROM ActiveUsers ACTUSR
     WHERE NOT EXISTS (SELECT NULL)
     FROM master.dbo.sysprocesses SYSPRC
     WHERE SYSPRC.HostName = ACTUSR.HostName COLLATE database_default
     AND SYSPRC.HostProcess = ACTUSR.HostId
     GROUP BY SYSPRC.HostName, SYSPRC.HostProcess))
The error message is:
Executed as user: . Incorrect syntax near ' '. [SQLSTATE 42000] (Error 102) Incorrect syntax near ' '. [SQLSTATE 42000] (Error 102). The step failed.
It looks like you have an extra closing bracket in the line
WHERE NOT EXITS (SELECT NULL)
Update:
Ok, you have said that you removed the bracket after the select null in that case the following 2 points also apply
The GROUP BY clause in the subquery is redundant and can be safely removed since you have no aggregate function and you are just checking if results NOT EXISTS.
Also one of the two sub queries can be removed which leaves you with the query below. This will delete records from ActiveUsers table where the HostName and HostId are not found in the master.dbo.sysprocesses table.
Try this, I have tested it and it worked fine for me.
DELETE FROM ActiveUsers
WHERE NOT EXISTS (SELECT NULL
FROM master.dbo.sysprocesses SYSPRC
WHERE SYSPRC.HostName = ActiveUsers.HostName COLLATE database_default
AND SYSPRC.HostProcess = ActiveUsers.HostId)

Vietnamese characters in sql select like

When I search with the following query
Select * From [table] WHERE Name like '%Hà Nội T&T%'
in my mssql database I get no results, even though I'm sure it exists in the db.
I seem to have trouble with characters like ộ, ẫ, and Đ.
I have tried changing collation-settings but nothing helps.
Any suggestions?
Try:
Select * From [table] WHERE Name like N'%Hà Nội T&T%'
Try changing connection encoding using:
SET character_set_client = charset_name;
Also please do not use Latin1 encoding, try switching to UTF8. Here is a FAQ for Vietnamse Unicode: http://vietunicode.sourceforge.net/main.html
You need to alter your column containing Vietnamese characters.
ALTER TABLE [table] ALTER COLUMN name NVARCHAR(100) COLLATE Vietnamese_CI_AS
And then do
SELECT * FROM [table] WHERE name LIKE '%Hà Nội%'
SQL FIDDLE DEMO

SQL Server Alter Table Query (Incorrect Syntax)

I'm stumped, and I know this is probably something very simple. I am trying to add two columns to an existing table.
I am receiving the following syntax error:
Incorrect syntax near 'PublishedDate' Expecting '(', or SELECT.
Here is my SQL:
ALTER TABLE my_table ADD (PublishedDate DATETIME, UnpublishedDate DATETIME)
Try without the parentheses:
ALTER TABLE my_table
ADD PublishedDate DATETIME, UnpublishedDate DATETIME
Here is a sqlfiddle with a demo for you to try.