I have a database with date format “yyyyymmdd’ and data-type varchar(8). In this case when I need to find a data for whole month I used to write code like below :
select * from table1
where Date Between (convert(varchar(6),getdate(),112)+'01')
AND (convert(varchar(6),getdate(),112)+'31'))
Now I just have a new table with date format “dd/mm/yyyy” and date type varchar(10) and I would like to extract data from the table for whole month without specifying exact date. I want start date as first day of current and month and end date as last day of the current month.
But I don’t now how to do the same as the date format is now ‘dd/mm/yyyy’.
First, thing you should store you date as the datatype of
dateand notvarcharYou can use
MONTH()or
DATEPART()See SQL Fiddle with Demo
Both of these, get the value of the
MONTH()— 8, 9, 10 — and then compares it to the month that you want.