Update Day and Month Except Year From Another Column - sql-server

I have table with two datetime columns in SQL Server 2000, I want to update only day and month in column_a with day and month in column_b except year.
For example:
column_a
----------
1/2/2009
1/2/2002
1/2/2016
3/1//1998
11/12/1987
column_b
---------
31/12/2015
11/10/2005
27/6/2017
31/12/2010
31/12/2011
Desired results:
31/12/2009
11/10/2002
27/6/2016
31/12/1998
31/12/1987
Thank you for your help.

Using date literals should work in SQL Server 2000, style 112 returns the date in YYYYMMDD so take the YYYY of one and add to MMDD of the other and convert that to datetime.
update tablex
set columnb = convert(datetime(left(convert(varchar, column_a, 112), 4) +
right(convert(varchar, column_b, 112), 4), 112)
It's a great deal easier in later versions. SQL Server 2000 is dead to me.
Use DATEFROMPARTS ( year, month, day )
update tablex
set columnb = datefromparts(year(column_a),month(column_b), day(column_b))

Related

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 select last three month all dates in SQL Server

I want to select all last 3 month dates from the table in SQL Server.
Data is like this:
Sunday 20-05-2012
Sunday 27-05-2012
Sunday 10-06-2012
Sunday 24-06-2012
Sunday 08-07-2012
Sunday 22-07-2012
Sunday 12-08-2012
Sunday 19-08-2012
Sunday 09-09-2012
Sunday 16-09-2012
Saving date column as varchar with day of week - bad idea. You can't write faster query, because you must convert all of time your field for using it in where clause. Also your query can't use indexes of date_column and each query will be use scan index.
With datetime column, query should be:
select date_column
from table_name
where date_column between dateadd(m, -3, getdate()) and getdate()
Try using this query:
select date_column from table_name
where datepart(m,date_column) > datepart(m,getdate())-3 and datepart(yy,date_column) >= datepart(yy,getdate())

Date part function in 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

TSQL Function to calculate 30 WORKING days Date from a Specified Date (SQL Server 2005)

TSQL Function to calculate 30 WORKING days Date from a Specified Date (SQL Server 2005)?
Input parameters would be Date and Number of Working Days.
Output would be the Calculated Date.
This would exclude Saturday, Sunday, Holidays and Day Holiday was observered.
i.e. If the Holiday falls on a weekend but it is observed on the Friday or Monday after the holiday.
For the Holidays we have a table with holiday and day it is being observed date.
Have a look at this article by Celko - in general you need to "pre-calculate" a calendar table to take in account all possible vagaries like Easter, bank holidays etc.
There's one right in the SQL online help if you scroll down to UDF to return the number of business days, including a check to a bank holidays table
you can tweak this.
Instead of writing a tsql function, it might easier if you build a table that's similar to the Date Dimension (DimDate) table in data warehouse. DimDate would contain a column named isHoliday. You can also add other columns that might be useful. Then you write a script to populate DimDate
Then you can run a query off it.
I don't have a table of holidays handy, so I haven't tested this very much - but as nobody else has attempted an answer, here's how I'd start:
declare #tempDate datetime,
#StartDate datetime,
#WorkingDays int,
#NonWorkingDays int,
#TargetDate datetime
set #StartDate = '2010-10-26' --Change this to a paramter
set #WorkingDays = 9 --Change this to a parameter
set #NonWorkingDays = -1
/*Work out the answer ignoring holidays */
set #tempDate = dateadd(d,#WorkingDays,#StartDate)
while (dateadd(d,#WorkingDays + #NonWorkingDays, #StartDate) < #tempDate)
begin
/*Work out how many holidays are in the interval we've worked out*/
select #NonWorkingDays = count(HolidayDate)
from Holidays
where HolidayDate between #StartDate and #tempDate;
/*Extend the interval to include the holidays we've just found*/
set #tempDate = dateadd(d,#NonWorkingDays,#tempDate)
/*See if #NonWorkingDays has changed with the new #tempDate*/
select #NonWorkingDays = count(HolidayDate)
from Holidays
where HolidayDate between #StartDate and #tempDate;
end
set #TargetDate = dateadd(d,#WorkingDays + #NonWorkingDays, #StartDate)
print 'Target Date: ' + cast(#TargetDate as varchar(50))
Note this only works for Holidays at the moment - not weekends. You'd have to load all weekends into the holiday table (or join to a weekends table or use the DATENAME function) but the calculation should be the same.
Not sure how your Holiday table handles duplicate dates (eg. Boxing Day and St Stephen's Day both fall on 26th Dec) so you might need to take account of that.

How do I exclude Weekend days in a SQL Server query?

How do I exclude values in a DateTime column that are Saturdays or Sundays?
For example, given the following data:
date_created
'2009-11-26 09:00:00' -- Thursday
'2009-11-27 09:00:00' -- Friday
'2009-11-28 09:00:00' -- Saturday
'2009-11-29 09:00:00' -- Sunday
'2009-11-30 09:00:00' -- Monday
this is the result I'm looking for:
date_created
'2009-11-26 09:00:00' -- Thursday
'2009-11-27 09:00:00' -- Friday
'2009-11-30 09:00:00' -- Monday
Thanks!
When dealing with day-of-week calculations, it's important to take account of the current DATEFIRST settings. This query will always correctly exclude weekend days, using ##DATEFIRST to account for any possible setting for the first day of the week.
SELECT *
FROM your_table
WHERE ((DATEPART(dw, date_created) + ##DATEFIRST) % 7) NOT IN (0, 1)
SELECT date_created
FROM your_table
WHERE DATENAME(dw, date_created) NOT IN ('Saturday', 'Sunday')
Assuming you're using SQL Server, use DATEPART with dw:
SELECT date_created
FROM your_table
WHERE DATEPART(dw, date_created) NOT IN (1, 7);
EDIT: I should point out that the actual numeric value returned by DATEPART(dw) is determined by the value set by using SET DATEFIRST:
http://msdn.microsoft.com/en-us/library/ms181598.aspx
Try the DATENAME() function:
select [date_created]
from table
where DATENAME(WEEKDAY, [date_created]) <> 'Saturday'
and DATENAME(WEEKDAY, [date_created]) <> 'Sunday'
The answer depends on your server's week-start set up, so it's either
SELECT [date_created] FROM table WHERE DATEPART(w,[date_created]) NOT IN (7,1)
if Sunday is the first day of the week for your server
or
SELECT [date_created] FROM table WHERE DATEPART(w,[date_created]) NOT IN (6,7)
if Monday is the first day of the week for your server
Comment if you've got any questions :-)
Calculate Leave working days in a table column as a default value--updated
If you are using SQL here is the query which can help you: http://gallery.technet.microsoft.com/Calculate...
Try this code
select (DATEDIFF(DD,'2014-08-01','2014-08-14')+1)- (DATEDIFF(WK,'2014-08-01','2014-08-14')* 2)

Resources