OLEDB Jet 4.0 SQL Character Limit - sql

I am attempting to generate lines for an SDF (Space Delimited File). I am creating these lines from a DBASE IV DBF file using an OLEDB adapter with extended properties DBASEIV to get at the data. My data column output is 425 characters long after padding, I am placing this into a datagridview in VB.NET to display it.
However when I run my query, while it seems to execute correctly the resultant field is restricted to 256 characters. The longest individual field I am reading is 35 characters and I am returning a dataset with 2 fields, the barcode and the SDF line. As I understand it OLEDB Jet 4.0 tries to guess the type based on the first 8 rows, however as all rows are equal length for the data column (425 chars) I don't get why it is choosing the smaller field type. I assume it is because my field is a generated one using string concatenation. I have included the horrible SQL at the bottom of this question. So my question is how can I get the full 425 character output? Or is there a way I can specify the datatype for my own field as memo?
SELECT scan,
RIGHT('0000000000000' + trim(cstr(scan)), 13) +
LEFT(trim(cstr(name)) + ' ', 35) +
LEFT(trim(cstr(name)) + ' ', 16) +
' ' +
' ' +
' ' +
'1 ' +
'0.00 ' +
'0.00 ' +
'1' +
'0.00 ' +
'0.01 ' +
'0.00 ' +
'F' +
'2' +
'0.00 ' +
'0.00 ' +
' ' +
' ' +
' ' +
'SALS' +
' ' +
' ' +
LEFT(trim(cstr(plof)) + ' ', 13) +
' ' +
' ' +
'0.00 ' +
'0.00 ' +
'0.00 ' +
'F' +
'T' +
'001' +
' ' +
'T' +
'01' +
' ' +
' ' +
' ' +
' ' +
'F' +
'F' +
' ' +
' ' +
'0 ' +
'0.00 ' +
' ' +
'0 ' +
'0.00 ' +
'0.00 ' +
'0.00 ' +
'0.00 ' +
'1 ' +
'1 ' +
'1 ' +
'0.00 ' +
'0.00 ' +
'0.00 ' +
'0.00 ' +
'0.00 ' +
'0.00 ' +
' ' +
' ' +
' '
as STTEMPLINE
from salus where cstr(scan) in (select distinct cstr(scan) from nonscan)
Thanks in advance for any help.

This is a known issue where data types are guessed based on data in the first few rows.
See this post...
Load Excel sheet into DataTable but only the first 256 chars per cell are imported,
Also, see this post for an explanation of how OLEDB deals with mixed data types...
http://social.msdn.microsoft.com/Forums/pl-PL/csharplanguage/thread/0404d003-5bfb-44f9-8e6b-aebdfce24875

Related

STRING_AGG Issue

