How to use WEEK and DAYOFWEEK functions in SQL Server [duplicate] - sql

This question already has answers here:
Week() function in sql script
(3 answers)
Closed 4 years ago.
I have a simple code written in MySQL which returns the week number based on specific date:
Is there any equivalent in SQL Server? I was trying to use it by none function works
WEEK(ADDDATE(date,5-DAYOFWEEK(date)),3)

Try DATEPART
DATEPART(dw, '2007-04-21')
where dw is DayOfWeek

Related

Previous Workday in Where Clause [duplicate]

This question already has answers here:
How to get Previous business day in a week with that of current Business Day using sql server
(8 answers)
Select the previous business day by using SQL
(3 answers)
Closed 3 months ago.
How do I obtain the previous working date in a where clause
without doing the following and changing this manually
WHERE date = GETDATE()-1
The date column is datetime but I just need the date too.

SYSTEM DATE as realtime entry to database [duplicate]

This question already has answers here:
Create a function to return current date and time in oracle
(4 answers)
Closed 8 years ago.
I need to get the system date as Last Modified date in my Database, what will be the query I need to give in Oracle SQL Developer ?
Use
current_date
or Use
TO_CHAR

how to find the weekend dates from given list of dates [duplicate]

This question already has answers here:
Get day of week in SQL Server 2005/2008
(11 answers)
Closed 8 years ago.
I have a list of dates and other columns in which have to find the weekend dates among them and the the weekend dates should be there in the list of given dates
Output should be some thing like this...
Any help is appricated thanks.
use datepart (dw,..
to filter data needed

SQL last month date of given date [duplicate]

This question already has answers here:
Get the last day of the month in SQL
(22 answers)
Closed 9 years ago.
i am using sql
How to get the last day of the month for a given date
01/03/2014 should return 01/31/2014
I only need the date part.. Thanks
For SQL-SERVER, try this:
SELECT convert(varchar,(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,'01/03/2014 ')+1,0))),101)

SQL date ranges [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
90 days range using SQL server
I am trying to get the counts 90 days prior to the operational date and the counts 90 days after the operational date. For example, my operational date is 4/1/2004. So,90 days prior to 4/1/2004 is (1/2/2004 to 3/31/2004) and 90 days after (including 4/1/2004) is 6/29/2004.
I used the following scripts and mannually calculate the days, which is not efficient...
select
site,
count(*) as prior_counts
from mytable
where mydate >='1/2/2004'
and mydate <'4/1/2004'
group by site
select
site,
count(*) as after_counts
from mytable
where mydate >='4/1/2004'
and mydate <'6/30/2004'
group by site
You should look at the DATEDIFF function or equivalent if you're not using SQL Server.
DATEDIFF on MSDN
If you are passing the date parameter in from an application, consider modifying the application to do the date range calculation for you. By passing in two parameters, you are taking the burden of the calculation off SQL, which will improve performance.