Showing posts with label sql. Show all posts
Showing posts with label sql. Show all posts

Monday, September 28, 2015

Thursday, September 10, 2015

sql conditional insert,update,delete with merge command



declare @target table
(
id int,
lead_id int
)
insert into @target(id,lead_id) values(1,521),(2,521),(1,13),(2,13)
--select * from @target

declare @source table
(
id int,
lead_id int
)

insert into @source(id,lead_id) values(2,521),(3,521),(4,521)
--select * from @source

merge into @target as ttable
using @source as stable
on stable.lead_id = ttable.lead_id and stable.id = ttable.id
when NOT MATCHED then
INSERT (id,lead_id)
VALUES(stable.id,stable.lead_id)
WHEN NOT MATCHED BY SOURCE
AND ttable.lead_id IN(521) THEN
        DELETE;

select * from @target

Monday, July 13, 2015

SQL check null in where condition

 where (ISNULL(NULLIF(@tptCode,''),N'') =N'' OR msl.tpt_cd= @tptCode)     

Wednesday, July 8, 2015

SQL Like query

select login_id,first_name + ' ' + last_name as name,'0' as evuda,count(*) over () as [record_count]
from userinfo
where comp_id = @LoginCompId
and first_name like '%'+ isnull(ltrim(rtrim(@UserName)),first_name) +'%'  
or last_name like '%'+ isnull(ltrim(rtrim(@UserName)),last_name) +'%'

Thursday, June 18, 2015

SQL nested case when then

 CASE
WHEN alertcon.alert_type = 'E'
THEN
CASE
    WHEN alertgat.gateway_id = 'M' THEN escUser.login_id
    WHEN alertgat.gateway_id = 'E' THEN escUser.email_id
    ELSE ''
END
WHEN alertcon.alert_type = 'R'
THEN
CASE
   WHEN alertgat.gateway_id = 'M' THEN escUser.login_id
   WHEN alertgat.gateway_id = 'E' THEN escUser.email_id
    ELSE ''
END
END as Recepient