sql_variant fields and the WHERE-query

dagviggo
Member Posts: 20
I'm stuck with this anoying problem on a NAV 5.01 database in SQL server (the database has the "Case sensitive" option enabled).
The problem is that I'm unable to search for items that contain letters in the Item field "No." using the WHERE-statement. Searching for items with a numeric item no. is no problem.
This works:
SELECT [No_]
,[Description]
FROM [My Database].[dbo].[My Company$Item]
WHERE [No_] = 1000
This fails:
SELECT [No_]
,[Description]
FROM [My Database].[dbo].[My Company$Item]
WHERE [No_] = 'abc123'
I've tried all sorts for casts without any success, like this:
WHERE [No_] = cast('abc123' as varchar(20))
(I thought Navision fields of type "sql_variant" = varchar in SQL ... but I guess not ...)
It still returns 0 records :-(
The problem is that I'm unable to search for items that contain letters in the Item field "No." using the WHERE-statement. Searching for items with a numeric item no. is no problem.
This works:
SELECT [No_]
,[Description]
FROM [My Database].[dbo].[My Company$Item]
WHERE [No_] = 1000
This fails:
SELECT [No_]
,[Description]
FROM [My Database].[dbo].[My Company$Item]
WHERE [No_] = 'abc123'
I've tried all sorts for casts without any success, like this:
WHERE [No_] = cast('abc123' as varchar(20))
(I thought Navision fields of type "sql_variant" = varchar in SQL ... but I guess not ...)
It still returns 0 records :-(
0
Comments
-
dagviggo wrote:the database has the "Case sensitive" option enabled
SELECT [No_] ,[Description] FROM [My Database].[dbo].[My Company$Item] WHERE [No_] = 'ABC123'
0 -
Sorry, really really bad example from my part, I've searched using captiols also ;-)
A better example that doesn't work either:
WHERE [No_] = '07-0101-715-5'0 -
try this
SELECT [No_] ,[Description] FROM [My Database].[dbo].[My Company$Item] WHERE [No_] = '07-0101-715-5' COLLATE SQL_Latin1_General_CP1_CI_AS
0 -
No difference, I've also tried with "Latin1_General_100_CI_AS".
Note that I only have this problem with fields that are defined as "sql_variant" in SQL ... like the "No." and "No. 2" fields in the Item table.
Search on other fields works fine, like this one:
WHERE [Description] LIKE '%Flash%'0 -
I'd just open the table in SQL Server and look at how the values are formatted. If a query on 'ABC123' returns nothing, then there is no record with that value, and it must be formatted differently. If you're concerned about SQL data type, you can take a look at (but not change!!) the table design within SQL Server to see what the data type is.0
-
This really puzzles me, I installed the database on a different SQL server and there it works ?!?
A search like this (on the new server) works fine:
WHERE (No_ = '01-0206-411-7')
I can't find any differences between the two SQL servers, but there must be a problem with my local server.
PS! The field type CODE(20) from Navision is shown in SQL as "sql_variant"
I'll try to reinstall my local SQL server ...
Thanks for all help so far :-)0 -
FINALLY!!!
Can't believe I didn't try that before. The "cast"-statement should be on the other side ... of course:
WHERE cast([No_] as varchar(20)) = '09CARFST PORA'
Thanks Ola!
:-)0 -
Thanks for sharing a solution. I'm sure somebody will run into this in future.0
-
It always comes down to properly converting datatypes doesn't it
If the [No_] field is a 'sqlvariant' datatype, then your original WHERE clause could then be:
WHERE [No_] = cast('abc123' as sql_variant)
0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions