Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7600349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:53:21+00:00 2026-05-30T22:53:21+00:00

i have about 12 tables i need to iterate thru and update all datetime

  • 0

i have about 12 tables i need to iterate thru and update all datetime fields (which is a varchar type) check if it’s a date and if so, add 5hrs to account for utc adjustment or if it’s an invalid date just set it to null. Below is a template I came up with. Just wondering if there are some other ways to do this?

update Tables_1
set releasedate = CASE ISDATE(releasedate) 
                     WHEN 1 THEN DATEADD(HH, 5, releasedate) 
                     ELSE NULL 
                  END,
    returndate = CASE ISDATE(returndate)
                     WHEN 1 THEN DATEADD(HH, 5, returndate) 
                     ELSE NULL 
                 END

given: sql server 2008, i know the columns and tables already that have the datetime type stored as varchar.

bonus request to add another check. If it is a date and the time is not specified, set the time to 5:00 AM

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:53:22+00:00Added an answer on May 30, 2026 at 10:53 pm

    If they all end with date you could build it dynamically:

    DECLARE @sql NVARCHAR(MAX) = N'';
    
    SELECT @sql += ', ' + name = CASE ISDATE(' + name + ') WHEN 1 THEN 
          DATEADD(HOUR, 5, ' + name + ' ELSE NULL END'
    FROM sys.columns 
    WHERE name LIKE '%date' AND [object_id] = OBJECT_ID('dbo.Tables_1');
    
    SELECT @sql = 'UPDATE dbo.Tables_1 SET ' + STUFF(@sql, 1, 1, N'');
    
    PRINT @sql;
    -- EXEC sp_executesql @sql;
    

    Now if you want to do that for 12 tables, you could just do this in a loop, e.g.

    DECLARE @t SYSNAME, @sql NVARCHAR(MAX);
    
    DECLARE c CURSOR LOCAL STATIC FORWARD_ONLY READ_ONLY
    FOR 
      SELECT name FROM sys.tables 
      WHERE name IN ('Tables_1' --, other tables
    );
    
    OPEN c;
    
    FETCH NEXT FROM c INTO @t;
    
    WHILE @@FETCH_STATUS = 0
    BEGIN
        SELECT @sql = ', ' + name = CASE ISDATE(' + name + ') WHEN 1 THEN 
              DATEADD(HOUR, 5, ' + name + ' ELSE NULL END'
        FROM sys.columns 
        WHERE name LIKE '%date' AND [object_id] = OBJECT_ID(@t);
    
        SELECT @sql = 'UPDATE ' + @t + ' SET ' + STUFF(@sql, 1, 1, N'');
    
        PRINT @sql;
        -- EXEC sp_executesql @sql;
    
        FETCH NEXT FROM c INTO @t;
    END
    
    CLOSE c;
    DEALLOCATE c;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have about 10 tables with ~10,000 rows each which need to be pulled
I have a table of about a million rows and I need to update
I have about 100 tables where all of them have duplicate foreign key constraints
I have been wondering about temp tables in sp's and how all that can
I have a database Student which contains about 20 tables. one of the tables
I have realised after making about 20 tables that I need to user utf8_unicode
I have a somewhat complicated assortment of tables for which I need to do
I have about 10 different sql statements that update different tables. They look similar
I have about 4 tables from which I like to gather information and then
I have database of about 5 tables. I am going to need to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.