I recently converted all ntext column types in my database to nvarchar(max).
I then ran EXECUTE sp_refreshview for the related views.
Yet when I run the following Parameter query (from classic ASP) on a view, I get an error:
Query:
SELECT CARID
FROM vwCAR
WHERE (1=1)
AND (Description LIKE '%'+ ? + '%')
ORDER BY CARID;
Error: The data types nvarchar and ntext are incompatible in the add operator (yet there are no longer any ntext columns!)
Yet I do NOT get this error if I run the same query directly SQL Server without the ? parameter as:
Query:
SELECT CARID
FROM vwCAR
WHERE (1=1)
AND (Description LIKE '%test%')
ORDER BY CARID;
I tried using Convert in the query, but had same result:
Query:
SELECT CARID
FROM vwCAR
WHERE (1=1)
AND (CONVERT(NVARCHAR(MAX), Description) LIKE N'%'+ ? + '%')
ORDER BY CARID;
What am I doing wrong?
Additional information:
I changed the type to nVarChar(4000) instead of (MAX) and everything works fine. This is a work around, but it solved the problem.
Just so I will know for the future, is it possible to run a parameter query using LIKE criteria on a nVarChar(Max) type column?
(Thank you #McNets for the post clean up .. I am new to this)
try to set before :
Set #value = '%' + #value + '%' ;
then use:
(description like #value )
I was using the wrong field type adLongVarWChar (203) in the parameter. Should have been using adVarWChar (202) for the nvarchar(max) type.
Confusion arose when I retrieved the field type directly from the database as noted below, it returned 203 for the nvarchar(max) type, so I assumed setting the parameter based on that type would work.
For each ofield in objRS.Fields
Redim Preserve FieldTypes(1,x)
FieldTypes(0,x) = ofield.type
FieldTypes(1,x) = ofield.definedsize
x = x + 1
Next
Related
I have a column in table which is stored in format:
{"field1":"val1","field2":"val4"}
{"field1":"val2","field2":"val5"}
{"field1":"val3","field2":"val6"}
I need to remove all field1 with values(e.g "field1":"val1","field1":"val2","field1":"val3" ) and result should be
{"field2":"val4"}
{"field2":"val5"}
{"field2":"val6"}
I am trying to acheive this via replace but stuck as in '"field1":"val1"' string val1 could be any value like null, some integer.
UPDATE emp SET col = REPLACE(col, '"field1":"val1"', '')
I am stuck due to this dynamic value of val1.
I would prefer to use the JSON_REMOVE function (MySQL) :
UPDATE emp
SET emp.col = JSON_REMOVE(emp.col, '$.field1');
You can also add a WHERE clause :
WHERE emp.col LIKE '%val6%';
References: MySQL JSON_REMOVE and MySQL JSON path
A blog post with examples: MySQL for your JSON
And a note about json path in MySQL:
Propery names in path must be double quoted if the property identifier contains interpunction (spaces, special characters, meta characters) bugs.mysql.com
You can do it like this:
SELECT SUBSTRING(Field, 1, INSTR(Field, '"field1"')) + SUBSTRING(Field, INSTR(Field, '"field2"'), LENGTH(Field)) FROM #Temp
I don't know if this works but this is the idea. (Can't test ATM)
Here is the MsSQL equivalent (works, just tested!):
SELECT SUBSTRING(Field, 0, CHARINDEX('"field1"', Field)) + SUBSTRING(Field, CHARINDEX('"field2"', Field), LEN(Field)) FROM #Temp
I'm looking for a way to select something in one column based on another column's data. I have 2 tables:
input_table - has a column called "year_of_birth"
result_table - has a column called "notes"
you see, that "notes" column might contain a year in there, something like "1980". I need to be able to select rows where the notes field contains a match to the year_of_birth field. I have my statement written like this so far:
select inp.year_of_birth, res.notes, res.result
from order_input inp join order_result res on inp.order_id = res.order_id
where res.result !='no match'
and res.notes like '%' + inp.year_of_birth + '%'
right now i'm getting an error thta says "conversion failed when converting the varchar value '%' to data type int. I'm not sure what i'm doing wrong because I have a similar statement i'm using that has this type of string in it that works...
You need to use CONCAT instead of +
res.notes like CONCAT('%', inp.year_of_birth, '%')
Try this:
select inp.year_of_birth, res.notes, res.result from order_input as inp join order_resultas res on inp.order_id = res.order_id where res.result <> 'no match' and res.notes like CONCAT('%', inp.year_of_birth, '%')
I have a column with datatype varchar using MySQL database. Suppose the value from a web form that gets saved in this column is : 2/4/2013
My search query goes like:
SELECT * FROM tbl WHERE colValue LIKE %2/4/2013%
But, it is crashing. For any other string am getting correct results. But, is it the forward slash which makes it crash. How can this be fixed ?
Regards !
since you want to select for specific date, why not use =
SELECT * FROM tbl WHERE colValue = '2/4/2013'
but if the data type of the column is DATE or DATETIME, use proper formatting although mysql automatically converts it,
SELECT * FROM tbl WHERE colValue = '2013-02-04'
For Using like operator you could use DATEPART() function...
select * from tbl
where (DATEPART(yy, colValue) = 2013
AND DATEPART(mm, colValue) = 04
AND DATEPART(dd, colValue) = 02)
Like this you can do like in SQL
Use an escape character for the /. The mysql escape character is the \.
Let's say we have following table.
UserId | Message
-------|-------------
1 | Hi, have a nice day
2 | Hi, I had a nice day
I need to have all { Hi,-have-a-nice-day-I-had } words separately.
Is there any way to do that ? What if I want to export words from whole database tables ?
Similar results would be also good.
try this:In Sql server 2005 or above
create table yourtable(RowID int, Layout varchar(200))
INSERT yourtable VALUES (1,'hello,world,welcome,to,tsql')
INSERT yourtable VALUES (2,'welcome,to,stackoverflow')
;WITH SplitSting AS
(
SELECT
RowID,LEFT(Layout,CHARINDEX(',',Layout)-1) AS Part
,RIGHT(Layout,LEN(Layout)-CHARINDEX(',',Layout)) AS Remainder
FROM YourTable
WHERE Layout IS NOT NULL AND CHARINDEX(',',Layout)>0
UNION ALL
SELECT
RowID,LEFT(Remainder,CHARINDEX(',',Remainder)-1)
,RIGHT(Remainder,LEN(Remainder)-CHARINDEX(',',Remainder))
FROM SplitSting
WHERE Remainder IS NOT NULL AND CHARINDEX(',',Remainder)>0
UNION ALL
SELECT
RowID,Remainder,null
FROM SplitSting
WHERE Remainder IS NOT NULL AND CHARINDEX(',',Remainder)=0
)
SELECT part FROM SplitSting ORDER BY RowID
SQLFIDDLE DEMO
Well, ok, here it goes.
In SQL Server you can use this...
SELECT word = d.value('.', 'nvarchar(max)')
FROM
(SELECT xmlWords = CAST(
'<a><i>' + replace([Message], ' ', '</i><i>') + '</i></a>' AS xml)
FROM MyMessageTbl) T(c)
CROSS APPLY c.nodes('/a/i') U(d)
And I hope that for MySQL you can use the same thing, using XML support - ExtractValue() etc.
EDIT: explanation
- replace([Message], ' ', '</i><i>') replaces e.g. 'my word' with 'my</i><i>word'
- then I add the beginning and the end of xml -> '<a><i>my</i><i>word</i></a>', so I have a valid xml... and cast it to xml type to be able to do something with it
- I select from that xml and shred xml nodes '/a/i' it to rows using CROSS APPLY c.nodes('/a/i');
alias rows using U(d), so one 'i' maps to column d (e.g. 'my')
- d.value('.', 'nvarchar(max)') extracts node content and casts it to character type
I have a table called SerialNos with the following columns:
Id, Parent_Id, SerialNo
There are different SerialNo on a Parent_Id
Like:
Id Parent_Id SerialNo
1 16 abc
2 16 def
3 23 hij
4 23 klm
5 23 nop
I just want to retrieve comma separated SerialNos. for particular Parent_Id
e.g. If Parent_Id is passed 16,then O/p should be : 'abc,def'
and if Parent_Id is passed 23,then O/p should be : 'hij,klm,nop'
here is a nice hack:
DECLARE #csv varchar(1000)
SELECT #csv = COALESCE(#csv+',','') + SerialNo
FROM SerialNos
WHERE Parent_Id = 23
SELECT #csv
Depending upon the database server you can use group_concat. For example if you are using mysql group_concat with a separator of "," will give you the result you are after.
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
If you are using a different database server check its documentation it is likely that it will have something similar. Please also keep in mind that some databases don't have this function.
Sorry just saw that your post was tagged sql-server2008. If sql server 2008 does not have group_concat or something similar try this link
http://blog.shlomoid.com/2008/11/emulating-mysqls-groupconcat-function.html
Sorry yet another edit, this will help.
http://groupconcat.codeplex.com/
for sql-server 2008 you can use the following query(with your table name):
select distinct STUFF(ISNULL((SELECT ', ' + x.SerialNo
FROM TableA x
WHERE x.Parent_Id= t.Parent_Id
GROUP BY x.SerialNo
FOR XML PATH (''), TYPE).value('.','VARCHAR(max)'), ''), 1, 2, '')
from TableA t
where Parent_Id = 16
Well i could advice my blog post here, but unfortunately its not in English :).
Here is something similar, just without CLR.
There i am aware of at least 4 solutions:
CLR grouping function (well, code is in english)
Can use cursors (not very fast)
Can use something similar to cursors
Can use XML
So one solution (not the fastest, but easy to write):
Create Function fn_MyFunction
(
#Parent_Id int
)
Returns NVarChar(1000)
As
Begin
Declare #txt NVarChar(1000)
SELECT #txt = COALESCE(#txt + ';' + SerialNo, SerialNo)
FROM dbo.Table Where Parent_Id = #Parent_Id
Return #txt
End