473,473 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Trigger For DateTime

Can someone give me a simple example of how to force a datetime value
to be stripped of the time portion when it is added/updated to a table.
I intend to use it for date only.

I figured a trigger was the way to do this but haven't gotten the
syntax right. Can someone please show me how? I've never written a
trigger before.

Thanks for any help.

Jul 23 '05 #1
8 2936
wa********@yahoo.com wrote:
Can someone give me a simple example of how to force a datetime value
to be stripped of the time portion when it is added/updated to a table.
I intend to use it for date only.

I figured a trigger was the way to do this but haven't gotten the
syntax right. Can someone please show me how? I've never written a
trigger before.


Saw this in a --CELKO-- post:

here is a proprietary but fast way to trim off the time:

CAST (FLOOR (CAST (my_date AS FLOAT)) AS DATETIME) = my_date

use CEILING() and you go to the next day.

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)
Jul 23 '05 #2
Yeah, that make sense. The question is how do you apply that alteration
to my_date when my date is part or a record being inserted into the DB
by some unknown program.

I need to write a trigger or something to run that code to modify the
insert/update.

Jul 23 '05 #3
(wa********@yahoo.com) writes:
Can someone give me a simple example of how to force a datetime value
to be stripped of the time portion when it is added/updated to a table.
I intend to use it for date only.

I figured a trigger was the way to do this but haven't gotten the
syntax right. Can someone please show me how? I've never written a
trigger before.


CREATE TRIGGER tbl_tri ON tbl FOR INSERT, UPDATE AS
UPDATE tbl
SET datecol = convert(char(8), t.datecol, 112)
FROM tbl t
JOIN inserted i ON t.keycol1 = i.keycol1
AND t.keycol2 = i.keycol2
...
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #4


Awesome Erland, thanks that's what I was looking for!

I've never seen a FROM / JOIN on an update statement before. I assume
the JOIN insures that only the rows in the table that match the rows in
the inserted table are modified rather than the whole table?

What's the FROM clause for though? Is it required? Or was it just so
you could alias the table to save typing?

Thanks again for your help.

Jul 23 '05 #5
(wa********@yahoo.com) writes:
Awesome Erland, thanks that's what I was looking for!

I've never seen a FROM / JOIN on an update statement before. I assume
the JOIN insures that only the rows in the table that match the rows in
the inserted table are modified rather than the whole table?

Welcome to SQL Server! :-)

FROM in UPDATE and DELETE is an extension to SQL Server (both Microsoft
and Sybase) which is very, very useful. This makes an UPDATE or DELETE
to be just like a SELECT. Thus:

UPDATE tbl
SET datecol = convert(char(8), t.datecol, 112)
FROM tbl t
JOIN inserted i ON t.keycol1 = i.keycol1
AND t.keycol2 = i.keycol2
...

Is just like:

SELECT datecol = convert(char(8), t.datecol, 112)
FROM tbl t
JOIN inserted i ON t.keycol1 = i.keycol1
AND t.keycol2 = i.keycol2
...

There are two drawbacks with this syntax:

1) It is not portable. If you want your code to run on other DBMS's
as well, don't use it.
2) If your joins are incomplete, so that the corresponding SELECT would
have duplicate rows, the result of the UPDATE statement is
unpredictable.

On the other hand, say that you have something like:

UPDATE tbl
SET col = (SELECT SUM(b.othercol)
FROM othertbl b
WHERE tbl.keycol = b.keycol)

If you rewrite this in the proprietary syntax:

UPDATE tbl
SET col = b.othercolsum
FROM tbl a
JOIN (SELECT keycol, othercolsum = SUM(othercol)
FROM othertbl
GROUP BY keycol) AS b ON a.keycol = b.keycol

My experience is that the last performs considerably better than the
ANSI-compliant query above. This is even clearer when you need to set
more than one column.
What's the FROM clause for though? Is it required? Or was it just so
you could alias the table to save typing?


Yes, FROM is needed in this syntax, but you don't actually have to
repeat the target table. This is legal:

UPDATE tbl
SET datecol = convert(char(8), tbl.datecol, 112)
FROM inserted i
WHERE tbl.keycol1 = i.keycol1
AND tbl.keycol2 = i.keycol2
...

But I don't like this, as it's not the same as a SELECT. By the way, if
I use a correlated subquery for the SET clause, I still put in a FROM
just to be able to use an alias.

Finally, I should admit that using EXISTS (SELECT * FROM inserted...) in
this case is cleaner, but writing joins requires less thinking. :-)
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #6

Wow, that's really excellent stuff. Thanks so much for explaining it
out.
I understood what you said except for EXISTS (SELECT * FROM
inserted...) being cleaner. I don't follow where that could be used
instead of a join. That's just going to be true all the time isn't it?

Jul 23 '05 #7
(wa********@yahoo.com) writes:
Wow, that's really excellent stuff. Thanks so much for explaining it
out.
I understood what you said except for EXISTS (SELECT * FROM
inserted...) being cleaner. I don't follow where that could be used
instead of a join. That's just going to be true all the time isn't it?


Nah, the WHERE clause was implied:

UPDATE tbl
SET datecol = convert(char(8), t.datecol, 112)
FROM tbl t
WHERE EXISTS (SELECT *
FROM inserted i
WHERE t.keycol1 = i.keycol1
AND t.keycol2 = i.keycol2
... )

Here I have the FROM clause only to be able to use an alias.

And, by the way, since this is a commen misunderstanding, I should say
that in SQL Server triggers fires once per *statement*. It's a common
mistake to assume that triggers fires once per row, which lures people
to write triggers that handles multi-row operation properly.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #8
Good point Erland. Well, I learned alot. Thanks so much for your time,
I appreciate it!

Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: efinney | last post by:
Hi, I'm a newbie to sql server and this may be a really dumb question for some you. I'm trying to find some examples of sql server triggers that will set columns (e.g. the created and modified...
9
by: Martin | last post by:
Hello, I'm new with triggers and I can not find any good example on how to do the following: I have two tables WO and PM with the following fields: WO.WONUM, VARCHAR(10) WO.PMNUM,...
7
by: rkrueger | last post by:
Given the following 3 Tables: CREATE TABLE ( NOT NULL , NOT NULL CONSTRAINT DEFAULT (getdate()), NULL , CONSTRAINT PRIMARY KEY CLUSTERED (
4
by: Peter Lin | last post by:
Dear all; I need to monitor a xml file so that I can update my data in hashtable, but the problem is it will trigger more than one event for a lastwrite action(I trigger this action by add...
3
by: jdprime | last post by:
Hi, I am trying to write a trigger that will run when someone updates the delivery charge within the specified table. It will work, but will only record one change to the table. My question is, if...
1
by: djdevx | last post by:
Dear all PostgreSQL xperts! Hi, I am newbie in PostgreSQL. I am currently developing an app that use PostgreSQL as a BackEnd Database. Now I am having problem with the function trigger since last...
9
by: Chico Che | last post by:
Have a table that has following fields (pkid, field1, field2, field3, field4). I need to create a trigger that will insert a row into another table with the pkid column that was updated. Any help...
1
by: allik7 | last post by:
SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO ALTER TRIGGER ON . INSTEAD OF INSERT AS DECLARE @CustNum int
1
by: asf93555 | last post by:
Running under SQL2000 I can not get an INSTEAD trigger to function. I've even copied the example directl from books online - no joy . . . Server: Msg 170, Level 15, State 1, Procedure...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.