Solved

Change inserted value on trigger

Posted on 2014-10-24
6
103 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

809 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