|
-
January 5th, 2009, 03:46 PM
#1
Query Results with commas b/w fields
Ok, so I want to execute this query:
Select user_id, f_name, l_name
from tableABC;
And I want it to return in this format:
abc123, Joe, Smith
sid938, Mel, Ward
iic837, Al, Jamison
I want the returned fields to be separated by commas. How would I modify the sql query to make this happen? I've seen it done elsewhere, but I'm not a dba guy and thought someone here might be able to quickly tell me how to do this.
Not sure if it matters, but this is on sql server.
Thanks!
-
January 6th, 2009, 12:24 AM
#2
Re: Query Results with commas b/w fields
I want the returned fields to be separated by commas.
I'm not sure what UR trying to do. Where should be executed this query? In SQL explorer (or in any other "client SQL tools")? In program that U write?
The solution depends on that!
If its SQL explorer than U have to select "generate results in text" and choose "comma separated" as a format specification.
OR U can also do it the hard way by concatenating strings like that:
Code:
Select IsNull(user_id,'')+','+IsNull(f_name,'')+','+IsNull(l_name,'')
from tableABC
Best regards,
Krzemo
Last edited by Krzemo; January 6th, 2009 at 12:26 AM.
-
January 6th, 2009, 04:44 PM
#3
Re: Query Results with commas b/w fields
Eh, that didn't work.
It's a proprietary tool that comes with the application that allows us to access the database without needing to log into the db server. It sits on the app server. I just drop a file with the sql statement into the tool and it executes the sql statement for me. It's just plain old sql.
-
January 6th, 2009, 05:29 PM
#4
Re: Query Results with commas b/w fields
Did you try the code posted by Krzemo?
 Originally Posted by Krzemo
Code:
Select IsNull(user_id,'')+','+IsNull(f_name,'')+','+IsNull(l_name,'')
from tableABC
-
January 8th, 2009, 08:57 AM
#5
Re: Query Results with commas b/w fields
 Originally Posted by dee-u
Did you try the code posted by Krzemo?
Heh...that's what I was talking about. It didn't seem to work.
-
January 8th, 2009, 09:04 AM
#6
Re: Query Results with commas b/w fields
From the looks of it it should work, what did you mean by it didn't seem to work? No result were returned or you encountered some error?
-
January 8th, 2009, 03:54 PM
#7
Re: Query Results with commas b/w fields
Returned an error
Succesfully connected to Microsoft SQL Server.
Time: Thu Jan 08 15:53:07.388 2009
[Select IsNull(user_name,'')+','+IsNull(full_name,'')+','+IsNull(login_time,'') from ip.ip_user]
ERROR: Unable to perform SQL [Select IsNull(user_name,'')+','+IsNull(full_name,'')+','+IsNull(login_time,'') from ip.ip_user].
Processed 2 lines and 0 SQL statements.
Time: Thu Jan 08 15:53:11.701 2009, Elapsed: 4.313 seconds
Done processing.
Seems like it's not working to me. Any other thoughts? I wonder if this proprietary tool can't handle stuff like this....
-
January 8th, 2009, 09:56 PM
#8
Re: Query Results with commas b/w fields
That could be a problem of that tool you are using, the sql looks fine to me.
-
January 9th, 2009, 11:06 AM
#9
Re: Query Results with commas b/w fields
Yeah, that's what it looks like. Thanks for the suggestion, though.
-
March 26th, 2009, 03:40 AM
#10
Re: Query Results with commas b/w fields
In SQL Server 2000
Code:
Select Cast(IsNull(user_Id,0) as Varchar)+','+IsNull(user_name,'')+','+IsNull(full_name,'')+','+
Cast(IsNull(login_time,'12am') as Varchar) from ip.ip_user
For datetime type of data you can also use convert function to get date in specific format.
In your statement IsNull(login_time,'') is culprit when login_time is null you can not substitute with string null the substitute value has to be same datatype
Last edited by ComITSolutions; March 26th, 2009 at 03:44 AM.
Encourage the efforts of fellow members by rating
Lets not Spoon Feed and create pool of lazy programmers
- ComIT Solutions
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|