How to change datetime values in SQL Server?

350805 pts.
Tags:
DateTime
SQL Server 2008
I want to present a datetime value without hours, minutes or seconds in SQL Server 2008? Would this code work?

Statement @SomeDate datetime = '2013-09-18 16:30:22'
select trunc_date(@SomeDate)
-----------------------
2013-09-18 00:00:00.000

Answer Wiki

Thanks. We'll let you know when a new response is added.

You can cast the datetime value into a date datatype.

 
declare @somedate datetime
 set @SomeDate = ’2013-09-18 16:30:22′
 print cast(@somedate as date)
That should get you what you need.

Discuss This Question: 3  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • carlosdl
    In oracle, there is a TRUNC function that can be used to remove the time portion of a date/time value, but I don't think the TRUNC_DATE is valid T-SQL function.

    Starting from SQL Server 2008, you can just cast the value to a DATE type:

    CAST(@SomeDate AS DATE);


    On earlier versions, you will probably need to use a combination of the DATEDIFF and DATEADD functions.

    69,920 pointsBadges:
    report
  • carlosdl
    The editor modified the code part:

    It should be:

    CAST(@SomeDate AS DATE);
    69,920 pointsBadges:
    report
  • msi77
    If you need time components to be zero, add to carlosdl's suggestion the reverse conversion:
    select cast(CAST(@SomeDate as date) as datetime)
    1,670 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following