So I have a table that has the month and year broken down, for example field called Month has the number 7 in it (this month) and the Year field has 2011. Theres also additional months years, etc. How can I query this to show just the current year, month?
In SQL Server you can use YEAR, MONTH and DAY instead of DATEPART.
(at least in SQL Server 2005/2008, I'm not sure about SQL Server 2000 and older)
I prefer using these "short forms" because to me, YEAR(getdate()) is shorter to type and better to read than DATEPART(yyyy, getdate()).
So you could also query your table like this:
select *
from your_table
where month_column = MONTH(getdate())
and year_column = YEAR(getdate())
This should work for SQL Server:
SELECT * FROM myTable
WHERE month = DATEPART(m, GETDATE()) AND
year = DATEPART(yyyy, GETDATE())
This should work in MySql
SELECT * FROM 'my_table' WHERE 'month' = MONTH(CURRENT_TIMESTAMP) AND 'year' = YEAR(CURRENT_TIMESTAMP);
How about:
Select *
from some_table st
where st.month = to_char(sysdate,'MM') and
st.year = to_char(sysdate,'YYYY');
should work in Oracle. What database are you using? I ask because not all databases have the same date functions.
DECLARE #CMonth int=null
DECLARE #lCYear int=null
SET #lCYear=(SELECT DATEPART(YEAR,GETDATE()))
SET #CMonth=(SELECT DATEPART(MONTH,GETDATE()))
select *
from your_table
where MONTH(mont_year) = MONTH(NOW())
and YEAR(mont_year) = YEAR(NOW());
Note: (month_year) means your column that contain date format. I think that will solve your problem. Let me know if that query doesn't works.
Related
I want to display data from user selected year of March to next year of April(ex:if user selected 2017, then need to select data from from 2017 march to april 2018 ). What is the best query for filtering it. It should be order by from selected year.
SELECT DATEPART(mm,submitted_date),SUM(total_count),YEAR(submitted_date)
FROM store_details
WHERE YEAR(submitted_date) = '2017'
AND MONTH(submitted_date) >=3
Group By YEAR(submitted_date),DATEPART(mm,submitted_date)
Order By YEAR(submitted_date),DATEPART(mm,submitted_date)
Well, later versions (2012+) of SQL Server have a nice built in function called datefromparts. However, since your question is tagged with sql-server-2005 this doesn't help so much unless you can upgrade your database (and you really should think about it, since 2005 is no longer supported by Microsoft).
However, since you know you want all the data between March and April of next year, you can simply do this:
SELECT DATEPART(MONTH,submitted_date),SUM(total_count),YEAR(submitted_date)
FROM store_details
WHERE submitted_date >= CAST(CAST(#Year as char(4)) + '-03-01 00:00:00' As datetime)
WHERE submitted_date < CAST(CAST(#Year + 1 as char(4)) + '-04-01 00:00:00' As datetime)
Group By YEAR(submitted_date),DATEPART(MONTH,submitted_date)
Order By YEAR(submitted_date),DATEPART(MONTH,submitted_date)
(That is, assuming the user provides the year as an int value.)
If you already have the starting month and date in hand, then I don't see why you can't just add an upper bound to the date in your WHERE clause:
SELECT
DATEPART(mm, submitted_date),
SUM(total_count),
YEAR(submitted_date)
FROM store_details
WHERE
(YEAR(submitted_date) = '2017' AND MONTH(submitted_date) >= 3) AND
(YEAR(submitted_date) = '2018' AND MONTH(submitted_date) < 4)
GROUP BY
YEAR(submitted_date),
DATEPART(mm, submitted_date)
ORDER BY
YEAR(submitted_date),
DATEPART(mm, submitted_date)
Using SQLCe, I have a column of DateTime type. I would like to filter just by year. Is it possible or should I store year separately, which seems to me redundant?
E.g. get distinct results of 2010,2011,2013.
Thanks
think you have the DATEPART function (but not the YEAR function)
so
select DatePart(yyyy, <yourDateTime>)
or if that's for ordering, of course
order by DatePart(yyyy, <yourDatetime>)
EDIT
select max(InvoiceID)
from yourTable
where DatePart(yyyy, IssuedDate) = 2013
You can use the DATEPART function to return the year for that column:
SELECT DATEPART(yyyy, datetimecolumn) FROM YourTable
You can then filter with a where clause:
WHERE datetimecolumn = 2014
The usual way to do this is to use a range filter:
select *
from table
where datecolumn >= '2012/01/01' and datecolumn < '2013/01/01'
This has the benefit that any index you may have on datecolumn can be used.
Since the answer you accepted shows that you only care about one single year, your objection to this answer doesn't really apply.
select max(InvoiceID)
from table
where IssuedDate >= '2012/01/01' and IssuedDate < '2013/01/01'
will work just fine.
I have a table that contains records with a column indicates the Date. Given a record, I would like to select all records that are in the same week as the record. How can SQL do that?
I should say that I'm using SQLite.
You can use DATEPART with wk to get the current week. Then just check for equality.
In this case, I have also checked yy to make sure that you do not check the year of a previous week.
SELECT *
FROM TABLE
WHERE DATEPART(wk, TABLE.DATECOLUMN)
= DATEPART(wk, (SELECT DATECOLUMN FROM TABLE WHERE ID = GivenID))
AND DATEPART(yy, TABLE.DATECOLUMN) = DATEPART(yy, (SELECT DATECOLUMN FROM TABLE WHERE ID = GivenID))
UPDATE FOR SQLITE
To do this in SQLLite, Refer to this SO question and then this article that states %W is what you use to get week and %Y for year. Which gives you:
SELECT *
FROM TABLE
WHERE STRFTIME('%W', TABLE.DATECOLUMN)
= STRFTIME('%W', (SELECT DATECOLUMN FROM TABLE WHERE ID = GivenID))
AND STRFTIME('%Y', TABLE.DATECOLUMN)
= STRFTIME('%Y', (SELECT DATECOLUMN FROM TABLE WHERE ID = GivenID))
Use the datediff() function:
datediff(ww,start_date,end_date) < 1
You can use BETWEEN to specify the records you want.
SELECT * FROM records WHERE datecolumn between 'YYYY/MM/DD' AND 'YYYY/MM/DD'
I have a table called logD, with a field named date (datefield type). Format is "year-month-day" IE: 2011-04-11
If today's date is 2011-07-31, I want all the records for the month of July. If today's date is 2011-02-14, I want all the records for the month of Feb and so on.
I am using SQL 2008 and reporting services to run a monthly report.
Try this:
SELECT *
FROM logD
WHERE YEAR(DATE) = YEAR(GetDate())
AND MONTH(DATE) = MONTH(GetDate())
If you have an Index on the column DATE then you can try this one:
SELECT *
FROM logD
WHERE [DATE] BETWEEN CONVERT(VARCHAR(6),GETDATE(),112)+'01' AND DATEADD(DAY,-1,DATEADD(MONTH,1,CONVERT(VARCHAR(6),GETDATE(),112)+'01'))
But, man, it looks ugly...
Try this:
DECLARE #firstOfMonth smalldatetime = dateadd(month,datediff(month,0,getdate()),0);
SELECT * FROM logD
WHERE [date] > #firstOfMonth
AND [date] < dateadd(month,1,#firstOfMonth);
How would retrieve all customer's birthdays for a given month in SQL? What about MySQL?
I was thinking of using the following with SQL server.
select c.name
from cust c
where datename(m,c.birthdate) = datename(m,#suppliedDate)
order by c.name
don't forget the 29th February...
SELECT c.name
FROM cust c
WHERE (
MONTH(c.birthdate) = MONTH(#suppliedDate)
AND DAY(c.birthdate) = DAY(#suppliedDate)
) OR (
MONTH(c.birthdate) = 2 AND DAY(c.birthdate) = 29
AND MONTH(#suppliedDate) = 3 AND DAY(#suppliedDate) = 1
AND (YEAR(#suppliedDate) % 4 = 0) AND ((YEAR(#suppliedDate) % 100 != 0) OR (YEAR(#suppliedDate) % 400 = 0))
)
Personally I would use DATEPART instead of DATENAME as DATENAME is open to interpretation depending on locale.
If you're asking for all birthdays in a given month, then you should supply the month, not a date:
SELECT c.name
FROM cust c
WHERE datepart(m,c.birthdate) = #SuppliedMonth
I'd actually be tempted to add a birthmonth column, if you expect the list of customers to get very large. So far, the queries I've seen (including the example) will require a full table scan, as you're passing the the data column to a function and comparing that. If the table is of any size, this could take a fair amount of time since no index is going to be able to help.
So, I'd add the birthmonth column (indexed) and just do (with possible MySQLisms):
SELECT name
FROM cust
WHERE birthmonth = MONTH(NOW())
ORDER BY name;
Of course, it should be easy to set the birthmonth column either with a trigger or with your client code.
SELECT * FROM tbl_Employee WHERE DATEADD( Year, DATEPART( Year, GETDATE()) - DATEPART( Year, DOB), DOB) BETWEEN CONVERT( DATE, GETDATE()) AND CONVERT( DATE, GETDATE() + 30)
This can be use to get the upcoming Birthday by days