Solved

SQL 2000 -  Query help

Posted on 2014-01-28
10
541 Views
Last Modified: 2014-01-28
SQL 2000 - I need to write a update query that will take the input date time value:


1. If the DateTime value is between 12.00 AM to 3.00 AM then set the date to previous day with time 12.59.59 PM.

how to write an Update query selecting from another query?

Soemthing like this:
update Table1
select datetime from Table1

Thanks
0
Comment
Question by:mani_sai
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 39815891
update <table>
set dateTimer = dateadd(d, -1, dateTimer)
where
datepart(hh,datetimer) between 0 and 2 or
(datepart(hh,datetimer) = 3 and datepart(mi, datetimer) = 0)
0
 
LVL 4

Expert Comment

by:ravikantninave
ID: 39815901
update TestTable set mdate = (Select Case When DATEPART(HOUR, AppMstDOJ ) >=0 and DATEPART(HOUR, AppMstDOJ ) <=3 then CAST(CAST(CONVERT(DATE, AppMstDOJ ,101) AS VARCHAR) + ' 12' + ':' +  '59' + ':' + '59' AS DATETIME) else AppMstDOJ  end from AppMast where AppMstID = 272)

Open in new window

0
 
LVL 8

Author Comment

by:mani_sai
ID: 39815905
Thanks.

Date is moved to previous date , I also want Time portion to be set 23:59:59...

any Idea?

Thanks
0
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 39815906
update <table>
set dateTimer = cast(LEFT(CONVERT(nvarchar, DATEADD(DAY, -1, dateTimer), 120), 11) + '23:59:59' as datetime)where
datepart(hh,datetimer) between 0 and 2 or
(datepart(hh,datetimer) = 3 and datepart(mi, datetimer) = 0)

If you already ran the first update just don't do the dateadd again:

update <table>
set dateTimer = cast(LEFT(CONVERT(nvarchar, dateTimer, 120), 11) + '23:59:59' as datetime)where
datepart(hh,datetimer) between 0 and 2 or
(datepart(hh,datetimer) = 3 and datepart(mi, datetimer) = 0)
0
 
LVL 4

Expert Comment

by:ravikantninave
ID: 39815915
update TestTable set mdate = (Select Case When DATEPART(HOUR, AppMstDOJ ) >=0 and DATEPART(HOUR, AppMstDOJ ) <=3 then CAST(CAST(CONVERT(DATE, DATEADD(DAY, -1, AppMstDOJ ),101) AS VARCHAR) + ' 23' + ':' +  '59' + ':' + '59' AS DATETIME) else AppMstDOJ  end from AppMast where AppMstID = 272)

Open in new window

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 11

Expert Comment

by:John_Vidmar
ID: 39815925
update Table1
set datetimefield = convert(varchar,dateadd(day,-1,datetimefield), 101) + ' 12:59:59 PM'
where convert(varchar,datetimefield,24) between '00:00:00' and '03:00:00'

Open in new window

0
 
LVL 18

Expert Comment

by:UnifiedIS
ID: 39815927
23:59.59 is easer than 12:59.59
Is it really 3 AM  or < 3 AM?

Just strip the time and subtract one second
UPDATE table
SEt yourdtm = DATEADD(second, -1, CONVERT(varchar(10), yourdtm, 101))
WHERE DATEPART(Hour, yourdtm) BETWEEN 0 AND 2
OR
DATEPART(Hour, yourdtm) = 3 AND DATEPART(minute, yourtdtm) = 0
0
 
LVL 8

Author Comment

by:mani_sai
ID: 39816143
Kyle Abrahams:

Further down my calculation...

I want to strip off the  time portion and update the same date column with just date...



Thanks to everyone for the suggestions.
0
 
LVL 8

Author Comment

by:mani_sai
ID: 39816170
I want to do update for the remaining records...

other than

where
datepart(hh,datetimer) between 0 and 2 or
(datepart(hh,datetimer) = 3 and datepart(mi, datetimer) = 0)
0
 
LVL 8

Author Closing Comment

by:mani_sai
ID: 39816203
Thanks
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now