Home >> Blog

iHwy Development Blog

The iHwy team shares their musings about their development experiences.

Creating delimited list using TSQL

Posted on November 24, 2008 10:56 by Jack
This is a neat little trick I picked up. If you've ever needed to create a delimited list from a single column of data stored in SQL Server and felt like it required unnecessary arm wrestling due to SQL syntax limitations, this could be handy for you next time, too:

Let's say you have a column called "lastName" and want to get a comma-delimited list of last names. Here's a way:

-- set up a var for the list

declare @list varchar(1000)
set @list = ''


-- create the delimited list using a query

select @list = @list + lastName + ', ' from person

-- trim off the trailing comma and space if necessary

if @list <> '' set @list = left(@list, len(@list) - 1)

-- take a look at the results

print @list

Here are some example results:

Rubble, Flintstone, Jetson, Munster

Neat trick.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Here's a short script that allows a Microsoft SQL Server administrator to restrict the databases that a SQL Server user will see when they access the database server using SQL Server Management Studio. You'd think that this info was easy to find, but trust us, it's not. It's out there, but usually requires weeding through a lot of irrelevant info to get to the main points:

1. First, using Object Explorer in SQL Management Studio, expand the Security > Logins node of the tree. Then right click and select "New Login..."

2. Enter a login name and some SQL Server authentication info (password) for the user. Leave the "Default database" at "master". Do NOT select any user database in this step. Do NOT go to the User Mapping page in the Login creation dialog to assign the user to any db's, either. The user you are creating will not be the user on any specific databases.

3. Now run this for each db you want your new user to be able to see when they login to SQL Management Studio:

use [someDB] -- use the real db name
go
sp_changedbowner 'theUser' -- use the new username
go

4. Next, deny the new user "public" access to ALL databases:

use master
go
deny VIEW any DATABASE to theUser
go

That pretty much does it. You should now be able to login to SQL Management Studio as that user and only see the databases that they own.

Think of it this way: by making them the owner, you're specifying what databases they can see in SQL Management Studio. Don't mix this up with setting an existing database user as db_owner. It's not the same thing.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

We've released an new extension for the jQuery Validation plugin that provides a more thorough credit card validation routine. It allows you to pass in the type of credit card being used, so the validation can also check card number prefix and length. This extension also allows spaces and dashes to be entered into the card number (it's up to you to strip them out when they get submitted). The extension also does mod-10 validation of the card numbers, like the core creditcard validation routine does.

Info, examples, documentation and download links are available in the Labs area of our site:

http://www.ihwy.com/Labs/jquery-validate-credit-card-extension.aspx

 

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5