Sub CTE or Nested Model - sql

I have a string variable with month and year. This can be multiple seperated with comma. I need to get all the dates of all months in the string
Eg: IF my String have '2014-01,2014-02' I need to return 01-01-2014, 02-01-2014 etc like all days in january and also all dates in february.
I tried like below
DECLARE #myString as NVARCHAR(2000) = '2014-03,2014-04'
;with cte1
as (
SELECT
CAST(
CAST(SUBSTRING(Data, 0, 5) AS VARCHAR(4)) +
RIGHT('0' + CAST(SUBSTRING(Data, 6, 2) AS VARCHAR(2)), 2) +
RIGHT('0' + CAST(1 AS VARCHAR(2)), 2)
AS DATE) AS StartDate
,CONVERT(DATE,DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,CAST(
CAST(SUBSTRING(Data, 0, 5) AS VARCHAR(4)) +
RIGHT('0' + CAST(SUBSTRING(Data, 6, 2) AS VARCHAR(2)), 2) +
RIGHT('0' + CAST(1 AS VARCHAR(2)), 2)
AS DATETIME))+1,0))) AS EndDate
FROM dbo.Split(myString , ',')
),
cte2
AS
(
SELECT StartDate, EndDate FROM cte1
UNION ALL
SELECT CONVERT(DATE, DATEADD(DAY,1 , StartDate)), CONVERT(DATE, DATEADD(DAY,1 , StartDate)) FROM cte2
WHERE CONVERT(DATE, DATEADD(DAY,1 , StartDate)) < EndDate
)
SELECT * FROM cte2
In the first cte i will return 2 rows with start date of the month and end date of the month. So i need to loop all the days in each row of the first cte
I have the work around with 2 seperate queries
-- To get the month start and end date
;with cte1
as (
SELECT
CAST(
CAST(SUBSTRING(Data, 0, 5) AS VARCHAR(4)) +
RIGHT('0' + CAST(SUBSTRING(Data, 6, 2) AS VARCHAR(2)), 2) +
RIGHT('0' + CAST(1 AS VARCHAR(2)), 2)
AS DATE) AS StartDate
,CONVERT(DATE,DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,CAST(
CAST(SUBSTRING(Data, 0, 5) AS VARCHAR(4)) +
RIGHT('0' + CAST(SUBSTRING(Data, 6, 2) AS VARCHAR(2)), 2) +
RIGHT('0' + CAST(1 AS VARCHAR(2)), 2)
AS DATETIME))+1,0))) AS EndDate
FROM dbo.Split(#CompareMonths, ',')
)
--To get all days between start and end date (Current month)
Declare #startdate date = CONVERT(DATE, DATEADD(dd, -DAY(DATEADD(MONTH, 0, GETDATE())) + 1, DATEADD(MONTH, 0, GETDATE())))
Declare #endDate date = DATEADD(DAY, -DAY(DATEADD(MONTH, 1, GETDATE())), DATEADD(MONTH, 1, GETDATE()))
;With cte
as
(
SELECT #startdate sDate
UNION All
SELECT DATEADD(day,1,sDate) FROM cte WHERE DATEADD(day,1,sDate) <= #endDate
)
select * from cte
I need to join this 2 concepts so that from the string i got all the days of all the months i passed

