How to use DATENAME in WHERE clause - sql-server-2008

Am using Sql Server 2008, I have a column named Date in my table, and I want to get the datas for the particular date.... I need to give this Date in my WHERE condition.
for example, if I want to get the records for the particular month in the given date, how can I use this Date in WHERE condition.
DATANAME(MONTH,'#Date')
if I give like this in my query I can get the month from the given DATE, the same way I tried by putting in WHERE condition like,
WHERE DATE= DATANAME(MONTH,'#Date')
here it reports conversion error...how can I display the datas for a particular month, can anyone help me

If you want a month of data for a table you should check against an interval. The query is not able to use indexes on the date column if you are applying functions on the column.
Use something like this to get data for April 2012.
-- The date parameter
declare #Date datetime
set #Date = '2012-04-11'
declare #FromDate datetime
declare #ToDate datetime
-- set FromFate to first of april
set #FromDate = dateadd(month, datediff(month, 0, #Date), 0)
-- set ToDate to first of may
set #ToDate = dateadd(month, 1+datediff(month, 0, #Date), 0)
select *
from YourTable
where [Date] >= #FromDate and [Date] < #ToDate

If you want to show data for a particular year and month you can use the YEAR and MONTH functions:
SELECT ...
FROM ...
WHERE YEAR(mydate) = 2012 AND MONTH(mydate) = 3 -- March, 2012

To me it seems that your field Date is not of type varchar or nvarchar, so using a condition where a Datetime = string is obviously wrong.
Have you tried
WHERE DATE= #Date

