Showing posts with label exists. Show all posts
Showing posts with label exists. Show all posts

Monday, March 26, 2012

Replace() & upper () in stored procedure

Hi;

I have a stored procedure :

<code
Create Procedure ControlDept
(

@.DeptID nvarchar(10)
)
As
If Exists
(
Select DeptName From Departments Where
DeptID LIKE @.DeptID
)
Return 1
Else
Return 0

Now I want to apply replace and upper functions to DeptID in database before saying
"DeptID LIKE @.DeptID".

for example the parameter is :"D&V"
DeptID in database is:"d & v" //there are spaces

if I say DeptID LIKE @.DeptID nothing is found because of character nonmatching
So I have to apply replace & upper functions to the column DeptID in database

but how?
can you help me please??You probably don't need the "Upper" function since *most* SQL Server functions are case insensitive by default.

As for the spaces, not sure what to tell you there. That's a one-off solution that you will have to code manually. For instance, what happens when the value is "D& V"? There are 2 spaces now, and you'd have to code a check for that too.

Also, if you are using LIKE, you need to have a % character. For example:
DeptID LIKE @.DeptID + '%'

Tuesday, March 20, 2012

Replace

I know there is a function in MySQL where you can use "Replace" to update a row if one already exists and if the row does not exist it will insert a new row. Can this be done in MSSQL also? Thanksif exists (select * from tbl where ...)
update tbl set ...
else
insert tbl ...

update tbl set ...
select @.error = @.@.error, @.rowcount = @.@.rowcount
if @.@.error = 0 and @.rowcount = 0
insert tbl ...

I include the @.@.error in the second method to show that if you check one then you will lose the other so have to save them both in one statement.