I was working with SQL Server 2017 and I used the following query,
select
'select s.' + string_agg (c.source_column + ', t.' + c.target_column, ', ') +
' from ' + t.source_table + ' s' +
' join ' + t.target_table + ' t' +
' on ' + string_agg('t.' + c.target_column + ' = s.' + c.source_column, ' and ') +
';' as query
from configuration_tables t
join configuration_columns c on c.id_configuration_tables = t.id_configuration_tables
group by t.source_table, t.target_table
order by t.source_table, t.target_table;
The query is used to get the list of columns in a string for each table name.
I have to move to 2016 due to some issues, I modified the query as below,
select
distinct 'select ' + STUFF((select ',' + 's.[' + c1.source_column + '],t.[' + c1.target_column + ']'
from recon_configuration_columns c1 where c1.id_recon_configuration_table = c.id_recon_configuration_table FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') ,1,1,'')
+ ' from ' + t.source_table + ' s' +
' join ' + t.target_table + ' t' +
' on ' + STUFF((select ' and ' + 's.[' + c1.source_column + '] = t.[' + c1.target_column + '] ' from recon_configuration_columns c1
where c1.id_recon_configuration_table = c.id_recon_configuration_table FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') ,1,5,'')
from
recon_configuration_tables t
join recon_configuration_columns c on c.id_recon_configuration_table = t.id_recon_configuration_table
I would like to know whether the query I changed is a standard one, also it wont create any problems in long run and always both queries should return the same results.
Thanks for your support

when using "SELECT varname = something + something etc. I cannot then do "WHERE varname = ..."

I have:
SELECT KEYWORDS = CAST(USRN AS VARCHAR(15)) + ' ' +
RTRIM(SD) + ' ' + RTRIM(NL.LOCALITY_NAME) + ' ' +
RTRIM(NT.TOWN_NAME) + ' ' + RTRIM(NA.AUTHORITY_NAME)
That gives me what looks like a column, but is not:
I want to have it so my code only selects the rows from KEYWORDS that match whatever the user is typing. Normally, if KEYWORDS was a column, I would write:
SELECT .... WHERE KEYWORDS = '%whateverTheUserIsTyping%'
but I cannot because keywords is not a real column and it is telling me that it does not exist.
How do I get around this? thanks
The column alias KEYWORDS isn't visible to the SQL Engine at the time that the WHERE clause is evaluated. You can just repeat your CAST statment, though.
SELECT
KEYWORDS = CAST(USRN AS VARCHAR(15)) + ' ' +
RTRIM(STREET_DESCRIPTOR) + ' ' + RTRIM(NSG_LOCALITY.LOCALITY_NAME) + ' ' +
RTRIM(NSG_TOWN.TOWN_NAME) + ' ' + RTRIM(NSG_AUTHORITY.AUTHORITY_NAME)
FROM yourTable
WHERE
CAST(USRN AS VARCHAR(15)) + ' ' +
RTRIM(STREET_DESCRIPTOR) + ' ' + RTRIM(NSG_LOCALITY.LOCALITY_NAME) + ' ' +
RTRIM(NSG_TOWN.TOWN_NAME) + ' ' + RTRIM(NSG_AUTHORITY.AUTHORITY_NAME)
LIKE '%whateverTheUserIsTyping%'
You can use derived table with an alias (here Q) and get the result from that by filtering in WHERE clause:
SELECT Q.KEYWORDS FROM (
SELECT KEYWORDS = CAST(USRN AS VARCHAR(15)) + ' ' +
RTRIM(STREET_DESCRIPTOR) + ' ' + RTRIM(NSG_LOCALITY.LOCALITY_NAME) + ' ' +
RTRIM(NSG_TOWN.TOWN_NAME) + ' ' + RTRIM(NSG_AUTHORITY.AUTHORITY_NAME)
) AS Q
WHERE Q.KEYWORDS LIKE '%whateverTheUserIsTyping%'
Because you can't use the column alias in the WHERE clause as you mentioned. Also instead of the KEYWORDS = '%whateverTheUserIsTyping%', you can use LIKE operator.

using CASE in SQL with update statement

I am trying to write a case statement something like this. Can someone help with the syntax
UPDATE A
CASE WHEN ATTDESC = 'ABC' THEN SET A.DESC = PG.VAL + ' - ' + LD.DESC + ' - ' + GH.PL3NAME
WHEN ATTDESC = 'DEF' THEN SET A.DESC = PG.VAL + ' - ' + LD.DESC + ' - ' + GH.PL3NAME
END
FROM ATTR A, PRODUCT PG, Global GH, Look LD
A CASE expression doesn't control flow; it returns a single value. So something like this:
...
SET [DESC]=CASE [ATTDESC]
WHEN 'ABC' THEN PG.VAL + ' - ' + LD.DESC + ' - ' + GH.PL3NAME
WHEN 'DEF' THEN PG.VAL + ' - ' + LD.DESC + ' - ' + GH.PL3NAME
ELSE [DESC]
END
FROM
...

using sql - Is not null in a select statement

I can't seem to figure out how to use the opposite of isnull or ifnull statements in sql. I need to say if a.Error1 is not null -- then print the ' - ' and the + CHAR(13)+CHAR(10). Basically There should be no dash or no new line break if the a.Error1 comes back null. So print the information if the field isn't null.
select a. ....
' - ' + a.Error1 + CHAR(13)+CHAR(10) +
' - ' + a.Error2 + CHAR(13)+CHAR(10) +
' - ' + a.Error3 + CHAR(13)+CHAR(10) +
' - ' + a.Error4 + CHAR(13)+CHAR(10) +
' - ' + a.Error5 + CHAR(13)+CHAR(10) +
' - ' + a.Error6 as 'error_message'
...
from table1 a
For example if for a given record error1, 2 and 5 returned output I would like the output to be as follows:
- Error1: There was a ...
- Error2: ....
- Error5: The data was ...
If no errors existed for that row it should simply be an empty/null field.
You can use CASE:
SELECT a. ....
(CASE WHEN a.Error1 IS NOT NULL
THEN ' - ' + a.Error1 + CHAR(13)+CHAR(10)
ELSE ''
END) +
(CASE WHEN a.Error2 IS NOT NULL
THEN ' - ' + a.Error2 + CHAR(13)+CHAR(10)
ELSE ''
END) +
(CASE WHEN a.Error3 IS NOT NULL
THEN ' - ' + a.Error3 + CHAR(13)+CHAR(10)
ELSE ''
END) +
...etc
Yes! i know i'm like 5 years too late but i too enountered this problem.
It's weird how it doesn't exist some kind of !ISNULL() but whatever.
Try this for a cleaner code:
select a. ....
IIF(a.Error1 IS NOT NULL, ' - ' + a.Error1 + CHAR(13)+CHAR(10) , '') as Error1,
IIF(a.Error1 IS NOT NULL, ' - ' + a.Error1 + CHAR(13)+CHAR(10) , '') as Error2
from table1 a
Learn more about IIF() function : SQL Server IIF Function
The COALESCE function does what you want here. The result of COALESCE is the first NOT NULL value it is passed. Below we use '', which is distinct from NULL so that the outer + is always applied to NOT NULL strings.
e.g.
select a. ....
COALESCE( ' - ' + a.Error1 + CHAR(13)+CHAR(10), '' ) +
COALESCE( ' - ' + a.Error2 + CHAR(13)+CHAR(10), '' ) +
COALESCE( ' - ' + a.Error3 + CHAR(13)+CHAR(10), '' ) +
COALESCE( ' - ' + a.Error4 + CHAR(13)+CHAR(10), '' ) +
COALESCE( ' - ' + a.Error5 + CHAR(13)+CHAR(10), '' ) +
COALESCE( ' - ' + a.Error6 , '' ) as 'error_message'
...
from table1 a
SELECT (CASE WHEN a.Error1 IS NOT NULL
THEN ' - ' + a.Error1 + CHAR(13)+CHAR(10) +
ELSE a.Error1
END) +
(CASE WHEN a.Error2 IS NOT NULL
THEN ' - ' + a.Error2 + CHAR(13)+CHAR(10) +
ELSE a.Error2
END) +
.....etc

space in a select statement in dynamic query

I have a dynamic query like this :
SET #str_Query = 'SELECT SIM.Item_ID,
SIM.Item_Description,
SU.Short_Description AS Unit,
SIM.Std_Lead_Time,'+
'' ''+' AS Last_Purchase_Rate
FROM FKMS_Item_Master AS SIM
INNER JOIN FKMS_STP_Units SU
ON SIM.Item_Purchase_Unit=SU.Unit_Id' +
' WHERE ' + #str_Condition +
' AND SIM.Location_Id =' + CAST(#aint_Location_Id AS VARCHAR(10)) +
' AND SIM.Item_Deleted =0
AND SIM.Approved_On IS NOT NULL'
+' ORDER BY SIM.Item_Description'
I want to retrieve space as Last_Purchase_Rate
It is showing syntax error in the portion of '' ''+' AS Last_Purchase_Rate
when I execute this query.
If I print this dynamic query, query seems correct. It shows as AS Last_Purchase_Rate with space before AS. Please help.
I would write
...SIM.Std_Lead_Time, '' '' AS Last_Purchase_Rate...
instead of
...SIM.Std_Lead_Time,'+'' ''+' AS Last_Purchase_Rate...
Why not use NULL instead of space and then handle the result in your app?
I.e.,
SET #str_Query = 'SELECT SIM.Item_ID,
SIM.Item_Description,
SU.Short_Description AS Unit,
SIM.Std_Lead_Time,
NULL AS Last_Purchase_Rate, -- and so on.
You could also use CHAR(32):
SET #str_Query = 'SELECT SIM.Item_ID,
SIM.Item_Description,
SU.Short_Description AS Unit,
SIM.Std_Lead_Time,
CHAR(32) AS Last_Purchase_Rate, -- and so on.
You did not escape all quotes.
A working version of your statement would be
SET #str_Query = 'SELECT SIM.Item_ID,
SIM.Item_Description,
SU.Short_Description AS Unit,
SIM.Std_Lead_Time,'
+ ''' '''
+ ' AS Last_Purchase_Rate
FROM FKMS_Item_Master AS SIM
INNER JOIN FKMS_STP_Units SU
ON SIM.Item_Purchase_Unit=SU.Unit_Id' +
' WHERE ' + #str_Condition +
' AND SIM.Location_Id =' + CAST(#aint_Location_Id AS VARCHAR(10)) +
' AND SIM.Item_Deleted =0
AND SIM.Approved_On IS NOT NULL'
+' ORDER BY SIM.Item_Description'
but I find that with a little reformatting, the error is easier to spot
SET #str_Query =
'SELECT SIM.Item_ID '
+ ', SIM.Item_Description '
+ ', SU.Short_Description AS Unit '
+ ', SIM.Std_Lead_Time '
+ ', '' ''' + ' AS Last_Purchase_Rate '
+ 'FROM FKMS_Item_Master AS SIM '
+ ' INNER JOIN FKMS_STP_Units SU '
+ ' ON SIM.Item_Purchase_Unit=SU.Unit_Id '
+ ' WHERE ' + #str_Condition
+ ' AND SIM.Location_Id = ' + CAST(#aint_Location_Id AS VARCHAR(10))
+ ' AND SIM.Item_Deleted =0 '
+ ' AND SIM.Approved_On IS NOT NULL '
+ ' ORDER BY SIM.Item_Description '
Try using tsql function SPACE(1)