FAQ Database Discussion Community


SQL display months when Count is 0 [duplicate]

sql,date,count,sql-server-2008-r2,datepart
This question already has an answer here: Include monthly counts including months where data doesn't exist 1 answer I was wondering if someone could help me... I have the following SQL query (have shortened this down as its a large union query) SELECT [ Month ], sum(total) from (select...

Datepart() is there any way to use a variable for the datepart interval

sql-server,tsql,datepart
I'm creating a SQL server stored procedure that is doing a lot of filtering based on the current week. Rather than passing in a start and end date, I am passing in a single date and using Datepart() to filter out the week. Where DATEPART(WEEK, cd.TurninDate) = DATEPART(WEEK, @StartDate) and...

How do I check whether the input date's DAY is the last day of the given month in SQL?

sql,sql-server,datepart
DECLARE @Date DATE = '2/28/2014' I need to test whether the DAY in the DATE above is the LAST day of the month and it should work for all months in the year. If it's true, then I need to return a true, if not, then false. E.g. SET @Date...

MSSQL - Getting last 6 weeks returns last 8 weeks

sql,sql-server,dateadd,datepart
I am having a problem with week numbers. The customers week starts on a Tuesday, so ends on a Monday. So I have done: Set DateFirst 2 When I then use DateAdd(ww,@WeeksToShow, Date) It occasionally gives me 8 weeks of information. I think it is because it goes over to...

how to extract date and time parts from mysql max query

mysql,select,max,datepart
I need to select a max(date) from a MySQL table and have each part of the date and time be in a separate variable. Something like: select max(date) as year, month, day, hour, minute, second from table How would I do that? ...

How to use DATEPART of the GETDATE as a variable for a query

sql,sql-server,date,getdate,datepart
This might be easier than I'm making it, I'm pretty new to SQL. I have a database with a lot of movies release dates in the format YYYY-MM-DD. I'm trying to write a query that would return films released on the current date (MM-DD) in various years. I've tried using:...

PostgreSQL 9.3 : Get day of the week

postgresql,postgresql-9.3,datepart
I want to retrieve day of the week from the given date. For example the date is: 2015-01-28 then it has to return Wednesday My bad try: select date_part('week',now()) as weekday; The above script gives me only number of week but not the day of the week....

Querying the exact years in between two other dates (MS SQL)

sql-server-2008,case,datediff,datepart
I am struggling with an assignment and I am not sure if there is a solution. To be clear: I am not looking for the amount of years in between(datediff()). I have to join two tables: Table 1 with attributes: ID P_Startyear P_Endyear Table 2 with attributes: ID B_Year There...