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)
)
Related
I had used this as Where Condition in Oracle
(ProjectDate between trunc(sysdate-1)+15/24 and trunc(sysdate)+8/24)
I was trying to convert view to SQL Server and I used to try
ProjectDate between (GetDate()-1)+15/24 and (GetDate())+8/24
I am not sure if using right function or not?
You need to use following expression:
ProjectDate between
DATEADD(HOUR,15,CONVERT(DATETIME, CONVERT(DATE, GETDATE())) - 1)
and DATEADD(HOUR,8,CONVERT(DATETIME, CONVERT(DATE, GETDATE())))
DATEADD: The DATEADD() function adds a time/date interval to date and then returns the date.
CONVERT(DATETIME, CONVERT(DATE, GETDATE())) returns the same as TRUNC(SYSDATE) in oracle
I am writing an munit test case and mocking my real SQL Server database tables with H2 in-memory instance.
My query has date condition to return records from previous month, so I was looking for something that works with SQL Server as well as H2. Finally I found below which syntactically works in both.
select
DATEADD(MONTH, DATEDIFF(MONTH, '1900-01-01', GETDATE()) - 1, '1900-01-01');
In SQL Server, it properly gives me start date of previous month but in H2, it is giving me start date of current month. If change -1 to 2 then H2 works but SQL Server returns the wrong date.
Can someone please help with how I can get correct syntax?
Same problem with last day of previous month -
select
DATEADD(D, -1, DATEADD(MONTH, DATEDIFF(MONTH, '1900-01-01', GETDATE()), '1900-01-01'))
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
I would like to convert a SQL query in MS Access into SQL Server. This query is about converting date into day of week. Please advise.
MS Access:
UPDATE Contact_Hist
SET WK_DAY = weekdayname(weekday(cdate(mid(date_id,5,2) & "/" & right(date_id,2) & "/" & left(date_id,4))));
For instance, DATE_ID = 20140703 as per the table (Contact_Hist) and would like to convert to the day of week i.e. 'Thursday'.
You should use SQL Server's DatePart() function:
DatePart(weekday, Cast('20140703' AS date))
This will return an integer between 1 (Monday) and 7 (Sunday) inclusive.
<edit2>
How the DatePart() return value [integer] is interpreted is determined by SQL Server's SET DATEFIRST setting (or default configuration). That is, whether a returned 1 is Monday, 2 is Monday, and so on.
</edit2>
<edit>
To get the name of the day of the week use the DateName() function.
DateName(weekday, Cast('20140703' AS date))
For reference purposes, both DatePart() and DateName() take a "date" (that is, a time, date, smalldatetime, datetime, datetime2, or datetimeoffset value) as the 2nd parameter. SQL Server will implicitly convert a date string into a date, so the CAST('20140703' AS date) is not required.
</edit>
In MS SQL Server 2012, you can do the following and get "Friday" back:
SELECT DATENAME ( weekday , '20140704' )
as demonstrated by this SQLFiddle: http://sqlfiddle.com/#!6/fc24a/8
So do:
UPDATE Contact_Hist SET WK_DAY = DATENAME ( weekday , date_id )
When I select two rows with a DATETEIME stamp, I only want the m/d/y data and nothing after that.
It has to changed during the select (not afterwards).
To remove the time you just need to do the following Assuming SQL Server
Pre SQL 2008
select DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0) yourdate
FROM yourtable
SQL 2008
select CAST(getdate() as date) yourdate
FROM yourtable
See Most efficient way in SQL Server to get date from date+time?
or
Best approach to remove time part of datetime in SQL Server
Is this for export? If you only want the text you can use a variety of coversion formats available on MSDN.
select convert(varchar, getdate(), 101)
-- output: 07/05/2011
Otherwise, if you're using sql 2008, you can just cast the datetime to date:
select cast(getdate() as date)