Date part function in SQL server - sql-server

I have a sample data set below:
Date
01/01/2010
01/02/2010
01/03/2010
Running the query below gives:
SELECT
DATEPART (MONTH, Date) AS MONTH
FROM MYTABLE
OUTPUT:
MONTH
1
I would like to the output as mm/dd/yyyy format as below.
MONTH
01/01/2010
Could someone please look into it?
Thanks a bunch!

If you want to truncate date to a month, you might use:
select dateadd(m, datediff(m, 0, getdate()), 0)
Put your date column instead of getdate().

If you are running the latest SQL Server 2012, you can use datefromparts function to make a "first of the month" date, like this:
SELECT
DATEFROMPARTS(YEAR(Date), MONTH(Date), 1) AS FIRST_OF_THE_MONTH
FROM MYTABLE

Related

How to get date before 3 months from a particular date, say current date in SQL?

How can we deduce the date falling exactly 3 months prior to the current date in SQL Server?
use dateadd
select convert(date, dateadd(month,-3,getdate()))
output
16/07/2018 00:00:00
Use DATEADD() function
SELECT DATEADD(M, -3,GETDATE()) AS WithTime,
CAST(DATEADD(M, -3, GETDATE()) AS DATE) AS WithoutTime
If you are working on newest versions of SQL Server (2012+) I would recommand to use TRY_CONVERT() or TRY_CAST() functions.

Update SQL dates in SQL Server 2008

this is what I need: from the saledate column I need to extract just the month and date and combine with the 2017 year in NewDate column, but I couldn't update. Any suggestions?
This is the Select statment, I'm trying to update with the alias NewDate and getting an error: The conversion of a varchar data type to a datetime data type resulted in an out of range value.
This is the data in the saledate column: 1983-09-01 00:00:00.000, I'm trying to make to be the same, just the year to be 2017.
SELECT saledate, renewaldate,CONVERT(date,saledate), ('2017'+ '-' + LTRIM(REVERSE(SUBSTRING(REVERSE(CONVERT(date,saledate)), 1, 5)))) AS NewDate FROM tprogram
UPDATE tprogram
SET renewaldate = ('2017'+ '-' + LTRIM(REVERSE(SUBSTRING(REVERSE(CONVERT(date,saledate)), 1, 5)))) FROM tprogram
You could use dateadd() with the day() and month() functions like so:
select dateadd(day,day(saledate)-1,dateadd(month,month(saledate)-1,'20180101')) as NewDate
For example:
select dateadd(day,day(getdate())-1,dateadd(month,month(getdate())-1,'20180101'))
returns: 2018-05-16
You say you need it with the 2017 year, but you're using a 2018 value. Here's something to get started.
SELECT CONVERT(DATE,'2017-'+CONVERT(VARCHAR(2),MONTH(SaleDate))+'-'+CONVERT(VARCHAR(2),DAY(SaleDate))) AS NewDate
You can use datefromparts as below:
select SaleDate, RenewalDate, Convert(date, SaleDAte),
DATEFROMPARTS(2018, datepart(month,SaleDate),DATEPART(day,SaleDate)) as NewDate
from yourtable
Just add the difference in years back to the sale date.
SELECT DATEADD(YEAR,DATEDIFF(YEAR,SaleDate,'20170101'),SaleDate)

How to retrieve all rows with a date in this and the next week? SQL Server

I have a Tasks table that contains Date, Description and WorkerID columns. I want to write a stored procedure that selects all tasks from this week and the following week. Can someone help me get to a solution please? I am not so good at this.
If indexing (performance) isn't a concern then it's a simple expression if your week coincides with the SQL Server setup:
where datediff(week, getdate(), [Date]) between 0 and 1
The snippet below is another option that works with both dates and datetimes. It's possible to adapt it to work with any ##datefirst setting but I'm just going to assume that the setting matches with the start of week you're looking to report on.
where
[Date] >= dateadd(day, 1 - datepart(weekday, getdate()), cast(getdate() as date))
and [Date] < dateadd(day, 15 - datepart(weekday, getdate()), cast(getdate() as date))
The following should give you what you want (assuming Monday is the first day of the week, and that the DBMS is SQL Server):-
select *
from Tasks
where [Date]
between
dateadd(dd,-(datepart(dw,[Date])-2),[Date]) --Monday of this week
and
dateadd(dd,13,dateadd(dd,-(datepart(dw,[Date])-2),[Date])) --Sunday of next week

Select Records for which the date field is spent a year

I have a table with columns
TypeExame, DateExame
How can I select all records with (Now-DateExame) > = 365 days?
select *
from MyTable
where datediff (day, DateExame, getdate()) >= 365
See DATEDIFF and GETDATE for further details.
If you have an index on DateExame, putting the condition like this will enable its usage:
SELECT *
FROM atable
WHERE DateExame <= DATEADD(DAY, -365, CAST(GETDATE() AS date))
;
The above references the date data type, which means you'd need at least SQL Server 2008 to run that. To make it work in earlier versions, you could rewrite the condition like this:
WHERE DateExame <= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) - 365, 0)
Basically, the modified version uses the DATEADD/DATEDIFF method of truncating a datetime value with a minor tweak to also subtract 365 days along the way.
However, if all your DateExame values are dates without the time part, this simpler version should work just as well:
WHERE DateExame <= DATEADD(DAY, -365, GETDATE())
That is, removal of GETDATE()'s result's time part would be perfectly unnecessary.
Try this one :
select *
from MyTable
where datediff (day, DateExame, getdate()) >= 365
By Using Datediff function you can subtract two dates . you can pass first argument as minute , day , month , year etc .

SQL Query-calculating last week data for an ordinary calendar year

I have to retrieve the last week data from a table.
i am using the following condition.
#prmCurrent_Year=Datepart(year,getdate())
last_week=case when Datepart(week,col_name)=1
then 52
else Datepart(week,col_name)-1 and
year_num=case
when Datepart(week,col_name)=1
then #prmCurrent_Year-1
else #prmCurrent_ Year
will this work properly or is there any other better query for this???
Something like this might suffice:
SELECT *
FROM Table
WHERE MyDate BETWEEN DATEADD(wk, -1, GetDate()) AND GetDate()
DateAdd reference on MSDN: http://msdn.microsoft.com/en-us/library/ms186819.aspx

Resources