I am trying to update a row in the my SQL Server 2008 R2 database.
The value of the particular column is of type String.
The current values in my database is of the form: NMA.
But it should be N'MA.
My query is:
UPDATE mymodifiedtable
SET FirstName = 'N'MA'
WHERE receiptNo = '45047603'
This is not working for me after googling it.
Pls advice.
I would also like a link where I can learn more on this type of escape characters in SQL Server.
You can use the quote character to escape quotes. Try SET FirstName = 'N''MA'
Related
I am running a select statement which contains the following CASE clause:
SELECT
(CASE MyTable.IsBook WHEN 1 THEN 'B' ELSE 'M' END) AS IsBookOrManuscript
FROM MyTable;
I have the same exact database(schema and data) restored in two different physical servers running SQL Server 2008 R2 with build version 10.50.4276.0 and SQL Server 2014 respectively.
When run in SQL Server 2014 the query returns as expected. When run in SQL Server 2008R2 the error message Incorrect syntax near ' '. occurs.
Searching the script file for non-ascii characters I found that indeed, three occurrences of 0x0A character appear in the CASE line and removing it solves the problem in SQL Server 2008R2.
Does anyone know why that happens? Is it an intended behavior? As far as I can see there are 2 CUs released since my last update BUT they do not seem to fix or recognize the problem. Any thoughts?
Check the Setting of
QUOTED_IDENTIFIER
on each server.
as MSDN says:
Causes SQL Server to follow the ISO rules regarding quotation mark delimiting identifiers and literal strings. Identifiers delimited by double quotation marks can be either Transact-SQL reserved keywords or can contain characters not generally allowed by the Transact-SQL syntax rules for identifiers.
so try the next code:
SET QUOTED_IDENTIFIER off
-- Type your code here
SET QUOTED_IDENTIFIER On
It seemed to be an easy task but I fail and do not find a solution for my problem: I have a local table in Access 2010 with a date/time column and I want to update a column in a SQL Server table with a datatype date.
Sending the date/time values direct to the SQL Server table fails, same with converting the date/time column with this VBA function:
Function DateForSQL(dteDate) As String
DateForSQL = "'" & Format(CDate(dteDate), "yyyy-mm-dd") & "'"
End Function
which gives
DateForSQL(Date()) = '2016-01-14'
and should work, I assumed.
The update command is this:
UPDATE SQL_table
INNER JOIN local_table ON SQL_table.ID = local_table.ID
SET SQL_table.DateField = DateForSQL(local_table.DateField)
But it fails again in Access with a type conversion error.
Even when changing the SQL Server table column to datetime I get the same error.
Same with sending to SQL a string like '14/01/2016' or '01/14/2016'.
The only thing I could do - eventually - is to change the datetime to text in Access and try again, but this could not be the only solution.
Any help?
Thanks
Michael
First of all, I recommend to use the ISO-8601 format for your date - which is YYYYMMDD (no dashes, nothing) - this works for all regional & language settings in SQL Server.
Next, I'm not sure about Access' SQL syntax, but in SQL Server, your UPDATE statement would be to be something like this:
UPDATE sql
SET sql.DateField = DateForSQL(local_table.DateField)
FROM local_table local
INNER JOIN SQL_table sql ON local.ID = sql.ID
First UPDATE, then SET, then FROM and INNER JOIN ...
I'm going to insert a Unicode text into SQL Server database by ADOQuery .
My text is stored in k_message WideChar variable.
I know that I have to prefix 'N' for Unicode actions in SQL Server,
But in this case How to apply this letter for k_message column?
on the other hand ADOQuery only gets non-Unicode text SQL string
and I have to use parameter instead.
With the following code I have received error:
Incorrect Syntax near #P1.
My Programming Environment is: Borland C++ Builder 6
txt="insert my_table(ksource,ksubject,kdate,ktime,kmessage) values ('SMS','"+
fsubject+"','"+
fdate+"','"+
ftime+"',N :k_message)";
DMod1->ADOQuery2->Close();
DMod1->ADOQuery2->Prepared=true;
DMod1->ADOQuery2->SQL->Clear();
DMod1->ADOQuery2->SQL->Add(txt);
DMod1->ADOQuery2->Parameters->ParamByName("k_message")->Value=k_message;
DMod1->ADOQuery2->ExecSQL();
I think this should work
txt="insert my_table(ksource,ksubject,kdate,ktime,kmessage) values ('SMS',N'"+
fsubject+"',N'"+
fdate+"',N'"+
ftime+"N',N :k_message)";
I have a database field with nVarchar(30). I am using asp.net MVC. When i insert the record in Unicode, i get ?????.
Any one can tell me how can i convert a string to unicode and insert into database.
I am using SQL Server 2008 R2.
Try to change your database collation to Latin1_General_BIN2.
http://msdn.microsoft.com/en-us/library/ms175835.aspx
Make sure:
You Use N' at the start of string literals containing such strings, e.g. N'enović'
If you want to query and ignore accents, then you can add a COLLATE clause to your select. E.g.:
SELECT * FROM Account
WHERE Name = 'enovic' COLLATE Latin1_General_CI_AI
I am using SQL Server 2008 express edition and its collation settings are set to default.I wish to store special characeters like á ,â ,ã ,å ,ā ,ă ,ą ,ǻ in my database but it converts them into normal characters like 'a'. How can I stop SQL Server from doing so?
Make sure that your columns are using the type nvarchar(...), rather than varchar(...). The former is Unicode, the latter is ASCII.
Also, make sure that your database default collation is set to Accent Sensitive, and that your columns are stored that way. You may also want to check your instance default collation, as that affects the default collation for your system databases, particularly tempdb.
Rahul, here is a very simple query that runs perfectly on SQL 2005 and 2008:
Query
DECLARE #t1 TABLE (
Col1 nvarchar(30)
)
INSERT INTO #t1 VALUES (N'á ,â ,ã ,å ,ā ,ă ,ą ,ǻ')
SELECT * FROM #t1
Result
Col1
------------------------------
á ,â ,ã ,å ,ā ,ă ,ą ,ǻ
There is nothing special here. No collation change from default, just a simple NVARCHAR column.
You said you are "just running direct queries in the database". Can you try this query and see if you get the same results?