SQL Basic #1 : How to get date value in SQL query?

August 14, 2008

(2) Comments

Disclaimer : This tutorial is meant for beginners and for anyone who find it useful. The main purpose of this post to keep everything i find useful for future reference for myself and for others.

This small tutorial is to show how to get particular date part of from a date value field in SQL Server query.

select sales_date,
Day(sales_date) as Day,
Month(sales_date) as Month,
Year(sales_date) as Year,
Day(getDate()) as Current_Day,
Month(getDate()) as Current_Month,
Year(getDate()) as Current_Year
from sales_detail

Result
Query Result

In above query my date field is sales_date , using Day , Month and Year function i can get the particular date part by passing sales_date as parameter. This is to get date part value from a particular field value.

To get date part from current date is almost same only passing the current date as parameter. To get current date use getDate() function which will return the current date.

BALA SINGAM

, , , , , , ,


1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...

2 responses to "SQL Basic #1 : How to get date value in SQL query?"

Bassem said:
March 28, 2009

Thanks,
It helped me to solve my problem.

March 29, 2009

I’m glad i could help u :)

Leave a comment

Name : 
Mail : 
Website : 
Message :