SQL Training Online

  • Training Courses
  • Beginner Book
  • Blog

October 19, 2012 by Joey Blue Leave a Comment

How to use Datetime in SQL Server – SQL Training Online

This video is a quick overview of using the SQL Server Datetime function.

[sharebox5_no_text] [/sharebox5_no_text]

The datetime is actually made up of two different pieces:

1) Date

2) Time

This can be demonstrated using the SQL Cast function on a string to convert it to a datetime.

Here is some SQL for the Date portion:

select cast('20121019' as datetime)

Here is some SQL for the Time portion:

select cast('10:05:00' as datetime)

Now, you can also simply use the built-in getdate() function to bring back the current system time.

select getdate()

And finally, I want to bring back a datetime from an actual table. time

In this case, we will use the employee table from the SQL Training Online Simple DB:

select hire_date from employee

If we want to convert the result to a more formatted string, we will use the TSQL CONVERT function:

select convert(varchar,hire_date,104)
from employee

In this example, we are converting from a datetime to a varchar (string). The result will be in the format of 104.

To translate 104 to a datetime format, you will want to look at the MSDN Documentation.

And, that is how you use the Datetime SQL.

Let me know what you think by commenting or sharing on twitter, facebook, google+, etc.

Filed Under: SQL Training Tagged With: datetime, SQL

  • « Previous Page
  • 1
  • 2

Recent Posts

  • SQL Database Normalization – 1NF, 2NF, 3NF, and 4NF
  • SQL Joins Tutorial for Beginners – Inner Join, Left Join, Right Join, Full Outer Join – SQL Training Online
  • Zillow House Price Analysis from CSV Archive – #PowerBI 002
  • Learn Basic SQL – 1 Hour Training Course – SQL Training Online
  • Create Table Statement in SQL Server and Inserting Baseball Homerun Leader Dataset – SQL Training Online

Popular Posts

  • SQL Functions
  • SQL Jobs in Oracle and Microsoft SQL Server
  • Troubleshooting a SQL Join
Copyright © 2023 · SQLTrainingOnline.com · Consulting at EmbarkBlue.com