Showing posts with label employee. Show all posts
Showing posts with label employee. Show all posts

Thursday, March 29, 2012

Case Statement Problem

I am trying to convert this to sql syntax

SELECT [2007_hours].proj_name, [2007_hours].task_name, [2007_hours].Employee,

IIf(Mid([task_name],1,3)='PTO','PTO_Holiday',

IIf(Mid([task_name],1,7)='Holiday','PTO_Holiday',

IIf(Mid([proj_name],1,9) In ('9900-2831','9900-2788'),'II Internal',

IIf(Mid([proj_name],1,9)='9900-2787','Sales',

IIf(Mid([proj_name],1,9)='9910-2799','Sales',

IIf(Mid([proj_name],1,9)='9920-2791','Sales',

)

)

)

)

) AS timeType, Sum([2007_hours].Hours) AS SumOfHours

Here is what i am trying to do:

select case

when SUBSTRING(task_name, 1, 3)= 'PTO'

then Replace(task_name,'PTO','PTO_Holiday') and Sum(td_hours) AS SumOfHours (this will give me an error)

when SUBSTRING(task_name, 1, 7)= 'Holiday' and SUBSTRING(task_name, 1, 3)= 'PTO'

then Replace(task_name,'Holiday','PTO_Holiday')and Sum(td_hours) AS SumOfHours (this will give me an error)

ELSE task_name

Thank you

Your "Sum(td_hours) as SumOfHours" must be moved out of the CASE statement; something like this maybe:

Code Snippet

select case
when SUBSTRING(task_name, 1, 3)= 'PTO'
then Replace(task_name,'PTO','PTO_Holiday')
when SUBSTRING(task_name, 1, 7)= 'Holiday' and SUBSTRING(task_name, 1, 3)= 'PTO'
then Replace(task_name,'Holiday','PTO_Holiday')
ELSE task_name
end,
Sum(td_hours) AS SumOfHours

|||Thank you

Sunday, March 25, 2012

Case sensitive search

I use to apply varbinary clause to go for a case sensitive search.Suppose for the case
use pubs
SELECT * FROM EMPLOYEE WHERE
convert(varbinary,LNAME)=convert(varbinary,'Chang' )

I am getting the correct result in every way.

But when I apply the same rule in the following way
use northwind
SELECT * FROM employees WHERE
convert(varbinary,lastname)=convert(varbinary,'Kin g')

I am not getting any result wheather I pass the correct case sensetive parameter or not.

Can anybody help me why there is a difference in these two results?
One thing I like to mention in the first case the data type is varchar and in the latter case the datatype is nvarchar.Is this not applicable to the fields of type nvarchar.

Subhasishmaybe there's some blanks after LastName :
convert(varbinary,Rtim(LastName))=...|||Indeed nvarchar is the problem

convert(varbinary,lastname)=convert(varbinary,N'Ki ng')

try
SELECT lastname, convert(varbinary,lastname), convert(varbinary,'King')
FROM employees|||this will work for you :

Select * From employees
where
convert(varbinary,convert(varchar,lastname))=
convert(varbinary,'Buchanan')

converting the nvarchar in varchar and then into a varbinary

Thursday, March 8, 2012

cascade update / foreign key

Hi!
For the sake of simplicity, I have three tables, Employee, Department and
Work

Employee >-- Department
\ /
\ /
^ ^
Work

The Work table have two columns, empno and depno and consists that the
employee has worked on another department.

Here is my scripts:
create table employee (empno int not null primary key, depno int not null)
create table department (depno int not null primary key)
create table work (empno int not null, depno int not null)

alter table employee add constraint fk_employee_department foreign key
(depno)
references department (depno)
on update cascade

alter table work add constraint fk_work_employee foreign key (empno)
references employee (empno)
on update cascade

alter table work add constraint fk_work_department foreign key (depno)
references department (depno)
on update cascade

My problem is the last command. SQL Server responds:
Server: Msg 1785, Level 16, State 1, Line 1
Introducing FOREIGN KEY constraint 'fk_work_department' on table 'work' may
cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON
UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

But I want the depno in the work table to be updated when a department.depno
changes a value.

Does anyone have a suggestion on how to overcome this problem?

Thanks in advance

Best regards,
Gunnar Vyenli
EDB-konsulent as
NORWAYHi,

SQL statement that you had provided is generating circular refrence in
updates
Workaround is to use triger instead of foreign key integrity.

Comment the one of foriegn key sql and use triger.

Note this trigger are not tested. but is only for your reference and
other work around for problem.

/*alter table work add constraint fk_work_employee foreign key (empno)
references employee (empno)
on update cascade
go
*/

create trigger trgUpdateEmpNoInDept
on employee
for update
as
set nocount on

declare @.NewEmpno int , @.OldEmpno int

select @.NewEmpno = inserted.empno from inserted
select @.OldEmpno = deleted.empno from deleted

update work set work.empno = @.NewEmpno
where work.empno = @.OldEmpno
if @.@.rowcount=0 or @.@.error<>0
begin
Rollback tran
end

set nocount off
go

create trigger trgCheckEmpExists
on Work
For Update
as

set nocount on

declare @.NewEmpno int
select @.NewEmpno = inserted.empno from inserted

If Not Exists (Select * from employee where empno = @.NewEmpno )
Begin
RAISERROR ('Empno do not exists', 16, 1)
Rollback Tran
End
set nocount off

go

Thanks & Regards, Amit

"Gunnar Vyenli" <gv@.edbkonsulent.no> wrote in message news:<3f6a24cb$1@.news.broadpark.no>...
> Hi!
> For the sake of simplicity, I have three tables, Employee, Department and
> Work
> Employee >-- Department
> \ /
> \ /
> ^ ^
> Work
> The Work table have two columns, empno and depno and consists that the
> employee has worked on another department.
> Here is my scripts:
> create table employee (empno int not null primary key, depno int not null)
> create table department (depno int not null primary key)
> create table work (empno int not null, depno int not null)
> alter table employee add constraint fk_employee_department foreign key
> (depno)
> references department (depno)
> on update cascade
> alter table work add constraint fk_work_employee foreign key (empno)
> references employee (empno)
> on update cascade
> alter table work add constraint fk_work_department foreign key (depno)
> references department (depno)
> on update cascade
> My problem is the last command. SQL Server responds:
> Server: Msg 1785, Level 16, State 1, Line 1
> Introducing FOREIGN KEY constraint 'fk_work_department' on table 'work' may
> cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON
> UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
> But I want the depno in the work table to be updated when a department.depno
> changes a value.
> Does anyone have a suggestion on how to overcome this problem?
> Thanks in advance
> Best regards,
> Gunnar Vyenli
> EDB-konsulent as
> NORWAY|||Thank you very much!
-Gunnar

"AMIT" <amitb@.zenithinfotech.com> wrote in message
news:f3093a66.0309182236.2313bc10@.posting.google.c om...
> Hi,
>
> SQL statement that you had provided is generating circular refrence in
> updates
> Workaround is to use triger instead of foreign key integrity.
> Comment the one of foriegn key sql and use triger.
> Note this trigger are not tested. but is only for your reference and
> other work around for problem.
> /*alter table work add constraint fk_work_employee foreign key (empno)
> references employee (empno)
> on update cascade
> go
> */
>
> create trigger trgUpdateEmpNoInDept
> on employee
> for update
> as
> set nocount on
> declare @.NewEmpno int , @.OldEmpno int
> select @.NewEmpno = inserted.empno from inserted
> select @.OldEmpno = deleted.empno from deleted
> update work set work.empno = @.NewEmpno
> where work.empno = @.OldEmpno
> if @.@.rowcount=0 or @.@.error<>0
> begin
> Rollback tran
> end
> set nocount off
> go
> create trigger trgCheckEmpExists
> on Work
> For Update
> as
> set nocount on
> declare @.NewEmpno int
> select @.NewEmpno = inserted.empno from inserted
> If Not Exists (Select * from employee where empno = @.NewEmpno )
> Begin
> RAISERROR ('Empno do not exists', 16, 1)
> Rollback Tran
> End
> set nocount off
> go
>
> Thanks & Regards, Amit
> "Gunnar Vyenli" <gv@.edbkonsulent.no> wrote in message
news:<3f6a24cb$1@.news.broadpark.no>...
> > Hi!
> > For the sake of simplicity, I have three tables, Employee, Department
and
> > Work
> > Employee >-- Department
> > \ /
> > \ /
> > ^ ^
> > Work
> > The Work table have two columns, empno and depno and consists that the
> > employee has worked on another department.
> > Here is my scripts:
> > create table employee (empno int not null primary key, depno int not
null)
> > create table department (depno int not null primary key)
> > create table work (empno int not null, depno int not null)
> > alter table employee add constraint fk_employee_department foreign key
> > (depno)
> > references department (depno)
> > on update cascade
> > alter table work add constraint fk_work_employee foreign key (empno)
> > references employee (empno)
> > on update cascade
> > alter table work add constraint fk_work_department foreign key (depno)
> > references department (depno)
> > on update cascade
> > My problem is the last command. SQL Server responds:
> > Server: Msg 1785, Level 16, State 1, Line 1
> > Introducing FOREIGN KEY constraint 'fk_work_department' on table 'work'
may
> > cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or
ON
> > UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
> > But I want the depno in the work table to be updated when a
department.depno
> > changes a value.
> > Does anyone have a suggestion on how to overcome this problem?
> > Thanks in advance
> > Best regards,
> > Gunnar Vyenli
> > EDB-konsulent as
> > NORWAY

Wednesday, March 7, 2012

Carriage Return ,VBCrLf in PDF

Hi,
I have a report render out in PDF. There's a Employee Preference
column which has "<BR>" to display each item on a new line. It works
ok by using Replace "BR" with VbCrLf, but there is a problem when
viewing in PDF.
For example: Red <BR> Blue <BR> Green, should be displayed as:
Red
Blue
Green
but after replace BR with a VbCrLf, the result displayed in multple
lines but there's always a mystery empty character in front of the
second line.
Red
Blue
Green.
ANyone have any ideas?
ThanksMike,
I am not sure if this will solve your problem; but I use
"System.Environment.NewLine" in my report to produce a newline.
So for example:
=Fields!Color.Value & System.Environment.NewLine & Fields!Color.Value ....
See if that works.
Rob Cuscaden
"MikeT" wrote:
> Hi,
> I have a report render out in PDF. There's a Employee Preference
> column which has "<BR>" to display each item on a new line. It works
> ok by using Replace "BR" with VbCrLf, but there is a problem when
> viewing in PDF.
> For example: Red <BR> Blue <BR> Green, should be displayed as:
> Red
> Blue
> Green
> but after replace BR with a VbCrLf, the result displayed in multple
> lines but there's always a mystery empty character in front of the
> second line.
> Red
> Blue
> Green.
> ANyone have any ideas?
> Thanks
>