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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:44:44+00:00 2026-05-13T13:44:44+00:00

I’ve decided that I’ll use 8601 datetimes for all the datetimes that I return

  • 0

I’ve decided that I’ll use 8601 datetimes for all the datetimes that I return from my app. Suddenly, in one particular proc, getdate() isn’t returning a datetime with a T in the middle. I should also mention that I’m converting the set containing a datetime to XML using FOR XML PATH. Typically when I convert a table containing datetime to xml I get 8601 formatted dates. But in one case, I’m not.

select (cast(getdate() as datetime)) -- returns 2010-01-25 10:13:46.033

So I directly converted it like so:

select convert(datetime, getdate(), 126) -- returns 2010-01-25 10:14:35.923

But if I cast it to an nvarchar I get the T!!

SELECT CONVERT(NVARCHAR(30), GETDATE(), 126) -- returns 2010-01-25T10:15:29.633

What’s even stranger to me is that if I select several versions of this with a union, the T version disappears. But selecting without the union, the T version (last one) remains.

-- returns 4 rows of 2010-01-25 10:15:57.333
select getdate() union all 
select (cast(getdate() as datetime))  union all
select convert(datetime, getdate(), 126)    union   all
SELECT CONVERT(NVARCHAR(30), GETDATE(), 126) 

I really have no idea what could cause this to happen. I thought 8601 dates were locale-independent, so I don’t think it’s anything like that.

Reference (“yyyy-mm-ddThh:mi:ss.mmm” for a 126): http://msdn.microsoft.com/en-us/library/ms187928.aspx

  • 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-13T13:44:44+00:00Added an answer on May 13, 2026 at 1:44 pm

    You are mixing up datetime values with formatted strings.

    The first two examples doesn’t return the date formatted in any way at all, it’s just a datetime value. How the value is formatted into text is decided by how you display the value after getting it from the database. If you lived in a different country so that your default culture settings were different, the date could for example be displayed as 1/25/2010 10:14 AM instead.

    In the second example the format parameter (126) is ignored, as there is no formatting or parsing involved when converting from a datetime value to a datetime value.

    The third example formats the datetime value into a string before it’s returned from the database, that’s why you get it in the format that the database uses.

    If you use a union with difference data types, it’s the type precedence that decides the type of the result. The datetime type has higher precedence than nvarchar, so it attempts to convert the nvarchar values to datetime.

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

Sidebar

Related Questions

No related questions found

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.