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 7891361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:35:59+00:00 2026-06-03T06:35:59+00:00

declare @str_datetime varchar(50) set @str_datetime=’30-04-2012 19:01:45′ — 30th April 2012 declare @dt_datetime datetime select

  • 0
declare @str_datetime varchar(50)
set @str_datetime='30-04-2012 19:01:45' -- 30th April 2012
declare @dt_datetime datetime
select @dt_datetime=@str_datetime

This is giving following error:

Msg 242, Level 16, State 3, Line 4
The conversion of a varchar
data type to a datetime data type resulted in an out-of-range value.

My question is how SQL Server decides which format to use for implicit datetime conversion?

  • 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-06-03T06:36:00+00:00Added an answer on June 3, 2026 at 6:36 am

    This can depend on a variety of factors – the operating system’s regional settings, the current user’s language and dateformat settings. By default, Windows uses US English, and the user’s settings are US English and MDY.

    But here are some examples to show how this can change.

    User is using BRITISH language settings:

    -- works:
    SET LANGUAGE BRITISH;
    SELECT CONVERT(DATETIME, '30-04-2012 19:01:45');
    
    -- fails:
    SELECT CONVERT(DATETIME, '04/13/2012');
    GO
    

    (Error)

    Msg 242, Level 16, State 3, Line 5
    The conversion of a varchar data
    type to a datetime data type resulted in an out-of-range value.

    User is using Français:

    -- works:
    SET LANGUAGE FRENCH;
    SELECT CONVERT(DATETIME, '30-04-2012 19:01:45');
    
    -- fails:
    SELECT CONVERT(DATETIME, '04/13/2012');
    GO
    

    (Error)

    Msg 242, Level 16, State 3, Line 1
    La conversion d’un type de
    données varchar en type de données datetime a créé une valeur hors
    limites.

    User is again using Français:

    SET LANGUAGE FRENCH;
    
    -- fails (proving that, contrary to popular belief, YYYY-MM-DD is not always safe):
    SELECT CONVERT(DATETIME, '2012-04-30');
    GO
    

    (Error)

    Msg 242, Level 16, State 3, Line 1
    La conversion d’un type de données
    varchar en type de données datetime a créé une valeur hors limites.

    User is using DMY instead of MDY:

    SET LANGUAGE ENGLISH;
    SET DATEFORMAT DMY;
    
    -- works:
    SELECT CONVERT(DATETIME, '30-04-2012 19:01:45');
    
    -- fails:
    SELECT CONVERT(DATETIME, '04-30-2012');
    GO
    

    (Error)

    Msg 242, Level 16, State 3, Line 2
    The conversion of a varchar
    data type to a datetime data type resulted in an out-of-range value.

    Your best bet, always, is to use ISO standard, non-regional, safe, unambiguous date formats. The two I typically recommend are:

    YYYYMMDD                  - for date only.
    YYYY-MM-DDTHH:MM:SS[.mmm] - for date + time, and yes that T is important.
    

    None of these fail:

    SET DATEFORMAT MDY;
    SET LANGUAGE ENGLISH;
    SELECT CONVERT(DATETIME, '20120430');
    SELECT CONVERT(DATETIME, '2012-04-30T19:01:45');
    SET LANGUAGE FRENCH;
    SELECT CONVERT(DATETIME, '20120430');
    SELECT CONVERT(DATETIME, '2012-04-30T19:01:45');
    SET LANGUAGE BRITISH;
    SELECT CONVERT(DATETIME, '20120430');
    SELECT CONVERT(DATETIME, '2012-04-30T19:01:45');
    SET DATEFORMAT DMY;
    SELECT CONVERT(DATETIME, '20120430');
    SELECT CONVERT(DATETIME, '2012-04-30T19:01:45');
    

    Therefore, I strongly recommend that instead of letting users type in free text date formats (or that you use unreliable formats yourself), control your input strings and make sure they adhere to one of these safe formats. Then it won’t matter what settings the user has or what the underlying regional settings are, your dates will always be interpreted as the dates they were intended to be. If you are currently letting users enter dates into a text field on a form, stop doing that and implement a calendar control or at least a pick list so you can ultimately control the string format that is passed back to SQL Server.

    For some background, please read:

    • Tibor Karaszi’s The ultimate guide to the datetime datatypes
    • All of the links in Dating Responsibly
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

declare @servername varchar(2000) set @EmriServerit=(select @@servername) declare @dbname varchar(2000) set @dbname ='Test1' declare @Dir
DECLARE @Symb varchar SET @Symb = 'Lara' SELECT Names FROM Cricket where Names like
DECLARE @table table(XYZ VARCHAR(8) , id int) INSERT INTO @table SELECT '4000', 1 UNION
declare @d varchar set @d = 'No filter' if (@d like 'No filter') BEGIN
DECLARE @lastname VARCHAR(25) DECLARE @birthdate VARCHAR(25) SELECT @lastname = 'Smith' SELECT @birthdate = '19-Apr-36'
DECLARE @STR_IDS VARCHAR(15) SET @STR_IDS='7,15,18' UPDATE TBL_USERS WHERE ID IN @STR_IDS I know the
I have the following query: DECLARE @str VARCHAR(500) SET @str = 'Barcode: 22037 CaseSize:
Declare: LPWSTR** lines= new LPWSTR*[totalLines]; then i set using: lines[totalLines]=&totalText; SetWindowText(totalChat,(LPWSTR)lines[totalLines]); totalLines++; Now I
declare c int set c = 1 while c<700 do update users set profile_display_name
I just want to know why this piece of code giving me segfault. if(argc

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.