Showing posts with label SQL 2005. Show all posts
Showing posts with label SQL 2005. Show all posts

Thursday, December 13, 2012

How to parse comma delimited string into IN Clause in SQL Server

Hi Folks,

Today i was trying to pass multi-value parameter into my select query in SQL Server. To accomplish this i stored the multi-value parameter in a comma delimited string. Then, i want to pass it in my IN clause of the select statement. here is an example to what i want to accomplish:

Delcare @myVar varchar(500)
Set @myVar='1,2,3,4,5,7'

Select * from Employee
where EmployeeId IN (@myVar)

You will get this error:
Msg 245, Level 16, State 1, Line 7
Conversion failed when converting the varchar value '1,2,3,4,5,7' to data type int.


It makes sense, because i have a varchar that holds all my values and i want to pass those in the IN clause that only accepts integer values!.

The solution for this problem is to parse this string into set of integers and pass it back to your query in your IN clause. For this reason, i have created a function that return a table of 1 column of type int. This will be passed back to the select statement i have mentioned above.

The function code to parse comma delimited string into set of integers:

-- =============================================
-- Author: Mostafa Elzoghbi
-- Create date: 12/10/2012
-- Description: Parse a string into set of numbers
-- =============================================

CREATE FUNCTION [dbo].[ParseCommaDelimitedString]
( @CommaSeparatedStr
nvarchar(1000) =NULL)
RETURNS @myTable TABLE ([Id] [int] NOT NULL)
AS
BEGIN
declare
@pos int
declare @piece varchar(500)
-- Need to tack a delimiter onto the end of the input string if one doesn't exist
if right(rtrim(@CommaSeparatedStr ),1) <> ','
set @CommaSeparatedStr = @CommaSeparatedStr + ','
set @pos = patindex('%,%' , @CommaSeparatedStr )
while @pos <> 0
begin
set @piece = left(@CommaSeparatedStr , @pos - 1)
-- You have a piece of data, so insert it, print it, do whatever you want to with it.
insert @myTable
select @piece

set @CommaSeparatedStr = stuff(@CommaSeparatedStr , 1, @pos, '')
set @pos = patindex('%,%' , @CommaSeparatedStr )
end
RETURN
END
-- =============================================

What you need after that to re-write your query as follows:

Select *

from Employee
where EmployeeId IN
( select * from ParseCommaDelimitedString(@myVar))

Now, you will be able to pass any comma delimited string, varchar,nvarchar to this function and it returns table of integer that you can set in the IN clause of you select statement or any other T-SQL statements you works with your logic.

Hope this helps.

--ME

Saturday, November 01, 2008

Integrate your SQL Reporting Services Reports in MOSS 2007

Hi Folks,

Out of the box add-on for MOSS 2007 which you can deploy your Sql Reporting Services Reports in MOSS 2007 Sites,Download the Add-on now and you can add Report Viewer webpart and integrate your created reports inside SharePoint Sites ,Download Link :

http://www.microsoft.com/downloads/details.aspx?FamilyID=1e53f882-0c16-4847-b331-132274ae8c84&displaylang=en

Note: This build working only with SQL 2005 SP2

Hope this helps.

Regards,
Mostafa arafa

Thursday, November 29, 2007

Recover your DB from Log Files in MS SQL DBMS

Hi Folks:

I got a call from one of my friends "Fady" regarding one of the guys dropped his tables without taking a backup of his DB.

This is my Support and answer to him.

"
Dear My Firend Fady,

Regarding your question yesterday, for that person who Dropped his tables, anyways this email has good news for him, as I told you he can recover the DB from its log file, simply pass these Urls to him, to recover your DB from Log File.

Urls:

http://sqlserver2000.databases.aspfaq.com/how-do-i-recover-data-from-sql-server-s-log-files.html

http://www.apexsql.com/sql_tools_log.asp

"

Hope this help you guys.......


Regards,
Moustafa arafa

Tuesday, August 14, 2007

ASP.Net 2.0 Data controls Tutrials

Hi all,

New Data Tutorials for asp.net 2.0 and VS 2005 has been published for free......Must Read them all...............

http://weblogs.asp.net/scottgu/archive/2007/08/08/great-new-asp-net-2-0-data-tutorials-published.aspx

Hope this helps.

Regards,
Moustafa arafa

Tuesday, July 17, 2007

Top 10 Gems in SQL 2005

Hi All,

Excellent article which highlight some features on SQL 2005 which weren't get the spot on the webcasts,articles and SQL Demos.

http://www.microsoft.com/technet/prodtechnol/sql/bestpractice/gems-top-10.mspx

Thanks my Friend ali Riza for his useful post.
http://www.dotnetking.com/TechnicalComments.aspx?LogID=299

Hope this helps.......... :)

Regards,
Moustafa arafa

Tuesday, July 10, 2007

Recover SQL 2005 DB passwords

Hi Folks,

I got an urgent request of one of my friends asking for a solution to recover SQL 2005 DB passwords,the situation was as follows:

He took a backup of SQL 2005 DB and set the password option on SQL 2005,and when he tried to restore the DB,SQL 2005 request the password which he forgot it.

My First question was: did he protect the bak file from SQL 2005 or not ? after i got that he protect the DB from SQL 2005,i started to search for third party solution to fix this problem since this DB contains very sensitive information.

I found a third party to recover the SQL 2005 user accounts passwords ,try it now :
http://www.safe-install.com/programs/sql-password.html

The security hole which i discovered that this program can't recover the SQL server passwords if the SQL Engine was configured to use only Windows Authentication Mode; and this is considered as an important security point when we use MS Products with Windows Authentication mode ONLY.

From this post,you have to take care of your SQL installation which mode is an appropriate to your organization.
Hope this helps.

In case you put the password on the bak file,then no way to do any kind of restore to this DB;see more on this article : http://mssqltips.com/tip.asp?tip=1108


Regards,
Moustafa arafa