Solved

MS SQL Server -- substring DateTime ?

Posted on 2012-03-15
5
840 Views
Last Modified: 2012-03-16
Below works, updating the value
to 2012-03-15 09:17:24.910

How can I change so it always
keeps the end as "02:00:00.000" ?

update [ReportServer].[dbo].[Schedule]
--set [StartDate] = '2012-03-30' + substring([StartDate],11,13)
set [StartDate] = CURRENT_TIMESTAMP,
    [NextRunTime] = CURRENT_TIMESTAMP
where [Name] like 'EOM - 3:30a%'
0
Comment
Question by:finance_teacher
  • 3
5 Comments
 
LVL 7

Assisted Solution

by:micropc1
micropc1 earned 100 total points
ID: 37725398
Try...

UPDATE [ReportServer].[dbo].[Schedule]
SET StartDate = CAST(CAST(YEAR(GETDATE()) AS varchar) + '-' + CAST(MONTH(GETDATE()) AS varchar) + '-' + CAST(DAY(GETDATE()) AS varchar) + ' 02:00' AS DATETIME)
WHERE [Name] LIKE 'EOM - 3:30a%'

Open in new window

0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 37725432
UPDATE [ReportServer].[dbo].[Schedule]
SET
    [StartDate] = CONVERT(char(8), CURRENT_TIMESTAMP, 112) + ' 02:00:00.000',
    [NextRunTime] = CONVERT(char(8), CURRENT_TIMESTAMP, 112) + ' 02:00:00.000'
WHERE
    [Name] LIKE 'EOM - 3:30a%'
0
 

Author Comment

by:finance_teacher
ID: 37726131
Below works, but how can I get the "substring"
to work since I have 50+ updates and want to
use a substring of the original StartDate value without
getting an Argument data type datetime is invalid for
argument 1 of substring function ERROR ?
------------------------------------------------------------------------------
UPDATE [ReportServer].[dbo].[Schedule]
SET StartDate =
         CAST(
                 CAST('2012-03-19' AS varchar) +
                 ' 03:30' AS DATETIME
                          --substring([StartDate],11,13) AS DATETIME
             )
WHERE [Name] LIKE 'EOM - 3:30a%'
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 37726569
Oh, OK, you don't always want 02:00:00.000, you want to retain the original time --
right?.

Btw, the format YYYYMMDD is *universal* in SQL Server and *always* works, but YYYY-MM-DD is not can fail under certain settings.


UPDATE [ReportServer].[dbo].[Schedule]
SET StartDate =
         CAST(
                 '20120319 ' + CONVERT(varchar(20), startDate, 114)
             )
WHERE [Name] LIKE 'EOM - 3:30a%'


SQL will automatically convert the varchar value to datetime if/when needed.
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 400 total points
ID: 37726576
If you just need to adjust the number of days, and keep exactly the same time, this would be faster:

SET StartDate = DATEADD(DAY, DATEDIFF(DAY, StartDate, '20120319'), StartDate)
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.

Join & Write a Comment

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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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 setup several different housekeeping processes for a SQL Server.

707 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