Showing posts with label characters. Show all posts
Showing posts with label characters. Show all posts

Tuesday, March 27, 2012

Getting the actual length for columns in a result set

Is there a way to get the length (i.e. number of characters, bytes ...
) in the metadata information for a resultset?
I'm using low level ODBC calls, and my problem is that both
SQLDescribeCol and SQLColAttribute return the column size for the
length. This doesn't sound bad, but when reading the data I need to
have allocated a buffer big enough to hold the data.
More specifically, if I'm using the Text datatype and the result set
contains the text "hello" the size will always = 2^31. Does anyone
know of a low level ODBC call which would return 5, prior to fetching
the data?
Thanks in advance, -geoffThe problem is that the length in the metadata describes the maximum length
of the column. A varchar(5) column will report 5 when you call
SQLDescribeCol(). The metadata for a column is the same for every row in
the table (instance of the column). What you are interested in is the
length of the actual data in the column (or the length of a particular
instance of the column). This information is considered part of the actual
data in the row, rather than part of the column's metadata.
For non-BLOB columns, you typically want to allocate a buffer that can hold
the maximum length of the column (sure there are exceptions). For BLOB
columns you really should use SQLGetData() (rather than SQLBindCol()). If
you call SQLGetData() with a NULL TargetValuePtr parameter, but a valid
StrLen_or_IndPtr parameter, then SQLGetData() will return the length of the
data that is available for the column. Then you can use that value to
allocate a buffer and call SQLGetData() again to read the column.
Brannon Jones
Developer - MDAC
This posting is provided "as is" with no warranties and confers no rights.
"Geoff" <geoffpollard@.go.com> wrote in message
news:595a2518.0402111842.698363b7@.posting.google.com...
> Is there a way to get the length (i.e. number of characters, bytes ...
> ) in the metadata information for a resultset?
> I'm using low level ODBC calls, and my problem is that both
> SQLDescribeCol and SQLColAttribute return the column size for the
> length. This doesn't sound bad, but when reading the data I need to
> have allocated a buffer big enough to hold the data.
> More specifically, if I'm using the Text datatype and the result set
> contains the text "hello" the size will always = 2^31. Does anyone
> know of a low level ODBC call which would return 5, prior to fetching
> the data?
> Thanks in advance, -geoff|||I suspected there was another way. However, SQLGetData seems to fail
when I pass a NULL TargetValuePtr. The SQLRETURN == -1, and the
SQLGetDiagRec states "Invalid argument value". Here is a code snippet:
SQLRETURN sql_status_code;
SQLINTEGER namelen;
for (int i=0;i<numColumns;i++)
{
// fails
sql_status_code =
SQLGetData(hstmt,i+1,SQL_C_CHAR,NULL,0,&namelen);
// works
SQLCHAR test[255];
sql_status_code =
SQLGetData(hstmt,i+1,SQL_C_CHAR,test,255
,&namelen);
}
Prior to this the following statements returned SQL_SUCCESS:
SQLAllocHandle(SQL_HANDLE_STMT, hdbc, hstmt);
// need to use server-side cursors to handle multiple queries
SQLSetStmtAttr(hstmt,SQL_ATTR_CURSOR_TYP
E,
(SQLPOINTER)SQL_CURSOR_STATIC,SQL_IS_INT
EGER);
SQLPrepare(hstmt, (SQLCHAR*) s_SQL, SQL_NTS);
SQLExecute(hstmt);
for (int i=0;i<numColumns;i++)
SQLDescribeCol(...) //read column names
SQLFetch(hstmt)
--[ here is where I've been calling SQLGetData ]--
Any help would be greatly appreciated. Thanks in advance, -geoff
"Brannon Jones [MS]" <branjo@.nospam.microsoft.com> wrote in message news:<ureN$Ba8DHA.2760@.T
K2MSFTNGP09.phx.gbl>...
> The problem is that the length in the metadata describes the maximum lengt
h
> of the column. A varchar(5) column will report 5 when you call
> SQLDescribeCol(). The metadata for a column is the same for every row in
> the table (instance of the column). What you are interested in is the
> length of the actual data in the column (or the length of a particular
> instance of the column). This information is considered part of the actua
l
> data in the row, rather than part of the column's metadata.
> For non-BLOB columns, you typically want to allocate a buffer that can hol
d
> the maximum length of the column (sure there are exceptions). For BLOB
> columns you really should use SQLGetData() (rather than SQLBindCol()). If
> you call SQLGetData() with a NULL TargetValuePtr parameter, but a valid
> StrLen_or_IndPtr parameter, then SQLGetData() will return the length of th
e
> data that is available for the column. Then you can use that value to
> allocate a buffer and call SQLGetData() again to read the column.
> --
> Brannon Jones
> Developer - MDAC
> This posting is provided "as is" with no warranties and confers no rights.
>
> "Geoff" <geoffpollard@.go.com> wrote in message
> news:595a2518.0402111842.698363b7@.posting.google.com...|||Sorry I was slightly incorrect. You need to pass a valid pointer for
TargetValuePtr, but you pass a zero length. We will return the available
length in the StrLen_or_IndPtr parameter, and the return code will be
SQL_SUCCESS_WITH_INFO (SQLGetDiagRec will return: "[Microsoft][ODBC SQL
Server Driver]String data, right truncation", SQLState: 01004). Then you
allocate the buffer and call again.
Let me know if that doesn't help.
Brannon Jones
Developer - MDAC
This posting is provided "as is" with no warranties and confers no rights.
"Geoff" <geoffpollard@.go.com> wrote in message
news:595a2518.0402140153.5a248edc@.posting.google.com...
> I suspected there was another way. However, SQLGetData seems to fail
> when I pass a NULL TargetValuePtr. The SQLRETURN == -1, and the
> SQLGetDiagRec states "Invalid argument value". Here is a code snippet:
> SQLRETURN sql_status_code;
> SQLINTEGER namelen;
> for (int i=0;i<numColumns;i++)
> {
> // fails
> sql_status_code =
> SQLGetData(hstmt,i+1,SQL_C_CHAR,NULL,0,&namelen);
> // works
> SQLCHAR test[255];
> sql_status_code =
> SQLGetData(hstmt,i+1,SQL_C_CHAR,test,255
,&namelen);
> }
> Prior to this the following statements returned SQL_SUCCESS:
> SQLAllocHandle(SQL_HANDLE_STMT, hdbc, hstmt);
> // need to use server-side cursors to handle multiple queries
> SQLSetStmtAttr(hstmt,SQL_ATTR_CURSOR_TYP
E,
> (SQLPOINTER)SQL_CURSOR_STATIC,SQL_IS_INT
EGER);
> SQLPrepare(hstmt, (SQLCHAR*) s_SQL, SQL_NTS);
> SQLExecute(hstmt);
> for (int i=0;i<numColumns;i++)
> SQLDescribeCol(...) //read column names
> SQLFetch(hstmt)
> --[ here is where I've been calling SQLGetData ]--
> Any help would be greatly appreciated. Thanks in advance, -geoff
> "Brannon Jones [MS]" <branjo@.nospam.microsoft.com> wrote in message
news:<ureN$Ba8DHA.2760@.TK2MSFTNGP09.phx.gbl>...
length
in
actual
hold
If
the
rights.

Wednesday, March 21, 2012

Getting rid of unwanted characters!

Hello Gurus, I’ve a table that has a column with a text data type. When th
ey
imported the data from a different system some non ASCII character slipped
into the table. What would be the best way to get rid of them?
For example: A man ne£ds ??help. to A man needs help
thanks in advance.Hi
If this is a one off, you may want to try:
UPDATE myStrings
SET str = STUFF(str,PATINDEX('%[^A-Z ]%',str),1 ,'')
WHERE PATINDEX('%[^A-Z ]%',str) > 0
WHILE @.@.ROWCOUNT > 0
BEGIN
UPDATE myStrings
SET str = STUFF(str,PATINDEX('%[^A-Z ]%',str),1 ,'')
WHERE PATINDEX('%[^A-Z ]%',str) > 0
END
John
"KB" wrote:

> Hello Gurus, I’ve a table that has a column with a text data type. When
they
> imported the data from a different system some non ASCII character slipped
> into the table. What would be the best way to get rid of them?
> For example: A man ne£ds ??help. to A man needs help
> thanks in advance.
>|||http://www.aspfaq.com/2445
"KB" <KB@.discussions.microsoft.com> wrote in message
news:F11D992A-CC05-4776-B43A-A3AB8412F291@.microsoft.com...
> Hello Gurus, I’ve a table that has a column with a text data type. When
> they
> imported the data from a different system some non ASCII character slipped
> into the table. What would be the best way to get rid of them?
> For example: A man ne£ds ??help. to A man needs help
> thanks in advance.
>|||Great Help! Thank you gentlemen.
"KB" wrote:

> Hello Gurus, I’ve a table that has a column with a text data type. When
they
> imported the data from a different system some non ASCII character slipped
> into the table. What would be the best way to get rid of them?
> For example: A man ne£ds ??help. to A man needs help
> thanks in advance.
>

Monday, March 19, 2012

Getting results of stored proc into temporary table

I have a need to get the results of a stored procedure into a temporary
table. The results of the query will be larger than 8000 characters so I
can't use a variable. Following is what is being done in the stored
procedure (findorphans):
==========
PRINT 't1'
SELECT mco_itn,chg_dt,chg_in FROM t1 LEFT JOIN t2 ON t2.mco_itn=t1.mco_itn
WHERE t2.mco_itn IS NULL
PRINT 't3'
SELECT mco_itn,chg_dt,chg_in FROM t3 LEFT JOIN t4 ON t4.mco_itn=t3.mco_itn
WHERE t4.mco_itn IS NULL
==========
I tried the following:
==========
CREATE TABLE ##msgtext (msgtext text)
INSERT INTO ##msgtext exec test..mxw_findorphans
...
DROP TABLE ##msgtext
==========
I end up getting an error as follows:
==========
t1
Server: Msg 213, Level 16, State 7, Procedure findorphans, Line 2
Insert Error: Column name or number of supplied values does not match table
definition
==========
Ultimately I am going to send the results in an email. I had it working
fine but the messages were getting cutoff due to the 8000 character limit.
In the documentation for xp_sendmail they had an example of sending a
message > 8000 which is where I am going with this. I think the problem may
be that line 2 returns 0 rows and for some reason is causing the problem.
Can anyone point me at what I am doing wrong?
TIA
MarkMark,
your temp table need to mirror exactly the columns the stored procedures
will return, so looking at it, your temp table should contain columns
mco_itn,chg_dt,chg_in
I'm also not sure if you can insert multiple resultsets into a temp table.
so you may have to make the queries in the stored procedure into a UNION.
Have you considered a user-defined function which can return a TABLE ? Look
them up in BOL.
Hope that helps.
Damien
"Mark Butler" wrote:

> I have a need to get the results of a stored procedure into a temporary
> table. The results of the query will be larger than 8000 characters so I
> can't use a variable. Following is what is being done in the stored
> procedure (findorphans):
> ==========
> PRINT 't1'
> SELECT mco_itn,chg_dt,chg_in FROM t1 LEFT JOIN t2 ON t2.mco_itn=t1.mco_itn
> WHERE t2.mco_itn IS NULL
> PRINT 't3'
> SELECT mco_itn,chg_dt,chg_in FROM t3 LEFT JOIN t4 ON t4.mco_itn=t3.mco_itn
> WHERE t4.mco_itn IS NULL
> ==========
> I tried the following:
> ==========
> CREATE TABLE ##msgtext (msgtext text)
> INSERT INTO ##msgtext exec test..mxw_findorphans
> ....
> DROP TABLE ##msgtext
> ==========
> I end up getting an error as follows:
> ==========
> t1
> Server: Msg 213, Level 16, State 7, Procedure findorphans, Line 2
> Insert Error: Column name or number of supplied values does not match tabl
e
> definition
> ==========
> Ultimately I am going to send the results in an email. I had it working
> fine but the messages were getting cutoff due to the 8000 character limit.
> In the documentation for xp_sendmail they had an example of sending a
> message > 8000 which is where I am going with this. I think the problem m
ay
> be that line 2 returns 0 rows and for some reason is causing the problem.
> Can anyone point me at what I am doing wrong?
> TIA
> Mark
>
>|||Looks like you can have multiple resultsets from a procedure:
CREATE PROC usp_test
AS
SELECT 1
SELECT 2
GO
DROP TABLE #test
CREATE TABLE #test ( value1 INT )
GO
INSERT INTO #test
EXEC usp_test
SELECT *
FROM #test
DROP PROC usp_test
GO
"Damien" wrote:
> Mark,
> your temp table need to mirror exactly the columns the stored procedures
> will return, so looking at it, your temp table should contain columns
> mco_itn,chg_dt,chg_in
> I'm also not sure if you can insert multiple resultsets into a temp table.
> so you may have to make the queries in the stored procedure into a UNION.
> Have you considered a user-defined function which can return a TABLE ? Lo
ok
> them up in BOL.
> Hope that helps.
>
> Damien
> "Mark Butler" wrote:
>|||When I send the output from the stored procedure directly into xp_sendmail
it is a text based message formated like the following:
=========
t1
mco_itn chg_dt chg_in
-- -- --
t3
mco_itn chg_dt chg_in
-- -- --
=========
I was expecting that to be true the way I was doing it but as you pointed
out that must be false.
Is there another or better way?
Do I need to CAST and concatinate the fields so they are one?
Is there a way to send the results to a text file that I can then attatch to
the email?
Thanx for any suggestions.
Mark
"Damien" <Damien@.discussions.microsoft.com> wrote in message
news:7F9C8D7E-06CF-4272-A690-34C86EC517D3@.microsoft.com...
> Mark,
> your temp table need to mirror exactly the columns the stored procedures
> will return, so looking at it, your temp table should contain columns
> mco_itn,chg_dt,chg_in
> I'm also not sure if you can insert multiple resultsets into a temp table.
> so you may have to make the queries in the stored procedure into a UNION.
> Have you considered a user-defined function which can return a TABLE ?
> Look
> them up in BOL.
> Hope that helps.
>
> Damien
> "Mark Butler" wrote:
>

Wednesday, March 7, 2012

getting length of 8 characters

Claim number (string)
CF060001
CF060001A
CF060001B
AV000001
AV000212F
AV000001F
FD232122
FD232122G
SD223213
SD223213H

I only want to get records, which have length of 8 characters.
So output will be CF060001, AV000001, FD232122, and SD223213

Anyone can help me to write this in sql?WHERE LEN(ClaimNumber) = 8

or possibly you will need

WHERE LEN(RTRIM(ClaimNumber)) = 8

Roy Harvey
Beacon Falls, CT

On 13 Dec 2006 12:55:20 -0800, "TGEAR" <ted_gear@.hotmail.comwrote:

Quote:

Originally Posted by

>Claim number (string)
>CF060001
>CF060001A
>CF060001B
>AV000001
>AV000212F
>AV000001F
>FD232122
>FD232122G
>SD223213
>SD223213H
>
>I only want to get records, which have length of 8 characters.
>So output will be CF060001, AV000001, FD232122, and SD223213
>
>Anyone can help me to write this in sql?

|||Roy Harvey (roy_harvey@.snet.net) writes:

Quote:

Originally Posted by

WHERE LEN(ClaimNumber) = 8
>
or possibly you will need
>
WHERE LEN(RTRIM(ClaimNumber)) = 8


Since len() does not count trailing blanks, rtrim is redudant here.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||On Wed, 13 Dec 2006 22:43:30 +0000 (UTC), Erland Sommarskog
<esquel@.sommarskog.sewrote:

Quote:

Originally Posted by

>Since len() does not count trailing blanks, rtrim is redudant here.


Thanks for the education! 8-)

Roy|||Roy Harvey napisal(a):

Quote:

Originally Posted by

WHERE LEN(ClaimNumber) = 8
>
or possibly you will need
>
WHERE LEN(RTRIM(ClaimNumber)) = 8


.... or in case N data, and automaticly avoid problem with spaces:

where datalength(ClaimNumber)/2 = 8

Matik|||Matik (marzec@.sauron.xo.pl) writes:

Quote:

Originally Posted by

Roy Harvey napisal(a):

Quote:

Originally Posted by

>WHERE LEN(ClaimNumber) = 8
>>
>or possibly you will need
>>
> WHERE LEN(RTRIM(ClaimNumber)) = 8


>
... or in case N data, and automaticly avoid problem with spaces:
>
where datalength(ClaimNumber)/2 = 8


Not really sure what you mean. len() counts characters and ignores
trailing spaces, so it is an ideal function to use in this case, as
it works the same with varchar and nvarchar data. (It does not work
with text/ntext though.)

datalength on the other hand counts bytes and includes trailing spaces,
so with datalength you need to trim and you need to know whether you
are working with varchar or nvarchar data.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx