Solved

Change inserted value on trigger

Posted on 2014-10-24
6
104 Views
Last Modified: 2014-11-11
Hi guys,

Here is a section of my trigger...

INSERT INTO XYZ (SupplierCode, ActiveStatus, DateUpdated, UpdatedBy)
    SELECT
         i.Supplier, i.UserField2,CURRENT_TIMESTAMP, SYSTEM_USER
    FROM
         inserted as i LEFT OUTER JOIN [TriggerTablesAscent].[dbo].[SupplierStatus] tta ON i.Supplier = tta.SupplierCode

Open in new window


My ActiveStatus field is stored as bit. I have just noticed the UserField2 is a single char of Y or N. I need to convert Y into True and N into False before the insert.

How is it best to achieve this?

Thanks,
Dean.
0
Comment
Question by:deanlee17
  • 2
  • 2
  • 2
6 Comments
 
LVL 33

Expert Comment

by:ste5an
ID: 40401277
Use a CASE expression like CASE I.UserField2 WHEN = 'Y' THEN 1 WHEN 'N' THEN 0 END or IIF() when your on SQL Server 2012+.
0
 

Author Comment

by:deanlee17
ID: 40401717
Would the case statement sit above INSERT INTO?
0
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 500 total points
ID: 40401752
No, it would be inline.

If you want True and False, then...

INSERT INTO XYZ (SupplierCode, ActiveStatus, DateUpdated, UpdatedBy)
    SELECT
         i.Supplier,CASE I.UserField2 WHEN 'Y' THEN 'True' ELSE 'False' END,CURRENT_TIMESTAMP, SYSTEM_USER
    FROM
         inserted as i LEFT OUTER JOIN [TriggerTablesAscent].[dbo].[SupplierStatus] tta ON i.Supplier = tta.SupplierCode

Open in new window


If you want 1 or 0 then...

INSERT INTO XYZ (SupplierCode, ActiveStatus, DateUpdated, UpdatedBy)
    SELECT
         i.Supplier,CASE I.UserField2 WHEN 'Y' THEN 1 ELSE 0 END,CURRENT_TIMESTAMP, SYSTEM_USER
    FROM
         inserted as i LEFT OUTER JOIN [TriggerTablesAscent].[dbo].[SupplierStatus] tta ON i.Supplier = tta.SupplierCode

Open in new window


If you're going to get technical about it, then true should be -1 and false should be 0 (or not true, or everything else). What specific data types do you want? An integer or a string?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 33

Expert Comment

by:ste5an
ID: 40401755
Nope. Read the link and my post again. CASE is an expression not a statement.
0
 

Author Comment

by:deanlee17
ID: 40401779
Thanks for the feedback. Lee that worked for the case above, but should it also work for an update? My Insert works but my update fails:

  SET
        ActiveStatus = CASE i.UserField2 WHEN 'Y' THEN 'True' ELSE 'False' END,CURRENT_TIMESTAMP, SYSTEM_USER, DateUpdated = CURRENT_TIMESTAMP, UpdatedBy = SYSTEM_USER

Open in new window


The True or False is going into a bit field.

Thanks,
Dean
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 40401789
Then use 1 and 0 as true and false.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question