I've a SQL script which is returned by Oracle, this output script then needs to be run in SQL server. I know it might sound odd but we are in the process of moving from Oracle to SQL server. so the problem is few of the lines from this script have statement like
insert into media values('Mumbai','C123','MP3','Gully ' || chr(38) || ' Gang','','');
When the above statement is tried in SQL server then error is thrown saying
Incorrect syntax near '|'.
How do I fix this?
I tried set define on/off but of no use.
Thanks
Presumably, you intend:
insert into media
values('Mumbai', 'C123', 'MP3',
concat('Gully ', char(38), ' Gang'), '', '');
Some comments:
Why not just use 'Gully & Gang'? On my keyboard at least, & is easy to type.
In Oracle, the '' are NULLs. Those are different in SQL Server. It is not clear which you want, but I might guess NULL.
List the columns explicitly on insert. That is a best practice.
In SQL Server, using '+' instead of '||'.
insert into media values('Mumbai','C123','MP3','Gully ' + char(38) + ' Gang','','');
All expressions are preferably of type string.
When converting from Oracle to SQL Server you should use (or have on-hand for reference) SQL Server Migration Assitant For Oracle, which will translate that statement for you as
INSERT MEDIA(
NAME,
A,
B,
C,
D,
E)
VALUES (
'Mumbai',
'C123',
'MP3',
'Gully ' + ISNULL(char(38), '') + ' Gang',
NULL,
NULL)
GO
Oracle silently converts '' to NULL, and SQL Server does not. So that's a required part of the conversion.
Related
Suppose we have a Teradata table db.queries in which prepared statements are stored in statement_code as CLOB, for example the content of such field can look like:
INSERT INTO DATA.TABLE
(ID, JOB_NAME, DATE)
VALUES(1, 'TEST_JOB', CAST(CURRENT_DAY AS DATE FORMAT 'YYYYMMDD')='$currentDay');
We have a stored procedure, which reads this data and then executes this using the following code:
SELECT statement_code
FROM db.queries
WHERE ACTIVE_FLAG = 1
INTO SQL_QRY;
EXECUTE IMMEDIATE SQL_QRY;
This is failing because the extracted statement_code in the SQL_QRY has now esacped single quotes.
Syntax error, expected something like '')'' or '','' between a string or a Unicode character literal and the word ''YYYYMMDD''.
Returned string from SQL_QRY is:
INSERT INTO DATA.TABLE
(ID, JOB_NAME, DATE)
VALUES(1, ''TEST_JOB'', CAST(CURRENT_DAY AS DATE FORMAT ''YYYYMMDD'')=''$currentDay'');
As opposed to the stored statement_code:
INSERT INTO DATA.TABLE
(ID, JOB_NAME, DATE)
VALUES(1, 'TEST_JOB', CAST(CURRENT_DAY AS DATE FORMAT 'YYYYMMDD')='$currentDay');
We have tried using OREPLACE in the variable setting to no avail. The function can replace double single quotes to any character, but single quotes.
By dumping various testing combinations:
OREPLACE(SQL_QRY, '''''', '*') --replaces to *
OREPLACE(SQL_QRY, '''', '*') --replaces to *
OREPLACE(SQL_QRY, '''', '') --get rids of quotes completely
OREPLACE(SQL_QRY, '''''', '''') --leaves the double quotes
Is there a way to overcome this nuisance or what is the proper way to achieve the goal? Retrieve prepared statements and execute them?
Kind Regards
Since your query is prepared already, I think you don't need to prepare it again, execute immediate converts ' to '' to make a string ready as a SQL statement to be executed and screws your already prepared string up.
So you just need to do :
EXECUTE SQL_QRY;
or if your queries stored with two single quotes maybe you need to convert them to one single quote:
OREPLACE(SQL_QRY, '''', ''')
I am trying to remove double quotes " from a column in my SQL export and I get an error, after researching the proper way... this is one of the ways I have tried....
SELECT
'293453' as custnum,
REPLACE(Orders.Order_Comments, '"', '') as FULFILL1,
OrderDetails.OrderID as OrderID2, etc.
The resulting error is:
Your SQL is invalid: Argument data type text is invalid for argument 1 of replace function.
Your Orders.Order_Comments columns is of type text. You can't use the REPLACE() function with that data type. It only works with char/varchar/nchar/nvarchar.
To fix this, the best thing to do is ALTER the table to use a varchar(max) column. The text type is depcrecated, anyway. But, if that's not an option, you'll have to cast it in the query:
REPLACE(CAST(Orders.Order_Comments as varchar(max)), '"', '')
Note that this is potentially very slow.
Take a look at this answer: SQL Server find and replace in TEXT field
The text data type is not valid for use with replace, so you need to cast it as VARCHAR(MAX)
REPLACE(CAST(Orders.Order_Comments AS VARCHAR(MAX)), '"', '')
Try this:
select REPLACE(isnull(Orders.Order_Comments,''), '"', '') as order_comments
from orders
Trying to replace a character of a column within all the Records of a table,
Wanted to change '_' with ' '
Any snippets how doing that?
Notes :
This is the primary key of type Nvarchar
Database is SQL Server CE
using management studio
You can use REPLACE to do that :
Update dbo.table
Set col = replace(col, '_', '');
Where col is the name of the column that you need to manipulate ...
How do you add a string to a column in SQL Server?
UPDATE [myTable] SET [myText]=' '+[myText]
That doesn't work:
The data types varchar and text are incompatible in the add operator.
You would use concat on MySQL, but how do you do it on SQL Server?
like said before best would be to set datatype of the column to nvarchar(max), but if that's not possible you can do the following using cast or convert:
-- create a test table
create table test (
a text
)
-- insert test value
insert into test (a) values ('this is a text')
-- the following does not work !!!
update test set a = a + ' and a new text added'
-- but this way it works:
update test set a = cast ( a as nvarchar(max)) + cast (' and a new text added' as nvarchar(max) )
-- test result
select * from test
-- column a contains:
this is a text and a new text added
Stop using the TEXT data type in SQL Server!
It's been deprecated since the 2005 version. Use VARCHAR(MAX) instead, if you need more than 8000 characters.
The TEXT data type doesn't support the normal string functions, while VARCHAR(MAX) does - your statement would work just fine, if you'd be using just VARCHAR types.
The + (String Concatenation) does not work on SQL Server for the image, ntext, or text data types.
In fact, image, ntext, and text are all deprecated.
ntext, text, and image data types will
be removed in a future version of
MicrosoftSQL Server. Avoid using these
data types in new development work,
and plan to modify applications that
currently use them. Use nvarchar(max),
varchar(max), and varbinary(max)
instead.
That said if you are using an older version of SQL Server than you want to use UPDATETEXT to perform your concatenation. Which Colin Stasiuk gives a good example of in his blog post String Concatenation on a text column (SQL 2000 vs SQL 2005+).
UPDATE test SET a = CONCAT(a, "more text")
hmm, try doing CAST(' ' AS TEXT) + [myText]
Although, i am not completely sure how this will pan out.
I also suggest against using the Text datatype, use varchar instead.
If that doesn't work, try ' ' + CAST ([myText] AS VARCHAR(255))
To Join two string in SQL Query use function CONCAT(Express1,Express2,...)
Like....
SELECT CODE, CONCAT(Rtrim(FName), " " , TRrim(LName)) as Title FROM MyTable
Scenario:
Need to pass n arguments to a stored procedure. One of the arguments is of type varchar(x). That varchar argument needs to be constructed from a handful of other varchar variables. This problem uses SQL Server 2005, but this behaviour applies to all versions of SQL Server.
Setup:
DECLARE #MyString varchar(500), #MyBar varchar(10), #MyFoo varchar(10)
SELECT #MyBar= 'baz '
SELECT #MyFoo= 'bat '
-- try calling this stored procedure!
EXEC DoSomeWork #MsgID, 'Hello ' + #MyBar + '" world! "' + #MyFoo + '".'
This produces the exception in SQL Server: Incorrect syntax near '+'. Typically you might think that the datatype would be wrong (i.e. the variables are of different types, but that would produce a different error message).
Here's a correct implementation that compiles without error:
SELECT #MyString= 'Hello ' + #MyBar + '" world! "' + #MyFoo + '".';
EXEC DoSomeWork #ID, #MyString
Question: Why is it that T-SQL can't handle the concatenation of a varchar as an argument? It knows the types, as they were declared properly as varchar.
The EXECUTE statement simply has a different grammar then other statements like SELECT and SET. For instance, observe the syntax section at the top of the following two pages.
EXECUTE statement: http://msdn.microsoft.com/en-us/library/ms188332.aspx
SET statement: http://msdn.microsoft.com/en-us/library/ms189484.aspx
The syntax for EXECUTE only accepts a value
[[#parameter =] {value | #variable
[OUTPUT] | [DEFAULT]]
Whereas the syntax for SET accepts an expression
{#local_variable = expression}
A value is basically just a hard coded constant, but an expression is going to be evaluated. It's like having the varchar 'SELECT 1 + 1'. It's just a varchar value right now. However, you can evaluate the string like this:
EXEC('SELECT 1 + 1')
I suppose all I'm pointing out is that the EXEC command doesn't allow expressions by definition, which you apparently found out already. I don't know what the intention of the developers of T-SQL where when they made it that way. I suppose the grammar would just get out of hand if you where allowed to throw subqueries within subqueries in the parameter list of a stored procedure.
T-SQL Expression: http://msdn.microsoft.com/en-us/library/ms190286.aspx
You cannot do something like this either
exec SomeProc getdate()
you have to put all that stuff in a param like you are doing at your bottom query
It might be because it is non deterministic (at least for functions)
It's a limitation on the EXEC statement. See The curse and blessings of dynamic SQL for more information.