Change Month and Year in a datetime - tsql

I have a datetime field in my query.
Select Account, Period, JEDate
From table
I would like to replace the year of the "JEDate" datetime field with a Parameter "#Year" (int).
I would also like to replace the month with the "Period" (int) field.
Example:
If the JEDate was 1900-01-01 00:00:00.000 (or any other date), the year was 2013 and the month was 2, I would like the JEDate to be 2013-02-01 00:00:00.000.
How do I do this?
Thank you

Perhaps:
UPDATE dbo.TableName
SET JEDate = CONVERT(DATETIME, CAST(#Year as varchar(4)) + '-' + CAST(Period as VARCHAR(2)) + '-' + '01', 102)
WHERE JEDate IS NOT NULL
Demo

An alternative approach that adds the difference between the original year and month, and the new year and month:
select dateadd(year, #year-Year(JEDATE), dateadd(month, Period-Month(JEDATE), JEDATE))

Related

DATE ADD function in PostgreSQL

I currently have the following code in Microsoft SQL Server to get users that viewed on two days in a row.
WITH uservideoviewvideo (date, user_id) AS (
SELECT DISTINCT date, user_id
FROM clickstream_videos
WHERE event_name ='video_play'
and user_id IS NOT NULL
)
SELECT currentday.date AS date,
COUNT(currentday.user_id) AS users_view_videos,
COUNT(nextday.user_id) AS users_view_next_day
FROM userviewvideo currentday
LEFT JOIN userviewvideo nextday
ON currentday.user_id = nextday.user_id AND DATEADD(DAY, 1,
currentday.date) = nextday.date
GROUP BY currentday.date
I am trying to get the DATEADD function to work in PostgreSQL but I've been unable to figure out how to get this to work. Any suggestions?
I don't think PostgreSQL really has a DATEADD function. Instead, just do:
+ INTERVAL '1 day'
SQL Server:
Add 1 day to the current date November 21, 2012
SELECT DATEADD(day, 1, GETDATE()); # 2012-11-22 17:22:01.423
PostgreSQL:
Add 1 day to the current date November 21, 2012
SELECT CURRENT_DATE + INTERVAL '1 day'; # 2012-11-22 17:22:01
SELECT CURRENT_DATE + 1; # 2012-11-22 17:22:01
http://www.sqlines.com/postgresql/how-to/dateadd
EDIT:
It might be useful if you're using a dynamic length of time to create a string and then cast it as an interval like:
+ (col_days || ' days')::interval
You can use date + 1 to do the equivalent of dateadd(), but I do not think that your query does what you want to do.
You should use window functions, instead:
with plays as (
select distinct date, user_id
from clickstream_videos
where event_name = 'video_play'
and user_id is not null
), nextdaywatch as (
select date, user_id,
case
when lead(date) over (partition by user_id
order by date) = date + 1 then 1
else 0
end as user_view_next_day
from plays
)
select date,
count(*) as users_view_videos,
sum(user_view_next_day) as users_view_next_day
from nextdaywatch
group by date
order by date;

Convert String "Friday June 1, 2018" to Date

I have a column in database from imported file in the format Friday June 1, 2018 and trying to format to a valid date conversion from varchar to date.
I have tried cast and convert with no success
SELECT CAST([Course date] as DATETIME)
FROM [dbo].[Test]
SELECT CONVERT(datetime, [Course date], 103)
FROM [dbo].[Test]
I expected conversion to type 103 UK dd/mm/yyyy
TRY_CONVERT seems to be able to handle your date string. Note that the name of the day is superfluous, and is not needed to determine exactly what the date is. So, we can remove it using base string functions.
WITH yourTable AS (
SELECT 'Friday June 1, 2018' AS datecol
)
SELECT
datecol AS input,
TRY_CONVERT(datetime, STUFF(datecol, 1, CHARINDEX(' ', datecol), '')) AS output
FROM yourTable;
Demo

subtracting month AND days with dateadd()

Trying to get minus 1 month and 6 days from todays date GETDATE()??
SELECT (DATEADD(mm,-1,GETDATE()) + (DATEADD(d,-6,GETDATE())))
This is returning : 2132-11-02 01:10:33.500
SELECT DATEADD(dd, -6, DATEADD(mm,-1, GETDATE()))

Get First and Last Day of Any Year

I'm currently trying to get the first and last day of any year. I have data from 1950 and I want to get the first day of the year in the dataset to the last day of the year in the dataset (note that the last day of the year might not be December 31rst and same with the first day of the year).
Initially I thought I could use a CTE and call DATEPART with the day of the year selection, but this wouldn't partition appropriately. I also tried a CTE self-join, but since the last day or first day of the year might be different, this also yields inaccurate results.
For instance, using the below actually generates some MINs in the MAX and vice versa, though in theory it should only grab the MAX date for the year and the MIN date for the year:
;WITH CT AS(
SELECT Points
, Date
, DATEPART(DY,Date) DA
FROM Table
WHERE DATEPART(DY,Date) BETWEEN 363 AND 366
OR DATEPART(DY,Date) BETWEEN 1 AND 3
)
SELECT MIN(c.Date) MinYear
, MAX(c.Date) MaxYear
FROM CT c
GROUP BY YEAR(c.Date)
You want something like this for the first day of the year:
dateadd(year, datediff(year,0, c.Date), 0)
and this for the last day of the year:
--first day of next year -1
dateadd(day, -1, dateadd(year, datediff(year,0, c.Date) + 1, 0)
try this
for getting first day ,last day of the year && firstofthe next_year
SELECT
DATEADD(yy, DATEDIFF(yy,0,getdate()), 0) AS Start_Of_Year,
dateadd(yy, datediff(yy,-1, getdate()), -1) AS Last_Day_Of_Year,
DATEADD(yy, DATEDIFF(yy,0,getdate()) + 1, 0) AS FirstOf_the_NextYear
so putting this in your query
;WITH CT AS(
SELECT Points
, Date
, DATEPART(DY,Date) DA
FROM Table
WHERE DATEPART(DY,Date) BETWEEN
DATEPART(day,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)) AND
DATEPART(day,dateadd(yy, datediff(yy,-1, getdate()), -1))
)
SELECT MIN(c.Date) MinYear
, MAX(c.Date) MaxYear
FROM CT c
GROUP BY YEAR(c.Date)
I should refrain from developing in the evenings because I solved it, and it's actually quite simple:
SELECT MIN(Date)
, MAX(Date)
FROM Table
GROUP BY YEAR(Date)
I can put these values into a CTE and then JOIN on the dates and get what I need:
;WITH CT AS(
SELECT MIN(Date) Mi
, MAX(Date) Ma
FROM Table
GROUP BY YEAR(Date)
)
SELECT c.Mi
, m.Points
, c.Ma
, f.Points
FROM CT c
INNER JOIN Table m ON c.Mi = m.Date
INNER JOIN Table f ON c.Ma = f.Date

How to convert TSQL Datetime to Julian INT date

I would like know how to convert date in sql to julian date int,
eg.
if my date was "01 Mar 2011" how do I convert this to Julian date (734197)?
declare #d datetime = '20110301'
select datediff(d, 0, #d) + 693596
DECLARE #normal_date VARCHAR(30)='2024/3/30';
SELECT #normal_date AS NormalDate,CONVERT(varchar,(CONVERT(INT,
SUBSTRING(#normal_date,1,2))%20)+1)+ CONVERT(varchar,
SUBSTRING(CONVERT(varchar,( datepart(year, #normal_date) * 1000 +
datepart(dy, #normal_date))), 3, 5)) AS JulianDate;