Changing Date Format of Twitter Api DATA in SQL SERVER - sql-server

i want to change date format of twitter api data it like this :
Fri Sep 11 08:31:48 +0000 2009
i want to convert it into format like this :
YYYY-DD-MM 00:00:00
How do i convert this in sql server 2012

Kinda late, but this is what worked for me
alter table Test
add [date2] as (
CONVERT(DATETIME, SUBSTRING(created_at,9,2)+'/'
+ CASE SUBSTRING(created_at,5,3)
WHEN 'Jan' then '01/'
WHEN 'Feb' then '02/'
WHEN 'Mar' then '03/'
WHEN 'Apr' then '04/'
WHEN 'May' then '05/'
WHEN 'Jun' then '06/'
WHEN 'Jul' then '07/'
WHEN 'Aug' then '08/'
WHEN 'Sep' then '09/'
WHEN 'Oct' then '10/'
WHEN 'Nov' then '11/'
WHEN 'Dec' then '12/'
else '' end
+RIGHT(created_at,4) + ' '
+ SUBSTRING(created_at,12,8), 105)
)
PERSISTED;

Related

how to convert month date in a text column into datetime

I have a table containing two columns like this:
Month_Date Year
Dec 31 2018
May 01 2020
Jun 05 2021
Jan 18 2022
Jul 19 2019
I hope to combine the Month_date and year in the same row and put it in a new column as a datetime format column. Could anyone help, please? I tried to convert the first column to a valid date, but failed because it doesn't show a complete month name.
You can try below approach.
Approach1:
select convert(datetime, [Month_Date] + ', ' + cast([Year] as varchar(4)), 107) from <TableName>
Approach2:
select cast(right(month_date,2)+'-'+left(month_date,3)+'-'+cast([Year] as varchar(4)) as date) from <TableName>

Date column order by

