3

Current Date Time in Python and SQL

 2 years ago
source link: https://blog.sqlauthority.com/2021/09/23/current-date-time-in-python-and-sql/?utm_campaign=current-date-time-in-python-and-sql
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Yesterday I wrote a blog post Getting Started with Python and after that, I got lots of good feedback. Lots of people encouraged me to learn more. Well, the goal of this blog is to focus on SQL but once in a while, it does not hurt to learn something different. After installing and getting started with Python, the very first exercise I did was to print the current date time in Python. Let us learn about it today.

Current Date Time in Python and SQL currentdatetime-800x354

In SQL Server Current Date-Time

The script is pretty straightforward and simple:

SELECT FORMAT(GETDATE(), 'yyyy/MM/dd HH:mm:ss') as DT

It will display as follows:

Current Date Time in Python and SQL currentdt1

In Python Current Date Time

Now let us try to do the same thing in Python.

import datetime
now = datetime.datetime.now()
print (now.strftime("%Y/%m/%d %H:%M:%S"))

It will display as follows:

Current Date Time in Python and SQL currentdt2

I learn quite a lots of things here. First of all, in python first, we have to import a module named datetime. Afterwards, we need to use the method now to get the current datetime. Afterwords, use the strftime() method to create a string representing the date in the desired format.

Well, I consider this is good progress for just a few minutes with a very brand new language. My goal is to build a Fibonacci series in python but I personally think it is a long way for me so far. Feel free to reach out to me on Twitter for further conversation.

Reference: Pinal Dave (http://blog.SQLAuthority.com)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK