Solved

SQL 2000 -  Query help

Posted on 2014-01-28
10
550 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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
 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

749 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