I have a date column CreateDate (datetime, not null) in my table.
Using the following where clause:
Where
-- Code for 12 Months Back at a Time
CreateDate >= dateadd(month,datediff(month, 0, getdate())-12,0)
If I run my report say on March 1, 2017, I should get 12 months of data. I do.
I would like to order by CreateDate and get the following order
Mar 2017
Feb 2017
Jan 2017
Dec 2016
Nov 2016
Oct 2016
Sep 2016
Aug 2016
Jul 2016
Jun 2016
May 2016
Apr 2016
Here is the complete query:
SELECT DISTINCT
tblCourtAttorneysAssigned.CourtAssignment AS [CourtAssignment],
tblCourtAttorneysAssigned.AttorneyBarNumber AS [AttorneyBarNumber],
tblCourtAttorneysAssigned.AttorneyFirstName AS [AttorneyFirstName],
tblCourtAttorneysAssigned.AttorneyLastName AS [AttorneyLastName],
--tblCourtAttorneysAssigned.CaseNo,
--tblPeople.LastName,
--tblPeople.FirstName,
tblCourtAttorneysAssigned.HowAssigned AS [HowAssigned],
CONVERT( VARCHAR(10), tblCourtAttorneysAssigned.CreateDate, 101) AS[CreateDate]
FROM tblBookIn
INNER JOIN tblOffense ON tblBookIn.BookInID = tblOffense.BookInID
INNER JOIN tblPeople ON tblBookIn.PersonID = tblPeople.PersonID
INNER JOIN tblCourtAttorneysAssigned ON tblOffense.CaseNo = tblCourtAttorneysAssigned.CaseNo
WHERE CourtAssignment LIKE 'M%'
AND
-- Code for 12 Months Back at a Time
CreateDate >= dateadd(month,datediff(month, 0, getdate())-12,0)
ORDER by
[CreateDate] asc
--courtassignment ASC,
-- AttorneyLastName ASC;
Thanks
I am guessing you are using order by CreateDate desc but you have aliased CreateDate in your query to be something like format(CreateDate, 'MMM yyyy') as CreateDate
So your order by will need to know you want it to use the original value instead of the new value that is being returned with the same name.
Try order by mytablename.CreateDate desc
Update after question edited to include query:
change your order by to:
order by tblCourtAttorneysAssigned.CreateDate desc
I think you should use order by clause at the end
SELECT { fn MONTHNAME(OrderDate) } AS MonthName, YEAR(OrderDate) AS Year, SUM(TotalValue) AS Profits
FROM [Order]
WHERE (YEAR(OrderDate) = #year)
GROUP BY { fn MONTHNAME(OrderDate) }, YEAR(OrderDate)
ORDER BY { fn MONTH(OrderDate) }, YEAR(OrderDate)
or
you can do like this
SELECT CASE { fn MONTH(OrderDate) }
when 0 then 'JAN'
when 1 then 'FEB'
when 2 then 'MAR'
when 3 then 'APR'
when 4 then 'MAY'
when 5 then 'JUN'
when 6 then 'JUL'
when 7 then 'AUG'
when 8 then 'SEP'
when 9 then 'OCT'
when 10 then 'NOV'
when 11 then 'DEC'
END
AS MonthName, YEAR(OrderDate) AS Year, SUM(TotalValue) AS Profits
FROM [Order]
WHERE (YEAR(OrderDate) = #year)
GROUP BY { fn MONTH(OrderDate) }, YEAR(OrderDate)
ORDER BY { fn MONTH(OrderDate) }, YEAR(OrderDate)
Thanks all, I have resolved the issue by removing the Convert on the date field and fixing the format in SSRS.
Thanks,

MS SQL DATE manipulation with Season of the Year

I have a table which includes the columns Season and Academic_Year. Academic_Year is type int. Season can be Spring, Summer or Fall.
Fall 2016 is from 1 Sep 2016 to 31 Dec 2016.
Spring 2016 is from 1 Jan 2017 to 31 Mar 2017 (not a typo - the Academic Year 2016 is from Sep 2016 to Aug 2017)
Summer 2016 is from 1 Apr 2017 to 31 Aug 2017
I need to select rows when getdate() is between a number of days before and after the start of the Season, in pseudo code
WHERE
( getdate() > 31/3/16 AND getdate() < 31/9/16 AND Season = 'Autumn' AND Academic_Year = 2016 )
OR ( getdate() > 31/9/16 AND getdate() < 31/1/17 AND Season = 'Spring' AND Academic_Year = 2016 )
OR ( getdate() > 31/12/16 AND getdate() < 31/4/17 AND Season = 'Summer' AND Academic_Year = 2016 )
My problem is creating the date for comparison with getdate(). I need to combine a string - '31/3/' - with an int - Academic_Year, and create a date.
The solution does not need to be efficient but it does need to work on 2008 and later sql server versions.
SQL will allow this conversion if you just concatenate the varchars and tell it to convert to date. For example:
declare #year int = 2016
declare #date varchar(10) = '3/31'
select cast(#date + '/' + cast(#year as varchar(4)) as date)
You have to cast the integer year to varchar to combine it with the other varchar, but you can then cast the entire thing to a date (or datetime) once you have a recognized date format.
You can form your date by doing by using concatenation of the string and year.
CONVERT(VARCHAR(4), Academic_Year) + '-03-31'
Once you have the date string you could use the date functions to manipulate it further if necessary.
WHERE (GETDATE() > CONVERT(VARCHAR(4),Academic_Year) + '-03-31'
AND GETDATE() < CONVERT(VARCHAR(4),Academic_Year) + '-09-30'
AND Season = 'Autumn') etc
worked for me. The trick was adding '-03-31' rather than preceeding with '31-03-'. Thanks #kicken

Query last 12 months of data where year and month are separate columns

I'm trying to figure out a way to query the last 12 completed months of data, so it would go up to last month and ignore this month.
I've done this before when the table had a date column, but this new table I'm working with separates the date into two month and year columns.
How would I go about this in SQL Server 2012?
Thanks in advance.
SAMPLE DATA:
CalendarYear CalendarMonth TotalSales
2014 3 35.00
2014 4 220.00
2015 2 243.00
2015 5 17.93
2015 6 216.36
2015 10 370.93
2015 12 350.00
2016 1 116.75
2016 2 13.78
DESIRED OUTPUT (assuming current time is in February 2016):
CalendarYear CalendarMonth TotalSales
2015 2 243.00
2015 5 17.93
2015 6 216.36
2015 10 370.93
2015 12 350.00
2016 1 116.75
I've assumed the Day part is always the 1st of the month.
WHERE
CONVERT(DATETIME, CalendarYear + '-' + CalendarMonth + '- 01') >= DATEADD(mm, -12, GETDATE())
The above assumes the columns are strings. Below I've done a version where the columns are numbers.
WHERE
CONVERT(DATETIME, CAST(CalendarYear AS NVARCHAR(4)) + '-' + CAST(CalendarMonth AS NVARCHAR(2)) + '-01') >= DATEADD(mm, -12, GETDATE())
You can just apply same code/logic to year and month you want data.
where to_date(year || month, 'YYYYMM')
between add_months(trunc(sysdate, 'MM'), -12) and trunc(sysdate)

yearweek on SQL server

How can I extract the year and week combination from a date in SQL Server using T-SQL and have it match the MySQL yearweek result?
For example these MySQL queries (Click here for MySQL SQL Fiddle):
SELECT yearweek('2013-12-31');
SELECT yearweek('2014-01-01');
This returns 201352 for both dates. That is the expected result.
But in SQL Server...
datepart works as expected for the year extract
sometimes datepart does not return the expected value for iso_week
The MySQL result cannot be achieved with this T-SQL query...
SELECT datepart(year, #dt) * 100 + datepart (iso_week, #dt);
T-SQL versions of the MySQL queries above (Click here for T-SQL SQL Fiddle):
SELECT datepart(year, '2013-12-31') * 100 + datepart (iso_week, '2013-12-31');
SELECT datepart(year, '2014-01-01') * 100 + datepart (iso_week, '2014-01-01');
The result is 201352 for the first date and 201401 for the second date.
However, this is not the expected result because...
2014-01-01 belongs to the last week of 2013
So the expected result is 201352
Do any of you more experienced T-SQL developers know how to extract the year/week of a given date and have this match what I see in MySQL?
I need to have the week start on Monday. This is why I am using iso_week. I have tested the results with week anyway and found the same issue. This query also produces 201401 when 201352 is expected.
SELECT datepart(year, '2014-01-01') * 100 + datepart (week, '2014-01-01');
If you look at the ISO_Week datepart definition at http://msdn.microsoft.com/en-us/library/ms174420.aspx, you'll see the following:
ISO 8601 includes the ISO week-date system, a numbering system for
weeks. Each week is associated with the year in which Thursday
occurs...
The numbering system in different countries/regions might not comply
with the ISO standard.
Since January 1, 2014 was a Wednesday; January 2, 2014 was the first Thursday of the year and thus week 1 of 2014 (according to ISO 8601).
Furthermore, looking at the MySQL definitions for yearweek and week, there are several mode options (http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_week)
So, I think you're going to have to write your own yearweek function for the week counting rule you want:
CREATE FUNCTION dbo.yearweek(#date date)
RETURNS INT
as
begin
set #date = dateadd(dd,-datepart(dw,#date)+1, #date)
return datepart(year,#date)*100 + datepart(week,#date)
end
go
select dbo.yearweek('2013-12-31'), dbo.yearweek('2014-01-01')
NOTE: I haven't fully tested this code, and I'm not sure exactly what your requirements are. This is just meant as an example of the type of process you need to follow.
I think it's because you're using "ISO_WEEK" as the datepart
Look at the description under the "ISO_WEEK datepart" here: http://msdn.microsoft.com/en-us/library/ms174420.aspx
using "WEEK" instead might get the desired result
TO extract YEAR, MONTH or DAY you can simply use the YEAR, MONTH and DAY function as shown in the followinf example. for any other part you have to use the DATEPART function , read here for a detailed list of arguements you can pass to DATEPART function.
SELECT YEAR(GETDATE()) AS [YEAR]
, DATEPART(WEEK, GETDATE()) [Week]
UPDATE
SELECT CAST(YEAR(GETDATE()) AS VARCHAR(4)) +'-'
+ CAST(DATEPART(WEEK, GETDATE()) AS VARCHAR(2))
It seems to me that what you want is a week number based on the previous Monday. Is this correct? If so, iso_week is no use to you as it is based on this week's Thursday.
To get the year and week based on the previous Monday, I think you need something like the following.
set datefirst 1 ;
select
SomeDate ,
datename( weekday, SomeDate ) as [WeekdayName] ,
datepart( weekday, SomeDate ) as [Weekday] ,
datepart( week, SomeDate ) as [Week] ,
datepart( iso_week, SomeDate ) as [ISO_week] ,
dateadd( day, -( datepart( weekday, SomeDate ) - 1 ), SomeDate ) as [PreviousMonday] ,
datepart( year, dateadd( day, -( datepart( weekday, SomeDate ) - 1 ), SomeDate ) ) as [MondayYear] ,
datepart( week, dateadd( day, -( datepart( weekday, SomeDate ) - 1 ), SomeDate ) ) as [MondayWeek]
from
dbo.DateDemo
order by
SomeDate ;
Abbreviated sample output is as follows.
SomeDate PreviousMonday MondayYear MondayWeek
2013-12-25 2013-12-23 2013 52
2013-12-26 2013-12-23 2013 52
2013-12-27 2013-12-23 2013 52
2013-12-28 2013-12-23 2013 52
2013-12-29 2013-12-23 2013 52
2013-12-30 2013-12-30 2013 53
2013-12-31 2013-12-30 2013 53
2014-01-01 2013-12-30 2013 53
2014-01-02 2013-12-30 2013 53
2014-01-03 2013-12-30 2013 53
2014-01-04 2013-12-30 2013 53
2014-01-05 2013-12-30 2013 53
2014-01-06 2014-01-06 2014 2

Resources