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

  • Home
  • SEARCH
  • 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 273715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:29:58+00:00 2026-05-12T00:29:58+00:00

I have a single char(8) variable formatted as ddmmyyyy in a stored procedure (quality

  • 0

I have a single char(8) variable formatted as ddmmyyyy in a stored procedure (quality and validity of this value is unknown and beyond my control). What is the best most efficient way to move the value into a datetime variable, and throw an error if it is not valid datetime.

DECLARE @Source       char(8)
DECLARE @Destination  datetime

SET @Source='07152009'

--your solution here


SELECT @Destination

here is the best way I could think of:

DECLARE @Source             char(8)
DECLARE @Temp               varchar(10)
DECLARE @Destination        datetime

set @Source='07152009'
SET @Temp=LEFT(@Source,2)+'/'+SUBSTRING(@Source,3,2)+'/'+RIGHT(@Source,4)

IF ISDATE(@Temp)!=1
BEGIN
    RAISERROR('ERROR, invalid date',16,1)
END
SET @Destination=@Temp

SELECT @Source AS Source, @Temp AS  Temp, @Destination AS Destination

EDIT here’s what I’m going to go with…

DECLARE @Source             char(8)
DECLARE @Destination        datetime

set @Source='07152009'
BEGIN TRY
    SET @Destination=CONVERT(datetime,RIGHT(@Source,4)        -- YYYY
                                      +LEFT(@Source,2)        -- MM
                                      +SUBSTRING(@Source,3,2) -- DD
                             )
END TRY
BEGIN CATCH
    PRINT 'ERROR!!!' --I'll add a little more logic here and abort processing
END CATCH

SELECT @Source AS Source, @Destination AS Destination
  • 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-12T00:29:59+00:00Added an answer on May 12, 2026 at 12:29 am

    First of all, since you’re using SQL Server 2005, you should put your code that might fail into BEGIN TRY.....END TRY BEGIN CATCH....END CATCH blocks – try/catch blocks for T-SQL!

    Second, for all date manipulation, I would always use ISO-8601 format which will work regardless of what current date format is set in SQL Server.

    ISO-8601 format is YYYYMMDD for just dates, or YYYY-MM-DDTHH:MM:SS for date with time – so I’d write your code as:

    BEGIN TRY
      SET @Source='07152009'
      SET @Temp = RIGHT(@Source, 4) +             -- YYYY
                  LEFT(@Source, 2) +              -- MM
                  SUBSTRING(@Source, 3, 2)        -- DD
    
      IF ISDATE(@Temp)!=1
      BEGIN
          RAISERROR('ERROR, invalid date',16,1)
      END
    
      SET @Destination = CAST(@Temp AS DATETIME)
    END TRY
    BEGIN CATCH
          -- handle error if something bombs out
    END CATCH
    

    Do not rely on any particular date format being set!! Send me your code and I’ll try it on a Swiss-German system – I almost guarantee it’ll break if you blindly assume “en-US” and thus “mm/dd/yyyy” – it’s not the same setting everywhere on this planet.

    Unfortunately SQL Server is rather weak handling dates – maybe that might be an extension point where using a CLR assembly inside SQL Server would make sense, to tap into the much richer date handling functions in .NET ??

    Marc

    PS: seems the ISO-8601 format I knew YYYY-MM-DD doesn’t always work in SQL Server – contrary to what Books Online seem to preach. Use YYYYMMDD or YYYY-MM-DDTHH:MM:SS instead.
    Thanks, gbn!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 246k
  • Answers 247k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer CodingHorror is the obligatory first, with JoelOnSoftware in close second… May 13, 2026 at 8:34 am
  • Editorial Team
    Editorial Team added an answer I hate it when I answer my own question but… May 13, 2026 at 8:34 am
  • Editorial Team
    Editorial Team added an answer You can use the vertical-align property to specify centered alignment.… May 13, 2026 at 8:34 am

Related Questions

I am using Named Pipes to transfer data from a client (C++) to a
I have a UTF-8 encoding string I am getting from reading a PDF, and
in a C program I have an long* that I want to serialize (thus
I am working on a project where I can to convert a api digit

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.