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
Related
I'm attempting to validate some column headings before the import of a monthly data set. I've set up an Execute SQL Task that's supposed to retrieve the column headings of the prior month's table and store it in Header_Row as a single string with the field names separated by commas. The query runs just fine in SQL Server, but when running in SSIS, it throws the following error:
"The type of the value (Empty) being assigned to variable 'User:Header_Row' differs from the current variable type (String)."
1) Does this mean that I'm not getting anything back from my query?
2) Is there another method I should be using in SSIS to get the query results I'm looking for?
3) Is there an issue with me using the variable reference in my query as a portion of a string? I think the answer is yes, but would like to confirm, as my variable was still empty after changing this.
Original Query:
SELECT DISTINCT
STUFF((
SELECT
',' + COLUMN_NAME
FROM
db_Analytics.INFORMATION_SCHEMA.COLUMNS aa
WHERE
TABLE_NAME = 'dt_table_?'
ORDER BY
aa.ORDINAL_POSITION
FOR
XML PATH('')
), 1, 1, '') AS Fields
FROM
db_Analytics.INFORMATION_SCHEMA.COLUMNS a;
EDIT: After changing the variable to cover the full table name, I have a new error saying "The value type (__ComObject) can only be converted to variables of the type Object."
Final Query:
SELECT DISTINCT
CAST(STUFF((
SELECT
',' + COLUMN_NAME
FROM
db_Analytics.INFORMATION_SCHEMA.COLUMNS aa
WHERE
TABLE_NAME = ?
ORDER BY
aa.ORDINAL_POSITION
FOR
XML PATH('')
), 1, 1, '') As varchar(8000)) AS Fields
FROM
db_Analytics.INFORMATION_SCHEMA.COLUMNS a;
You are attempting to parameterize your query. Proper query parameterization is useful for avoiding SQL Injection attacks and the like.
Your query is looking for a TABLE_NAME that is literally 'dt_table_?' That's probably not what you want.
For laziness, I'd just rewrite it as
DECLARE #tname sysname = 'dt_table_' + ?;
SELECT DISTINCT
STUFF((
SELECT
',' + COLUMN_NAME
FROM
db_Analytics.INFORMATION_SCHEMA.COLUMNS aa
WHERE
TABLE_NAME = #tname
ORDER BY
aa.ORDINAL_POSITION
FOR
XML PATH('')
), 1, 1, '') AS Fields
FROM
db_Analytics.INFORMATION_SCHEMA.COLUMNS a;
If that's not working, you might need to use an Expression to build out the query.
I'm really pretty sure that this is your problem:
TABLE_NAME = 'dt_table_?'
I'm guessing this is an attempt to parameterize the query, but having the question mark inside the single-quote will cause the question mark to be taken literally.
Try like this instead:
TABLE_NAME = ?
And when you populate the variable that you use as the parameter value, include the 'dt_table_' part in the value of the variable.
EDIT:
Also in your ResultSet assignment, try changing "Fields" to "0" in the Result Name column.
There are two issues with the query above:
1) The query in the task was not properly parameterized. I fixed this by putting the full name of the prior month's table into the variable.
2) The default length of the result was MAX, which was causing an issue when SSIS would try to put it into my variable, Header_Row. I fixed this by casting the result of the query as varchar(8000).
Thanks for the help everyone.
I have 'zipcode' column with value 19707-1234. I have to remove hyphen in between and change the value to 197071234. Can anyone please help me, how to do this using sql query irrespective of index?
You can simply use the replace function:
SELECT REPLACE(zipcode, '-', '')
We a have built in replace function,you can use that
Declare #zipcode varchar(50)='19707-1234'
Select replace(#zipcode,'-','')
Use inbuilt REPLACE function of SQL.
SELECT REPLACE('19707-1234','-','');
In mysql I have used below query for replacing hyphen
*SELECT concat( SUBSTRING_INDEX('0125-3256','-',1),SUBSTRING_INDEX('0125-3256','-',-1)) as replace_hypen*
Output of this query = '01253256'
So you can use for zipcode code column
SELECT concat( SUBSTRING_INDEX(zipcode,'-',1),SUBSTRING_INDEX(zipcode,'-',-1)) as replace_hypen
I am using FOR XML PATH in my stored procedure to concatenate rows.But it gives me performance issue.can any one please tell me a function that i can use instead of for xml path.
You can create a CLR Aggregate function to do concatenation.
Example here: http://www.mssqltips.com/sqlservertip/2022/concat-aggregates-sql-server-clr-function/
You can concatenate storing the string in a variable, just with a simple select.
Example concatenating with a comma:
DECLARE #MyConcatenateData VARCHAR(1000)
SELECT #MyConcatenateData = COALESCE(#MyConcatenateData + ',', '') + MyTextColumn
FROM MyTable
I'm tring to run a find and replace query on some sql data using Management Studio. I basically want to remove the word FREE from any content.
I tried running this query;
UPDATE Table_1
SET ContentDetails = REPLACE(ContentDetails, 'FREE', '')
WHERE (ContentDetails LIKE '%FREE%')
But I get an error saying that data type text is invalid for argument 1 of replace function.
Since you have a text column, you would need to use updatetext, which is painful, at best. However, you can cast contentdetails as a varchar(max), and you'll be peachy.
update table_1
set contentdetails = replace(cast(contentdetails as varchar(max)), 'FREE', '')
where contentdetails like '%FREE%'
Moreover, I highly recommend that you look into converting that column from text to varchar(max). It, along with ntext and image, is a currently deprecated data type, which will be removed at some point in the future of SQL Server.
Try
UPDATE Table_1
SET ContentDetails = REPLACE(CAST(ContentDetails as VARCHAR), 'FREE', '')
WHERE (ContentDetails LIKE '%FREE%')
although it might cut the data off where the value is longer than what fits in a VARCHAR.
Updated due to comment
I think all you need to do is cast your ContentDetails field as a varchar with the length of the field i.e.
UPDATE Table_1
SET ContentDetails = REPLACE(CAST(ContentDetails as VARCHAR(100)), 'FREE', '')
WHERE ContentDetails LIKE '%FREE%'
I basically have an xml column, and I need to find and replace one tag value in each record.
For anything real, I'd go with xpaths, but sometimes you just need a quick and dirty solution:
You can use CAST to turn that xml column into a regular varchar, and then do your normal replace.
UPDATE xmlTable SET xmlCol = REPLACE( CAST( xmlCol as varchar(max) ), '[search]', '[replace]')
That same technique also makes searching XML a snap when you need to just run a quick query to find something, and don't want to deal with xpaths.
SELECT * FROM xmlTable WHERE CAST( xmlCol as varchar(max) ) LIKE '%found it!%'
Edit: Just want to update this a bit, if you get a message along the lines of Conversion of one or more characters from XML to target collation impossible, then you only need to use nvarchar which supports unicode.
CAST( xmlCol as nvarchar(max) )
To find a content in an XML column, look into the exist() method, as described in MSDN here.
SELECT * FROM Table
WHERE XMLColumn.exist('/Root/MyElement') = 1
...to replace, use the modify() method, as described here.
SET XMLColumn.modify('
replace value of (/Root/MyElement/text())[1]
with "new value"
')
..all assuming SqlServer 2005 or 2008. This is based on XPath, which you'll need to know.
update my_table
set xml_column = replace(xml_column, "old value", "new value")