Solved

SQL 2000 -  Query help

Posted on 2014-01-28
10
546 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

813 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

14 Experts available now in Live!

Get 1:1 Help Now