DATEPART(ww,Date) with SET DATEFIRST - sql-server

I´m using SQL Server 2005
I´m trying to get the week with DatePart(ww,date) function
My code
SELECT datepart(ww,'2012-01-08 00:00:00')
Return 2
But I want ...
Return 1
According with IS0-8601 and this table from this website
YEAR 2012
Week-01 From 2012-1-2 to 2012-1-8
...
Am I wrong?
There is any trick with SET DATEFIRST 1, I´m trying without success.
Thanks for your time
I can´t use ISO_WEEK, because SQL Server 2005 don´t work

Use ISO_WEEK:
SELECT datepart(ISO_WEEK,'2012-01-08 00:00:00')
You can read more about it on MSDN.
Edit:
I didn't realize ISO_WEEK was not available in SQL Server 2005. Since it's based on the Thursday of the week, the problem now shifts to finding the Thursday from the given day:
DECLARE #Date date = '2012-01-08'
DECLARE #Thursday date = DATEADD(DAY, 3-(DATEPART(WEEKDAY, #Date) + ##DATEFIRST - 2) % 7, #Date)
SELECT (DATEPART(DAYOFYEAR,#Thursday) - 1) / 7 +1

Related

Convert IIF() and datepart Access statement to SQL Server

I have a query that pulls some information a given week and it should be on a week day hence the datepart portion. can't get this command to pull into SQL correctly.
IIf(DatePart('w',Date())=2,Date()-3,Date()-1) And Date()))
The above is from the SQL view of access.
dateadd(day,
case datepart(weekday, getdate())
when 2 then -3 else -1 end, /* assumes datefirst is 7 */
cast(getdate() as date)
)

Find Weekending Date (Friday) based on another Date column in SQL Server?

I am trying to replicate a weekending query that I use in access, in SQL Server and not having much luck with getting ti to perform how I need it to.
Basically, I need to find the week-ending date (with the week-ending date being a Friday) of another date column and would like it formatted to be American short date (eg. 10/06/2017).
I use the below in Access which gets me the result I need. So if the ACTUAL_DATE is 10/03/2017, the result I would need is 10/06/2017.
Actual_Date_WE: [ACTUAL_DATE] + 7 - Weekday([ACTUAL_DATE], 7)
Thank you! :)
try this
Declare #Date date
SET #Date = '2017-10-01'
select dateadd(day, (7+(6 - datepart(WEEKDAY, #date))) %7, #date)
declare #i date='10/3/2017'
declare #Friday int=6
SELECT convert(varchar,dateadd(day,(#Friday-DATEPART(dw,#i)),#i) ,110)

SQL Date - functions

>=DateAdd("ww",-9,Date()-Weekday(Date(),0)+1))
I have the above function in some SQL coding that I have inherited and am struggling to work out what it is actually calculating can anyone help?
Consider this SQL with the statement deconstructed into parts.
It was tested in sql-server 12, and uses the GETDATE() instead of the Date() function.
select
GETDATE() as today,
##DATEFIRST as first_day_of_the_week_number,
DATEPART(WEEKDAY,GETDATE()) as current_day_number_of_the_week,
GETDATE()-DATEPART(WEEKDAY,GETDATE()) as previous_saturday,
GETDATE()-DATEPART(WEEKDAY,GETDATE())+1 as previous_sunday,
DATEADD("ww",-9,GETDATE()-DATEPART(WEEKDAY,GETDATE())+1) as previous_sunday_9_weeks_back;
So it would give the sunday of 9 weeks back.
Note that it assumes that ##DATEFIRST equals 7.
If ##DATEFIRST equals 1 then it would return the monday of 9 weeks back.

How to get 3 letter abbreviation for month in SQL

How to get month in 3 letters in SQL.
In SQL Table data is inserted:
2016-01-07 09:38:58.310
I need only month result in 3 letters like below:
Jan
Assuming you're using SQL Server 2012 or newer, you can use the FORMAT function:
SELECT FORMAT([Date], 'MMM', 'en-US')
Adapt the locale as needed.
Since you're on SQL Server 2008, I'd use
SELECT LEFT(DATENAME(MONTH, [Date]), 3)
Try this (I am assuming you are using Sql Server).
Select Convert(char(3), GetDate(), 0)
If you need full name of month, try
Select Datename(month, GetDate())
Or you could just do:
LEFT(GETDATE(), 3)
For instance you could declare a variable:
Declare #MONTH VARCHAR(3)
Set #MONTH = LEFT(GETDATE(), 3)

How to default the time for the date with SQL Server

Does anyone know how can I default the time for the date with SQL Server?
Example:
When I use getdate() and it will return me the current date and time. How can I get the current date but default the time portion to '10:00:00.000' ?
This works for SQL Server (SQLFiddle):
SELECT DATEADD(hour, 10, CAST(CAST(GETDATE() AS DATE) AS DATETIME))
Explanation: GETDATE() gives current date and time. Casting to DATE makes it date only (midnight time). Casting it again to DATETIME makes it compatible with DATEADD, which finally adds 10 hours to it.
Use the below if you are using sql server.
select cast(cast(getdate()AS INT)+0.41667 as datetime)
Note time is scaled on a 0.0 to 0.9999, and the no. of hours are equally distributed. e.g. 0.5 will give 12a.m.
This is from DB2. But same concept should work very DB. Just convert that date in to Timestamp
SELECT TIMESTAMP(CURRENT_DATE, TIME('10:00:00')) AS MY_DATE FROM SYSIBM.SYSDUMMY1
in sql server 2008 and above:
select convert(datetime,convert(varchar(10),convert(date,(GETDATE())))+' 00:00:00')
select convert(datetime,convert(varchar,convert(date,getdate())) + ' 10:00:00.000')
SQL FIDDLE
Again, in Ms SQL Server, You can also use
Select DateAdd(day, datediff(day, getdate()), 0) + 10/24.0

Resources