Update SQL dates in SQL Server 2008 - sql-server

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)

Related

Update Day and Month Except Year From Another Column

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))

When I select between two dates in SQL Server, the result is returned from in range and out range together?

I have this query
select *
from Vw_storeout
where ItemDate between '14/10/2018' and '15/10/2018'
The result gets data from September and October. Actually just I want data from October.
I've changed date format to varchar(50).
Date is date, you should compare it as date not string.
select *
from Vw_storeout
where TRY_CAST(ItemDate AS DATE) between CAST('20181014' AS DATE)
and CAST('20181015' AS DATE);
You should store ItemDate as DATE instead of VARCHAR(50).

SQL: Query where date is like today

I have a little problem with my SQL Query:
I have this value on my table: 2014-10-23 00:00:00
I have a record like this each 2 minutes every day, and I need to SELECT all the value of today.
Now I made this:
WHERE mytable.data LIKE CURDATE()
and it doesn't work. I tried a lot of things found here on stackoverflow and nothing could help me.
Thanks for answer.
You don't say which version of SQL Server you are using, if you have the DATE data type available you can cast the datetime returned by getdate() or CURRENT_TIMESTAMP to strip off the time part.
But then you need a range to find all the matching rows, something like:
SELECT *
FROM mytable
WHERE mytable.data >= CAST(GETDATE() AS date)
AND mytable.data < CAST(DATEADD(day, 1, GETDATE()) AS date)
SQL Fiddle

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

Selecting date value without time in SQL Server 2005

I am facing a problem selecting the date in SQL Server 2005. The table name is Softskill and the column name is insertdate. The data is stored as 2011-09-22 08:50:28.000 in this format.
To select, I am passing values from the front end as '2011-09-22', I mean the date only.
I tried to use
SELECT INSERTDATE FROM SOFTSKILL WHERE INSERTDATE = '2011-09-22'.
But it is not showing the record. May I know which format the column is following and is there any way to retrieve data by using only date?
You want to search for a range of times that encompass the day. Something like:
SELECT INSERTDATE
FROM SOFTSKILL
WHERE INSERTDATE >= '2011-09-22 00:00:00'
AND INSERTDATE < '2011-09-23 00:00:00'
You could also apply functions to INSERTDATE to extract just the date portion, but that will make the query nonsargable.
SELECT *
FROM SOFTSKILL
WHERE DATEADD(dd, 0, DATEDIFF(dd, 0, InsertDate)) = '2011-09-22'
This will do what you need
SELECT INSERTDATE
FROM SOFTSKILL
WHERE CAST(FLOOR(CAST(INSERTDATE AS FLOAT)) AS DATETIME) = '2011-09-30'
You need to truncate INSERTDATE so its time part is 00:00:00.000; then, it will be equal to '2011-09-30' assuming INSERTDATE is '2011-09-30 XX:XX:XX.xxx'.

Resources