Shouldn't it be:
DATENAME(MONTH, #Date)
Instead of:
DATANAME(MONTH,'#Date')
(Notice "DATA" vs "DATE" and #Date isn't in quotations)
Then to use this against a date/datetime column you would have to cast both sides like below:
WHERE datename(Month, [Date]) = datename(Month, [Date])
Warning: The above does not use any indexes so isn't as efficient as "WHERE Date = Date"

First: Remove '' from variable. #Date, not '#Date'
If you want to find dates from specific month. (You have to remember about year condition also)
WHERE DATANAME(MONTH, #Date) = 'April'
if you want to find exact date:
WHERE DATE = #date

Related

SQL Query Between Dates in Visual Studio Table

I have a table within Visual Studio and I am wanting to create an input field in for a StartDate and another input field for an EndDate where a user selects a date for both and it will return all records that have a date that falls between the Start and End dates that the user chooses. I would also like the user to have the option to only search for the StartDate and leave the EndDate null so essentially only having 2 input fields for the table (or 1 if End Date is null).
I have this right now which returns the date the user selects but only on that date:
(Date = #Date) AND (#Date BETWEEN #StartDate AND #EndDate)
#StartDate is a date that comes before any date in the DB. #EndDate is the current day. This is just in case a user types in a date that is not within the date range of the database and it will return an error.
I have tried making the parameter #StartDate equal to #Date so when the user selects a date, it will make that the starting date. I also have the input field for the user to select an EndDate which seems to work. I just can't get the start date parameter to work like I want.
These are the queries I have tried:
1.
(Date = #Date) AND (#StartDate = #Date) AND (#Date BETWEEN #StartDate AND #EndDate)
2.
(Date = #Date) AND (#Date BETWEEN #Date AND #EndDate)
How do I modify this query/parameters to get the user to select a start date and an end date in input fields and for the table to return all the records that are between those dates?
(Date = #Date) AND ...
This will always require Date to equal #Date to be true, regardless of the rest unless some OR ... comes along.
Assuming the two fields fill #StartDate and #EndDate (to me it isn't clear, why there is a third variable, #Date) you're probably searching for something like:
#EndDate IS NULL
AND Date = #StartDate
OR Date BETWEEN #StartDate
AND #EndDate
If #EndDate IS NULL (the user hasn't put anything in the respective field) Date = #StartDate has to be true. Otherwise Date BETWEEN #StartDate AND #EndDate (which won't ever be true if #EndDate IS NULL just in case you wonder).

Given datetime, filter results by month of that datetime

For sql server 2008: what is the best way to filter results, to return only results that are in the same month as a certain date?
The best I could come up with is the following:
-- set up test data
DECLARE #TABLE1 AS TABLE (
ID INT,
STRING VARCHAR(MAX),
DATECOLUMN DATETIME
)
INSERT INTO #TABLE1
SELECT
0 ID,
'TABLE1 0' STRING,
CONVERT(DATETIME, '2016-10-13 12:45:00', 102) DATECOLUMN
UNION ALL SELECT 1, 'TABLE1 1', CONVERT(DATETIME, '2016-9-13 12:45:00', 102)
UNION ALL SELECT 2, 'TABLE1 2', CONVERT(DATETIME, '2016-10-1 00:00:00', 102)
UNION ALL SELECT 3, 'TABLE1 3', CONVERT(DATETIME, '2016-10-31 23:59:59', 102)
-- set up constraint
DECLARE #SOMEDATE DATETIME = CONVERT(DATETIME, '2016-10-13 12:45:00', 102)
-- filter
SELECT * FROM #TABLE1
WHERE MONTH(DATECOLUMN) = MONTH(#SOMEDATE)
Is this the best way?
Your query will return records where the month of DateColumn matches the month of #SomeDate, although (as #Jayvee points out) this would be true regardless of the year. So if that's what you want, your solution is fine.
But you asked whether that was the best solution. I would say no, because this won't take advantage of any index you may have that includes DateColumn.
So this is what I would do, if I were on SQL Server 2008:
declare #someDate datetime = GetDate()
declare #startDate date
declare #endDate date
-- I want the first day of the month, at 12:00:00AM (no time component).
-- The DateAdd expression subtracts days, and converting to the Date data type
-- ensures that I don't have a time component hanging around.
set #startDate = convert(date, DateAdd(day,- DatePart(day, #someDate) + 1, #someDate))
-- I want the first day of the next month. This is easy:
set #endDate = DateAdd(month, 1, #startDate)
-- Here's my actual query, that can take advantage of indexes that include DateColumn:
select *
from Table1
where DateColumn >= #startDate and DateColumn < #endDate
Note that I said >= for start date but < for the end date. That ensures that I'll pick up any date entries for the last day of the month that have a non-zero time component, but won't go into the next month.
The best way for a long-term solution is to create a date dimension table (you can find scripts out there to auto-generate one). The date dim table will just contain a list of dates as far back and forward as you care to go, and it includes columns like DayOfWeek, QuarterOfYear, YYYYMM, etc., so you can join CAST(YOURDATE AS DATE) to the date dim's date and pull nifty date info. Here you can join both of your tables to the date dim and use WHERE t1.YYYYMM = t2.YYYYMM.

Count first day of month between two dates

Could someone advise how I can go about counting the number of first day of month between two dates?
For example: 02/01/2015 to 05/05/2015 will count as 4.
It can be accomplished easily like this:
DECLARE #sd DATE = '02/02/2015', #ed DATE = '05/01/2015'
SELECT DATEDIFF(mm, #sd, #ed) + CASE WHEN DAY(#sd) = 1 THEN 1 ELSE 0 END
If you have a sequence of dates stored somewhere (like in a calendar table) you can easily count the dates matching the day(date) predicate. If you don't have any suitable table with a date sequence you can use a recursive common table expression to generate one on the fly like this:
declare #start date
set #start = '02/01/2015'
declare #end date
set #end = '05/05/2015'
;with dates (date) as (
select #start date
union all
select dateadd(day, 1, date) as date from dates where date < #end
)
select count(*) no_of_firsts from dates where day(date) = 1
Sample SQL Fiddle

Compare datetime in stored procedure

I have a sql 2008 database and I am creating a stored procedure that shall check if a datetime is more than 3 hours old but I don't know how to do it.
Do you have some way to do it?
the datetime is a field in the table.
BR
Rather than applying DATEDIFF to the column value, which will negate an index, I suggest using a comparison of the column to an expression (which can use an index).
If you want this as a filter:
SELECT columns
FROM dbo.table
WHERE DateTimeColumn < DATEADD(HOUR, -3, CURRENT_TIMESTAMP);
(If you want only the rows that are newer than 3 hours old, change < to > or >=.)
If you want to return all rows with a column showing whether it is more than 3 hours old:
SELECT columns, [3HoursOld] = CASE
WHEN DateTimeColumn < DATEADD(HOUR, -3, CURRENT_TIMESTAMP)
THEN 'Yes, older than 3 hours.'
ELSE 'No, not older than 3 hours.'
END
FROM dbo.table;
Take at look at the DATEDIFF function.
DATEDIFF ( datepart , startdate , enddate )
You would then use with datepart set to hh and the enddate set to the current time. To get the current database time you could use GETDATE(). Compare the result with 3 since it will return the number of hours passed.
#date is the date you want to compare
declare #date datetime
set #date= '2012-02-15 14:20:42.797'
SELECT DateDiff(hh, DATEADD(hh,-3,#date), GETDATE()) --if it's > 3
you better create a Boolean function that does the trick that you can use where ever you like

Need to find date's between 2 date's in SQL Server 2008

I need help with query to find all the dates that between 31/12/2009 and 31/02/2010
In SQL Server 2008
i try this:
SELECT convert(varchar(50), MyDate, 103)
where convert(varchar(50), MyDate, 103) >= '31/12/2009' and convert(varchar(50), MyDate, 103) <='31/02/2010'
but it give me wrong result
why ?
I had a different interpretation of the question: "how to generate all the dates between a certain range?"
Here's a solution to that:
--define start and end limits
Declare #todate datetime, #fromdate datetime
Select #fromdate='2009-03-01', #todate='2009-04-10'
;With DateSequence( Date ) as
(
Select #fromdate as Date
union all
Select dateadd(day, 1, Date)
from DateSequence
where Date < #todate
)
--select result
Select * from DateSequence option (MaxRecursion 1000)
There is a nice article that shows how to generate sequences (numbers, dates, times) using CTEs.
Edit:
After clarification, the issue seems to be the date format being input: dd/mm/yyyy.
SQL Server expects the format mm/dd/yyyy.
I would simply transform it before running the select statement:
-- Assuming two variables, #inputFromDate and #inputToDate, in the format of dd/mm/yyyy...
declare #fromDate varchar(10), #toDate varchar(10)
set #fromDate =
substring(#inputFromDate, 3, 2) + '/' +
substring(#inputFromDate, 1, 2) + '/' +
substring(#inputFromDate, 7, 4)
set #toDate =
substring(#inputToDate, 3, 2) + '/' +
substring(#inputToDate, 1, 2) + '/' +
substring(#inputToDate, 7, 4)
select * from SomeTable where dateCol >= #fromDate and dateCol < #toDate
-- you can change the < or >= comparisons according to your needs
If the MyDate column is a datetime, as it appears to be, then it's already in the right "format". Don't convert it to a varchar(50) in the predicate condition - this makes your query non-sargable and will kill performance on any indexes you might have.
Instead, take your parameters as date or datetime instances:
SELECT ...
FROM MyTable
WHERE MyDate >= #BeginDate
AND MyDate <= #EndDate
Your query should not depend on a specific date format in the input parameters - those parameters are not varchar types, they are datetime (or date). When you run this query or stored procedure from whatever environment the application is in and supply binding parameters (you are using bind parameters, right?), said library will automatically handle any formatting issues.
If you try to use the >= and <= operators on character representations of dates, with any format other than the ISO standard yyyymmdd, you will get the wrong results, because the alphabetical order is different from the temporal order. Don't do this.
If you simply need to write an ad-hoc query, i.e. this isn't being run from any programming environment, then simply do not use the dd/mm/yyyy format. Use the ISO date format instead; it is unambiguous and implicitly convertible to datetime values:
SELECT ...
FROM MyTable
WHERE MyDate >= '20091231'
AND MyDate <= '20100231'
Honestly, no other solution is acceptable in my mind. For ad-hoc queries, always use the unambiguous ISO standard for dates. For applications connecting to the database, always use bind parameters. If you're doing anything else, you're writing code that's either unreliable, insecure, or both.
BOL is always a good reference...
Start with BETWEEN
Did you try this:
DECLARE #FROMDATE DATETIME
DECLARE #TODATE DATETIME
SET #FROMDATE = GETDATE()
SET #TODATE = GETDATE()+7
;WITH DATEINFO(DATES) AS (SELECT #FROMDATE UNION ALL SELECT DATES + 1 FROM DATEINFO WHERE DATES < #TODATE)
SELECT * FROM DATEINFO OPTION (MAXRECURSION 0)