Avatar of finance_teacher
finance_teacher
 asked on

MS SQL Server -- substring DateTime ?

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%'
Microsoft SQL ServerMicrosoft SQL Server 2008Microsoft SQL Server 2005

Avatar of undefined
Last Comment
Scott Pletcher

8/22/2022 - Mon
SOLUTION
Chris Ashcraft

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Scott Pletcher

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%'
finance_teacher

ASKER
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%'
Scott Pletcher

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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
Scott Pletcher

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.