With the help of a numbers table and a splistring function.
SQL Fiddle
MS SQL Server 2012 Schema Setup:
create table dbo.Number(N int primary key);
go
insert into dbo.Number(N)
select top(11000) row_number() over(order by 1/0) - 1
from sys.all_objects as o1, sys.all_objects as o2
go
create function [dbo].[SplitString](#List nvarchar(max), #Delimiter nvarchar(255)) returns table
with schemabinding
as
return
(
select substring(#List, N, charindex(#Delimiter, #List + #Delimiter, N) - N) as Item
from dbo.Number
where N <= convert(int, len(#List)) and
substring(#Delimiter + #List, N, len(#Delimiter)) = #Delimiter
);
Query 1:
declare #S nvarchar(max) = '2014-01,2014-02'
select T2.D
from dbo.SplitString(#S, N',') as S
cross apply (select cast(S.Item+'-01' as date)) as T1(D)
cross apply (
select dateadd(day, N.N, T1.D)
from Number as N
where N between 0 and datediff(day, T1.D, eomonth(T1.D))
) as T2(D)
Results:
| D |
|------------|
| 2014-01-01 |
| 2014-01-02 |
| 2014-01-03 |
| 2014-01-04 |
| 2014-01-05 |
| 2014-01-06 |
| 2014-01-07 |
| 2014-01-08 |
| 2014-01-09 |
| 2014-01-10 |
| 2014-01-11 |
| 2014-01-12 |
| 2014-01-13 |
| 2014-01-14 |
| 2014-01-15 |
| 2014-01-16 |
| 2014-01-17 |
| 2014-01-18 |
| 2014-01-19 |
| 2014-01-20 |
| 2014-01-21 |
| 2014-01-22 |
| 2014-01-23 |
| 2014-01-24 |
| 2014-01-25 |
| 2014-01-26 |
| 2014-01-27 |
| 2014-01-28 |
| 2014-01-29 |
| 2014-01-30 |
| 2014-01-31 |
| 2014-02-01 |
| 2014-02-02 |
| 2014-02-03 |
| 2014-02-04 |
| 2014-02-05 |
| 2014-02-06 |
| 2014-02-07 |
| 2014-02-08 |
| 2014-02-09 |
| 2014-02-10 |
| 2014-02-11 |
| 2014-02-12 |
| 2014-02-13 |
| 2014-02-14 |
| 2014-02-15 |
| 2014-02-16 |
| 2014-02-17 |
| 2014-02-18 |
| 2014-02-19 |
| 2014-02-20 |
| 2014-02-21 |
| 2014-02-22 |
| 2014-02-23 |
| 2014-02-24 |
| 2014-02-25 |
| 2014-02-26 |
| 2014-02-27 |
| 2014-02-28 |
Ref: Split strings the right way – or the next best way

Related

SQL query to CONCAT/list unique column data if rows have identical data in the other columns?

Thanks in advance. I'm basically trying to run a SQL query so that the results are 1:1:1:1:Many for StaffID:Name:Floor:Date:Shifts.
Here is my initial query and example results:
SELECT
ST.STAFFNUM [StaffID],
ST.FULLNAME [Name],
ST.AREA [Floor],
CONVERT(VARCHAR(10), TS.EVENTDATE, 103) [Date],
LEFT(CONVERT(VARCHAR(10), TS.SHIFTSTART, 8), 5) [ShiftStart],
LEFT(CONVERT(VARCHAR(10), TS.SHIFTEND, 8), 5) [ShiftEnd]
FROM
TIMES TS
LEFT JOIN STAFF ST ON TS.STAFFNUM = ST.STAFFNUM
WHERE
TS.EVENTDATE BETWEEN '2021/01/01' AND '2021/01/01'
ORDER BY
ST.AREA,
ST.FULLNAME,
TS.EVENTDATE,
TS.SHIFTSTART
;
StaffID | Name | Floor | Date | ShiftStart | ShiftEnd
==============================================================
1000 | Andrew | 1 | 01/01/2021 | 06:00 | 14:00
1000 | Andrew | 1 | 01/01/2021 | 14:00 | 15:00
8654 | Belinda | 2 | 01/01/2021 | 06:00 | 14:00
9876 | Craig | 3 | 01/01/2021 | 06:00 | 14:00
I then combined the ShiftStart and ShiftEnd columns with the following to get the further below results:
CONCAT(LEFT(CONVERT(VARCHAR(10), TS.SHIFTSTART, 8), 5),'-',LEFT(CONVERT(VARCHAR(10), TS.SHIFTEND, 8), 5)) [Shift]
StaffID | Name | Floor | Date | Shift
====================================================
1000 | Andrew | 1 | 01/01/2021 | 06:00-14:00
1000 | Andrew | 1 | 01/01/2021 | 14:00-15:00
8654 | Belinda | 2 | 01/01/2021 | 06:00-14:00
9876 | Craig | 3 | 01/01/2021 | 06:00-14:00
What I can't figure out to do next though is to combine Andrew's shifts (and anyone else's multiple shifts on the same Date and Floor etc) like the below:
StaffID | Name | Floor | Date | Shifts
=================================================================
1000 | Andrew | 1 | 01/01/2021 | 06:00-14:00, 14:00-15:00
8654 | Belinda | 2 | 01/01/2021 | 06:00-14:00
9876 | Craig | 3 | 01/01/2021 | 06:00-14:00
NOTE: If someone is transferred to another Floor (occasional), I want to keep that Shift/s on separate rows unique to the Floor so the Floor data can be split up and emailed to that floor's manager.
As long as all data of the rows are identical except for the Shift, I want to combine those rows and list the Shifts.
Thanks again!
As mentioned, you should use the String_Agg function as follows:
SELECT [StaffID], Max([Name]) As Name, [Floor], [Date], String_Agg([Shift], ',') WITHIN GROUP (ORDER BY [Shift]) As Shifts
FROM (
SELECT
ST.STAFFNUM [StaffID],
ST.FULLNAME [Name],
ST.AREA [Floor],
CONVERT(VARCHAR(10), TS.EVENTDATE, 103) [Date],
CONCAT(LEFT(CONVERT(VARCHAR(10), TS.SHIFTSTART, 8), 5),'-',LEFT(CONVERT(VARCHAR(10), TS.SHIFTEND, 8), 5)) [Shift]
FROM
TIMES TS
LEFT JOIN STAFF ST ON TS.STAFFNUM = ST.STAFFNUM
WHERE
TS.EVENTDATE BETWEEN '2021/01/01' AND '2021/01/01'
ORDER BY
ST.AREA,
ST.FULLNAME,
TS.EVENTDATE,
TS.SHIFTSTART) As T
Group by [StaffID], [Floor], [Date]
For Sql Server 2012, you can try "CTE" with "FOR XML PATH".
WITH CTE As
(SELECT
ST.STAFFNUM [StaffID],
ST.FULLNAME [Name],
ST.AREA [Floor],
CONVERT(VARCHAR(10), TS.EVENTDATE, 103) [Date],
CONCAT(LEFT(CONVERT(VARCHAR(10), TS.SHIFTSTART, 8), 5),'-',LEFT(CONVERT(VARCHAR(10), TS.SHIFTEND, 8), 5)) [Shift]
FROM
TIMES TS
LEFT JOIN STAFF ST ON TS.STAFFNUM = ST.STAFFNUM
WHERE
TS.EVENTDATE BETWEEN '2021/01/01' AND '2021/01/01'
ORDER BY
ST.AREA,
ST.FULLNAME,
TS.EVENTDATE,
TS.SHIFTSTART)
SELECT [StaffID], MAX([Name]) AS [Name], [Floor], [Date],
STUFF((SELECT ', ' + [Shift]
FROM CTE
WHERE [StaffID] = T.[StaffID] AND [Floor] = T.[Floor] AND [Date] = T.[Date]
FOR XML PATH(''),TYPE).value('(./text())[1]','VARCHAR(MAX)'),1,1,'') AS Shifts
FROM CTE AS T
GROUP BY [StaffID], [Floor], [Date]

Break a date range into hours per day for each job

Yesterday I had asked for an efficient way to break a date range into hours per day and received an answer at the following link...
Is there an efficient way to break a date range into hours per day?
Now I need to go a step further and generate the same thing for each job in a list. I have a table with the following sample information...
+-------+-------------------------+-------------------------+
| JobID | StartDate | EndDate |
+-------+-------------------------+-------------------------+
| 1 | 2015-01-27 07:32:35.000 | 2015-01-28 14:39:35.000 |
| 2 | 2015-01-27 07:32:35.000 | 2015-01-29 16:39:35.000 |
| 3 | 2015-03-02 09:46:25.000 | 2015-03-05 17:24:15.000 |
+-------+-------------------------+-------------------------+
And I need to get a list like the following...
+-------+------------+-------+
| JobID | Date | Hours |
+-------+------------+-------+
| 1 | 2015-01-27 | 16.47 |
| 1 | 2015-01-28 | 14.65 |
| 2 | 2015-01-27 | 16.47 |
| 2 | 2015-01-28 | 24.00 |
| 2 | 2015-01-29 | 16.65 |
| 3 | 2015-03-02 | 14.23 |
| 3 | 2015-03-03 | 24.00 |
| 3 | 2015-03-04 | 24.00 |
| 3 | 2015-03-05 | 17.40 |
+-------+------------+-------+
Can the recursive CTE (from the link I included) be modified to include a JobID?
Thanks,
Carl
Here is what I came up with for a solution...
DECLARE #testTable TABLE (JobID INT, startdate DATETIME, enddate DATETIME);
INSERT INTO #testTable VALUES (1,'2015-01-27 07:32:35.000','2015-01-28 14:39:35.000');
INSERT INTO #testTable VALUES (2,'2015-01-27 07:32:35.000','2015-01-29 16:39:35.000');
INSERT INTO #testTable VALUES (3,'2015-03-02 09:46:25.000','2015-03-02 17:24:15.000');
WITH cte AS (
SELECT JobID,CAST(startdate AS DATE) startdate,DATEDIFF(minute, startdate, DATEADD(DAY, 1, CAST(startdate AS DATE) ) ) / 60.0 hours,enddate from #testTable
UNION ALL
SELECT JobID,DATEADD(DAY,1, startdate), DATEDIFF(minute, DATEADD(DAY,1, startdate), CASE WHEN DATEADD(DAY,2, startdate) > enddate
THEN enddate ELSE DATEADD(DAY,2, startdate) END) / 60.0, enddate
FROM cte
WHERE startdate <> CAST(enddate AS DATE)
)
SELECT * FROM cte
ORDER BY JobID, startdate

SQL Days before end of the month

i have got table with transactions, looking like:
+----+--------------+----------------+------+
| ID | OrderDate | DeliveryDate | EUR |
+----+--------------+----------------+------+
| 1 | 2015-02-21 | 2015-02-25 | 100 |
| 2 | 2015-03-01 | 2015-03-14 | 110 |
| 3 | 2015-03-01 | 2015-03-17 | 90 |
| 4 | 2015-03-10 | 2015-03-20 | 250 |
| 5 | 2015-03-31 | 2015-03-31 | 350 |
+----+--------------+----------------+------+
ANd I need to get sum of revenue and number of orders (COUNT of IDs) based on Days before the end of the month when order gets delivered.
SELECT datediff(day, OrderDate, CAST(DATEADD(month, DATEDIFF(month,0,getdate()+1,0)-1) as Date) as DBEOM, SUM(EUR) as Rev, COUNT(ID) as NumberOfOrders
FROM transactions
WHERE MONTH(DeliveryDate) = 3 AND YEAR(DeliveryDate) = 2015
GROUP BY datediff(day, OrderDate, CAST(DATEADD(month, DATEDIFF(month,0,getdate()+1,0)-1) as Date) as DBEOM
ORDER BY 1
The result in this case would be like:
+-----+-----+----------------+
|DBEOM| Rev | NumberOfOrders |
+-----+-----+----------------+
| 0 | 350 | 1 |
| 21 | 250 | 1 |
| 30 | 200 | 2 |
+-----+-----+----------------+
This is done in SQL 2008, so I can't simply use EOMONTH. I have tried, what is above, but i am getting
ERROR -
[Microsoft][ODBC SQL Server Driver][SQL Server]The datediff function
requires 3 argument(s).
Many thanks in advance for advice!
The easiest way I've found get the last day of the month with more primitive functions is to get the first day of the next month and then subtract a day.
I'm not a TSQL guy so this syntax likely won't be correct but you need something more like
DATEADD(day, DATEFROMPARTS(DATEPART(year, DATEADD(month,1,getdate()), DATEPART(month, DATEADD(month,1,getdate()), 1), -1)
Try:
SELECT datediff(day,
OrderDate,
dateadd(DAY,
-1,
dateadd(MONTH,
1,
dateadd(DAY,
1-day(DeliveryDate),
DeliveryDate
)
)
)
) as DBEOM, SUM(EUR) as Rev, COUNT(ID) as NumberOfOrders
FROM t
WHERE MONTH(DeliveryDate) = 3 AND YEAR(DeliveryDate) = 2015
GROUP BY datediff(day,
OrderDate,
dateadd(DAY,
-1,
dateadd(MONTH,
1,
dateadd(DAY,
1-day(DeliveryDate),
DeliveryDate
)
)
)
)
ORDER BY 1
sqlfiddle.com

SQL not delivering expected result with RIGHT JOIN

I have been working on this query for some time now, and reading right join question after right join question here on SO, but I cannot figure this one out.
I have the following Query:
DECLARE #ExpectedDateSample VARCHAR(10)
DECLARE #Date datetime
DECLARE #DaysInMonth INT
DECLARE #i INT
--GIVE VALUES
SET #ExpectedDateSample = SUBSTRING(CONVERT(VARCHAR, DATEADD(MONTH, +0, GETDATE()), 112),5,2)+'/'+CONVERT(VARCHAR(4), DATEPART(YEAR, GETDATE()))
SET #Date = Getdate()
SELECT #DaysInMonth = datepart(dd,dateadd(dd,-1,dateadd(mm,1,cast(cast(year(#Date) as varchar)+'-'+cast(month(#Date) as varchar)+'-01' as datetime))))
SET #i = 1
--MAKE TEMP TABLE
CREATE TABLE #TempDays
(
[days] VARCHAR(50)
)
WHILE #i <= #DaysInMonth
BEGIN
INSERT INTO #TempDays
VALUES(#i)
SET #i = #i + 1
END
SELECT DATEPART(DD, CONVERT(DATE, a.budg_tempDODate1, 103)) ExpectedDate, SUM(a.budg_do1_total) ExpectedAmount
FROM CRM.dbo.budget a
RIGHT JOIN #TempDays on DATEPART(DD, CONVERT(DATE, a.budg_tempDODate1, 103)) = #TempDays.days
WHERE DATEPART(MONTH, a.budg_tempDODate1) = DATEPART(MONTH, GetDate()) AND DATEPART(YEAR, a.budg_tempDODate1) = DATEPART(YEAR, GetDate())
GROUP BY a.budg_tempDODate1
--DROP TABLE TO ALLOW CREATION AGAIN
DROP TABLE #TempDays
In my Budget table I have a few days out of the month missing, but that is why I create a Temp table to count all the days of the month. And then RIGHT join to that Temp table.
I am trying to calculate how much cash is expected on each day of the month. If the day does not exist in my budget table, DO NOT leave it out completely, but rather display 0 is expected.
What I am currently getting
+------+---------------+
| DAYS | AMOUNT |
+------+---------------+
| 1 | 34627.000000 |
| 2 | 72474.000000 |
| 3 | 27084.000000 |
| 4 | 9268.000000 |
| 5 | 32304.000000 |
| 6 | 23261.000000 |
| 7 | 5614.000000 |
| 9 | 3464.000000 |
| 10 | 20046.000000 |
| 12 | 7449.000000 |
| 13 | 265163.000000 |
| 14 | 24210.000000 |
| 15 | 68848.000000 |
| 16 | 31702.000000 |
| 17 | 2500.000000 |
| 19 | 2914.000000 |
| 20 | 238406.000000 |
| 21 | 15642.000000 |
| 22 | 2514.000000 |
| 23 | 46521.000000 |
| 24 | 34093.000000 |
| 25 | 899081.000000 |
| 26 | 204085.000000 |
| 27 | 316341.000000 |
| 28 | 48826.000000 |
| 29 | 2657.000000 |
| 30 | 440401.000000 |
+------+---------------+
What I was Expecting:
+------+---------------+
| DAYS | AMOUNT |
+------+---------------+
| 1 | 34627.000000 |
| 2 | 72474.000000 |
| 3 | 27084.000000 |
| 4 | 9268.000000 |
| 5 | 32304.000000 |
| 6 | 23261.000000 |
| 7 | 5614.000000 |
| 8 | NULL |
| 9 | 3464.000000 |
| 10 | 20046.000000 |
| 11 | NULL |
| 12 | 7449.000000 |
| 13 | 265163.000000 |
| 14 | 24210.000000 |
| 15 | 68848.000000 |
| 16 | 31702.000000 |
| 17 | 2500.000000 |
| 18 | NULL |
| 19 | 2914.000000 |
| 20 | 238406.000000 |
| 21 | 15642.000000 |
| 22 | 2514.000000 |
| 23 | 46521.000000 |
| 24 | 34093.000000 |
| 25 | 899081.000000 |
| 26 | 204085.000000 |
| 27 | 316341.000000 |
| 28 | 48826.000000 |
| 29 | 2657.000000 |
| 30 | 440401.000000 |
+------+---------------+
As you can see, the expected result still shows the days Im not expecting any value.
Can Anybody notice anything immediately wrong with my query... Any help and tips would be greatly appreciated.
I'm using SQL server 2008
Thanks!
Mike
Change your where clause to part of the join, and display the day value from #tempdays, not the data table
SELECT
#TempDays.days ExpectedDate, SUM(a.budg_do1_total) ExpectedAmount
FROM CRM.dbo.budget a
RIGHT JOIN #TempDays on
DATEPART(DD, CONVERT(DATE, a.budg_tempDODate1, 103)) = #TempDays.days
AND DATEPART(MONTH, a.budg_tempDODate1) = DATEPART(MONTH, GetDate())
AND DATEPART(YEAR, a.budg_tempDODate1) = DATEPART(YEAR, GetDate())
GROUP BY #TempDays.days
The problem is your Where Clause -
WHERE DATEPART(MONTH, a.budg_tempDODate1) = DATEPART(MONTH, GetDate()) AND DATEPART(YEAR, a.budg_tempDODate1) = DATEPART(YEAR, GetDate())
The where clause and the group by clauses are applied on the table with less data.
So, the result set is getting confined to the table with less data ignoring the join clauses.
I have rewritten this and tested it.
declare #budget table
(
budg_tempDODate1 datetime,
budg_do1_total float
)
insert into #budget(budg_tempDODate1, budg_do1_total)
select '2014-06-01', 25
union select '2014-06-01', 23
union select '2014-06-02', 23
union select '2014-06-02', 23
union select '2014-06-02', 23
union select '2014-06-03', 23
union select '2014-06-04', 23
union select '2014-06-05', 23
union select '2014-06-05', 23
union select '2014-06-05', 23
union select '2014-06-06', 23
union select '2014-06-07', 23
union select '2014-06-08', 23
union select '2014-06-09', 23
union select '2014-06-10', 23
DECLARE #ExpectedDateSample VARCHAR(10)
DECLARE #Date datetime
DECLARE #DaysInMonth INT
DECLARE #i INT
--GIVE VALUES
SET #ExpectedDateSample = SUBSTRING(CONVERT(VARCHAR, DATEADD(MONTH, +0, GETDATE()), 112),5,2)+'/'+CONVERT(VARCHAR(4), DATEPART(YEAR, GETDATE()))
SET #Date = Getdate()
SELECT #DaysInMonth = datepart(dd,dateadd(dd,-1,dateadd(mm,1,cast(cast(year(#Date) as varchar)+'-'+cast(month(#Date) as varchar)+'-01' as datetime))))
SET #i = 1
--MAKE TEMP TABLE
CREATE TABLE #TempDays
(
[days] int
)
WHILE #i <= #DaysInMonth
BEGIN
INSERT INTO #TempDays
VALUES(#i)
SET #i = #i + 1
END
--select * from #TempDays
SELECT
td.days as ExpectedDate,
SUM(isnull(a.budg_do1_total, 0)) ExpectedAmount
FROM
--CRM.dbo.budget a
#TempDays td
left outer JOIN #budget a on
DATEPART(MONTH, a.budg_tempDODate1) = DATEPART(MONTH, GetDate())
AND DATEPART(YEAR, a.budg_tempDODate1) = DATEPART(YEAR, GetDate())
AND DATEPART(DD, CONVERT(DATE, a.budg_tempDODate1, 103)) = td.days
GROUP BY
--a.budg_tempDODate1
td.days
--DROP TABLE TO ALLOW CREATION AGAIN
DROP TABLE #TempDays
Here is the sql fiddle

converting varchar(50) to datetime and populating rest of the table

I am working on a data warehouse and in the data model theres a dimension table dim.date which needs to be populated from excel sheet.
this is table structure
CREATE TABLE timeme
(
datekey INT IDENTITY NOT NULL,
fulldate VARCHAR(50),
englishdayname VARCHAR(20),
englishmonthname VARCHAR(20),
daynumberofthemonth INT,
daynumbarofyear INT,
weeknumberofyear INT,
calenderquater INT,
calenderyear INT
)
INSERT INTO timeme
(fulldate)
VALUES ('03-13-2013-02:41:38'),
('03-13-2013-02:39:50'),
('03-13-2013-02:53:15'),
('03-13-2013-02:59:47')
This is what i tried
-- select * from timeme
--update timeme set fulldate= cast(left(fulldate, 10)+ ' ' +replace(substring(fulldate, 12, 12), '.', ':') as datetime)-- but it should be 2013-03-13 02:41:38.000
UPDATE timeme
SET englishdayname = Datename(dw, fulldate),
englishmonthname = Datename(mm, fulldate),
daynumberofthemonth = Datepart(d, fulldate)
Update timeme
SET daynumbarofyear -- this i didnt get rest how to populate
can somebody help me populating entire table
Are you looking for this?
UPDATE timeme
SET fulldate = SUBSTRING(fulldate, 7, 4) + '-' + LEFT(fulldate, 5) + ' ' + RIGHT(fulldate, 8);
UPDATE timeme
SET englishdayname = DATENAME(dw, CAST(fulldate AS DATE)),
englishmonthname = DATENAME(mm, CAST(fulldate AS DATE)),
daynumberofthemonth = DATEPART(dd, CAST(fulldate AS DATE)),
daynumbarofyear = DATEPART(dy, CAST(fulldate AS DATE)),
weeknumberofyear = DATEPART(ww, CAST(fulldate AS DATE)), -- or isoww instead of ww
calenderquater = DATEPART(qq, CAST(fulldate AS DATE)),
calenderyear = DATEPART(yy, CAST(fulldate AS DATE));
Outcome:
| DATEKEY | FULLDATE | ENGLISHDAYNAME | ENGLISHMONTHNAME | DAYNUMBEROFTHEMONTH | DAYNUMBAROFYEAR | WEEKNUMBEROFYEAR | CALENDERQUATER | CALENDERYEAR |
|---------|---------------------|----------------|------------------|---------------------|-----------------|------------------|----------------|--------------|
| 1 | 2013-03-13 02:41:38 | Wednesday | March | 13 | 72 | 11 | 1 | 2013 |
| 2 | 2013-03-13 02:39:50 | Wednesday | March | 13 | 72 | 11 | 1 | 2013 |
| 3 | 2013-03-13 02:53:15 | Wednesday | March | 13 | 72 | 11 | 1 | 2013 |
| 4 | 2013-03-13 02:59:47 | Wednesday | March | 13 | 72 | 11 | 1 | 2013 |
Here is SQLFiddle demo