Wednesday, March 28, 2012

Merge Different Data Types in Query

I need to merge a numeric field (ID) with the varchar field (description) in
my query. Any idea how I can do this?
ID DESC
22 Text here
20 Text Here
Query Date to be: "(22) Text"
Thanks in advance!!You can use CAST, CONVERT or STR function to do this. For details of these
function, pl. refer to SQL Server Books Online.
SELECT '"' + CAST( id AS VARCHAR ) + SPACE( 3 ) +
desc + '"'
FROM tbl ;
Anith|||Anith,
This is awesome. I am looking into this function as we speak. However, I
ran your suggestion and it does exactly what I want except it has 'Select '
before the data as part of the field. Anyway around this?
Example:
"Select 1999 Short Description" instead of "1999 short description"
"Anith Sen" wrote:

> You can use CAST, CONVERT or STR function to do this. For details of these
> function, pl. refer to SQL Server Books Online.
> SELECT '"' + CAST( id AS VARCHAR ) + SPACE( 3 ) +
> desc + '"'
> FROM tbl ;
> --
> Anith
>
>|||I think you have quotes messed up somewhere. Without a table DDL & sample
data it is hard to point out the issue. Also it may have something to do
with the wrapper code/interface/client programming language you may be
using. Here is an example to check it out in Query Analyzer:
CREATE TABLE tbl ( id_ INT NOT NULL PRIMARY KEY,
descr_ VARCHAR(50) NOT NULL );
INSERT tbl SELECT 1999, 'short description' ;
SELECT '"' + CAST( id_ AS VARCHAR ) + SPACE( 3 ) + descr_ + '"'
FROM tbl ;
Anith|||Here is the Query I'm using in the view:
SELECT DISTINCT
TOP 100 PERCENT dbo.IssueSubType.IssueSubTypeID,
dbo.IssueSubType.IssueSubTypeTitle, dbo.IssueType.IssueTypeID,
'SELECT ' + CAST(dbo.Issue.IssueID AS VarChar) +
SPACE(2) + dbo.Issue.ShortDescription + '' AS Expr1, dbo.Issue.IssueID
FROM dbo.IssueType INNER JOIN
dbo.IssueSubType ON dbo.IssueType.IssueTypeID =
dbo.IssueSubType.IssueTypeID INNER JOIN
dbo.Issue ON dbo.IssueType.IssueTypeID =
dbo.Issue.IssueTypeID AND dbo.IssueSubType.IssueSubTypeID =
dbo.Issue.IssueSubTypeID
WHERE (dbo.IssueType.IssueTypeID = 16)
ORDER BY dbo.IssueSubType.IssueSubTypeID
"Anith Sen" wrote:

> I think you have quotes messed up somewhere. Without a table DDL & sample
> data it is hard to point out the issue. Also it may have something to do
> with the wrapper code/interface/client programming language you may be
> using. Here is an example to check it out in Query Analyzer:
> CREATE TABLE tbl ( id_ INT NOT NULL PRIMARY KEY,
> descr_ VARCHAR(50) NOT NULL );
> INSERT tbl SELECT 1999, 'short description' ;
> SELECT '"' + CAST( id_ AS VARCHAR ) + SPACE( 3 ) + descr_ + '"'
> FROM tbl ;
> --
> Anith
>
>|||Remove the part >> 'SELECT ' + << from your query.
Also, as a suggestion, remove TOP 100 & ORDER BY from the view. It offers
nothing beneficial & could cause optimization issues when used in complex
queries.
If an ordered resultset is the intention, consider using ORDER BY clause in
the query using the view instead.
Anith|||That was it. I tried various combinations of that but never that. Thanks
for your help!!!
"Anith Sen" wrote:

> Remove the part >> 'SELECT ' + << from your query.
> Also, as a suggestion, remove TOP 100 & ORDER BY from the view. It offers
> nothing beneficial & could cause optimization issues when used in complex
> queries.
> If an ordered resultset is the intention, consider using ORDER BY clause i
n
> the query using the view instead.
> --
> Anith
>
>

No comments:

Post a Comment