SQL query for selecting specific date range (such as the current month only) - sql

I am working in SQL Server 2012.
I am trying to collect all data for the current month (2015-07) and group them. When I run this query it selects only the current day.
SELECT
YEAR, MONTH, IDWHSE, IDLOCATION, IDCUST,
SUM(PALLETDAYS) AS PALLETDAYS,
COUNT(*) AS LOCATIONDAYS
FROM
[METRICS].[dbo].[DailyData]
WHERE
DATE = CONVERT(date, DATEADD(MM, 0, GETDATE()))
GROUP BY
YEAR, MONTH, IDWHSE, IDLOCATION, IDCUST, PALLETDAYS
Thanks in advance
Gerry

Try this:
SELECT
YEAR, MONTH, IDWHSE, IDLOCATION, IDCUST,
SUM(PALLETDAYS) AS PALLETDAYS,
COUNT(*) AS LOCATIONDAYS
FROM
[METRICS].[dbo].[DailyData]
WHERE
MONTH(DATE) = MONTH(SYSDATETIME())
GROUP BY
YEAR, MONTH, IDWHSE, IDLOCATION, IDCUST, PALLETDAYS
By using the MONTH() function, you get the month number - both of your column Date (which is a really horribly bad name for a column, since DATE is also a reserved keyword for a datatype in SQL Server 2012 - try to use something more meaningful than just Date!) and the current date (I prefer the SYSDATETIME() function over GETDATE())

Related

Convert date into individual numerical columns for year month and day SQL

I have a date column in the format YY-MON-DD, e.g. 25-JUN-05. Is it possible to isolate this into 3 separate columns for year, month and day? Where month is converted from text to numerical, e.g. Year: 25, Month: 06, Day: 05?
MS SQL SERVER
As Nebi suggested, you can use DATEPART and extract each part and store it into different columns.
SELECT DATEPART(DAY,'2008-10-22'); -- Returns DAY part i.e 22
SELECT DATEPART(MONTH,'2008-10-22'); -- Returns MONTH part i.e 10
SELECT DATEPART(YEAR,'2008-10-22'); -- Returns YEAR part i.e 2008
Try with the below script,if you are using SQL Server.
SELECT 'Year: '+CAST(LEFT(YourdateColumn,2) as VARCHAR(2))+', Month: ' +CAST(MONTH('01-'+SUBSTRING(YourdateColumn,4,3)+'-15')as VARCHAR(2))+', Day:'+CAST(RIGHT(YourdateColumn,2)as VARCHAR(2))
FROM Yourtable
sample output :
You didn't specify your DBMS.
The following is standard SQL assuming that column really is a DATE column
select extract(year from the_column) as the_year,
extract(month from the_column) as the_month,
extract(day from the_column) as the_day
from the_table;

MSSQL Edit date (YEAR, MONTH) in one command

I need manipulate with time in one command. I have this date "15.02.2013" and now i need this time change on this "15.01.2012". Date is dynamic stats for me. i need change time with this functions (GATEDATE, YEAR, MONTH) or others.
INPUT:
"15.02.2013"
I need this output:
"15.01.2012"
Can you help me please?
You can do a double DATEADD on the same value.
I.e.
SELECT DATEADD(year, -1, DATEADD(month, -1, #date_from)) FROM Table1
The inner DATEADD is your original, which subtracts the month. This is then wrapped in a second DATEADD which subtracts the year. Alternatively, if it's always 1 year and one month, you could easily subtract 13 months:
SELECT DATEADD(month, -13, #date_from))
You could even choose to have a computed column in your table using the function to calculate the date.

splitting a datetime column into year, month and week

I want to split a datetime column so that the year and the month both have their own column in a select statement output. I also want to have a column by week of the year, as opposed to specific date.
Basically, I want separate year, month, and week columns to show up in my select statement output.
Try using the DatePart function as shown in the following:
select
datepart(year,Mydate),
datepart(month,Mydate),
datepart(week,Mydate)
From
MyTable
Note: If you need to calculate the week number by ISO 8601 standards then you'll need to use datepart(iso_week,Mydate)
You could also look at the DateName function
select
datename(month,Mydate)
From
MyTable
Here is another way. Use SQL Servers YEAR() and MONTH() functions. For week, I use datepart(week,Mydate) as noted by #DMK.
SELECT YEAR(MyDate), MONTH(MyDate), DATEPART(WEEK,Mydate) From YourTable
check this
select datepart(Month,DateColumn) Mnth,datename(Month,DateColumn) MnthName,datepart(Year,DateColumn) Year1,((day(DateColumn)-1) / 7) + 1 week from dbo.Table

SQL - get last month data

I have been using this query to extract information from last month
SELECT *
FROM Member
WHERE DATEPART(m, date_created) = DATEPART(m, DATEADD(m, -1, getdate()))
with the end of the year approaching, will this automatically pull Dec 2012 when i run it in Jan 2013 ?
Yes. your getdate() function will give the current date when the query is run. And you are adding -1 to the month and comparing month of date_created column and the last month. But I think you should also do comparison of year. You should add two conditions month and year both.
Yes, it will pull December data. But it will pull December data from any year, not just 2012
Yes, it will. DATEADD is a SQL internal function that adds to the full date, not just the selected part (day, month, year).

SQL Select data by this week

Hi how do I get the data by current week?
Select * from Transaction where transactionDate ....
In SQL Server based on week of year. Please see DATEPART for ##DATEFIRST etc. for example, this is all trades since Sunday in US/UK settigs:
WHERE DATEPART(week, transactionDate) = DATEPART(week, GETDATE())
Edit:
For Access, use this DatePart and use "ww" for the part of date you want.
In answer to the comment, "week" is not a variable; it's the bit of the date you want
So:
WHERE DatePart("ww", transactionDate) = DatePart("ww", GETDATE())
In Microsoft Access
Last n days:
SELECT *
FROM Transaction
WHERE transactionDate >=Date()-7
If you have indexes and this type of difference suits, it will be faster because it is sargable
This week by week difference:
SELECT *
FROM Transaction
WHERE DateDiff("w",[transactionDate],Date())=0
BTW It is considered bad practice to use *
DateDiff: http://office.microsoft.com/en-us/access/ha012288111033.aspx
Simple but portable:
SELECT *
FROM Transaction
WHERE transactionDate >= ?
AND transactionDate <= ?
Calculate the two parameters in your server-side code to whatever definition of 'week' you need.
In IBM DB2
SELECT *
FROM Transaction
WHERE transactionDate BETWEEN CURRENT TIMESTAMP - 7 days AND CURRENT TIMESTAMP;
In Access, if you want to run a query to find records that fall in the current week, use
SELECT *
FROM table
WHERE table.DateField Between (Date()-Weekday(Date())+1) And (Date()-Weekday(Date())+7);
That runs Sunday through Saturday. Use +2 and +6 instead if you want the workweek.
mySQL (standard date stamp)
SELECT *
FROM Transaction
WHERE WEEK(NOW())=WEEK(transactionDate);
mySQL (unix timestamp stamp)
SELECT *
FROM Transaction
WHERE WEEK(NOW())=WEEK(FROM_UNIXTIME(transactionDate));
Bit of a unoptimized query. Could be a more efficient way.
Note: This isn't a rolling 7 days. Just the current week of the year.
EDIT: Sorry I didn't see the ms-access tag. ignore all of this :|