?
Solved

MS SQL Server -- substring DateTime ?

Posted on 2012-03-15
5
Medium Priority
?
870 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 7

Assisted Solution

by:micropc1
micropc1 earned 400 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:Scott Pletcher
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:Scott Pletcher
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:
Scott Pletcher earned 1600 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

765 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