tsql

SQL Setting Variable for Current Year Month by Combining two VarChar fields using case statement

ぃ、小莉子 提交于 2020-04-18 05:18:00
问题 I am trying to declare a variable for the current year and month based on a field called YEARMO. It's a 6 character varchar field with the first 4 characters representing the year and the next 2 characters representing the month (ex. February 2018 as 201802). I need the month variable to have a 0 in front of it if is a month that only has one digit (any month before October). Current code returns '20182' and would like it to return '201802'. declare @current_month varchar(6) set @current

How to update SQL Server Table after query sort by datetime

帅比萌擦擦* 提交于 2020-04-18 03:46:40
问题 I'm using a SQL Server database and I have a datetime column, datatype is datetime , now I already know how to sort table by datetime: SELECT * FROM [database].[dbo].[data] ORDER BY [datetime]; Result: datetime 2020-03-18 09:18:00 2020-03-18 09:19:00 2020-03-18 09:20:00 2020-03-18 09:21:00 ............. ............. ............. My question is after the above step that query sort it by datetime, how can I update and save the table? Because I want to keep or the data sort by datetime. Can

The maximum recursion 100 has been exhausted

余生长醉 提交于 2020-04-17 19:09:56
问题 When I run this query I am getting error when I increase the date to be above months with calender_cte as ( select convert(date, '2019-01-01') as startdate, convert(date, '2019-12-31') as enddate union all select dateadd(day, 1, startdate), enddate from calender_cte cc where startdate < enddate ) SELECT DATEADD (week, datediff(week, 0, cc.StartDate), -1) as 'WeekOf', DATEADD (week, datediff(week, 0, cc.StartDate), +5) as 'to' --ISNULL(DATEPART(wk, Inter.StartDate), 0) as 'WeekNumber' FROM

The maximum recursion 100 has been exhausted

只愿长相守 提交于 2020-04-17 19:03:34
问题 When I run this query I am getting error when I increase the date to be above months with calender_cte as ( select convert(date, '2019-01-01') as startdate, convert(date, '2019-12-31') as enddate union all select dateadd(day, 1, startdate), enddate from calender_cte cc where startdate < enddate ) SELECT DATEADD (week, datediff(week, 0, cc.StartDate), -1) as 'WeekOf', DATEADD (week, datediff(week, 0, cc.StartDate), +5) as 'to' --ISNULL(DATEPART(wk, Inter.StartDate), 0) as 'WeekNumber' FROM

The maximum recursion 100 has been exhausted

走远了吗. 提交于 2020-04-17 19:03:29
问题 When I run this query I am getting error when I increase the date to be above months with calender_cte as ( select convert(date, '2019-01-01') as startdate, convert(date, '2019-12-31') as enddate union all select dateadd(day, 1, startdate), enddate from calender_cte cc where startdate < enddate ) SELECT DATEADD (week, datediff(week, 0, cc.StartDate), -1) as 'WeekOf', DATEADD (week, datediff(week, 0, cc.StartDate), +5) as 'to' --ISNULL(DATEPART(wk, Inter.StartDate), 0) as 'WeekNumber' FROM

Get the MotherTongue and the Fluent language of an Employee

廉价感情. 提交于 2020-04-17 19:01:36
问题 I have the following query which gets as result the language spoken by an employee and his corresponding level : SELECT E.EmployeeId ,ISNULL(L.ID ,0) AS LanguageId ,L.Label AS Language, ll.Label AS LanguageLevel FROM Employee e LEFT JOIN AF_AdminFile aaf ON e.AdminFileId = aaf.AdminFileId LEFT JOIN AF_Language al ON aaf.AdminFileId = al.AdminFileId LEFT JOIN Language l ON al.LanguageId = l.ID LEFT JOIN LanguageLevel ll ON al.LanguageLevelId = ll.LanguageLevelId ORDER BY e.EmployeeId The

Get the MotherTongue and the Fluent language of an Employee

自古美人都是妖i 提交于 2020-04-17 18:59:20
问题 I have the following query which gets as result the language spoken by an employee and his corresponding level : SELECT E.EmployeeId ,ISNULL(L.ID ,0) AS LanguageId ,L.Label AS Language, ll.Label AS LanguageLevel FROM Employee e LEFT JOIN AF_AdminFile aaf ON e.AdminFileId = aaf.AdminFileId LEFT JOIN AF_Language al ON aaf.AdminFileId = al.AdminFileId LEFT JOIN Language l ON al.LanguageId = l.ID LEFT JOIN LanguageLevel ll ON al.LanguageLevelId = ll.LanguageLevelId ORDER BY e.EmployeeId The

How to pivot two columns in SQL Server?

烈酒焚心 提交于 2020-04-16 05:49:11
问题 I have the following table UserName UserId ----- ---- Bob 445 Bob 450 Rachel 512 Rachel 520 Rachel 570 Simon 771 Simon 760 and I am trying to pivot it so that a new column is created for each username, with UserID's listed per UserName Bob Rachel Simon 445 512 771 450 520 760 570 回答1: Just in case you were looking for a dynamic pivot Example Declare @SQL varchar(max) = ' Select * From ( Select * ,RN = row_number() over (partition by username order by UserId) from #YourTable ) A Pivot (max

Convert all XML Special Characters back to Regular Characters (Within SQL)

只谈情不闲聊 提交于 2020-04-16 02:28:18
问题 How do I convert all Special Characters in XML to the ASCII Value? For Example DECLARE @xml XML = (SELECT 'abc & xyz><' FOR XML PATH('')) SELECT @xml --@xml is now 'abc & xyz ><' I wish to convert back to the ASCII varchar value (i.e. 'abc & xyz><'). The only way I have found is to manually replace all special XML Characters i.e. SELECT REPLACE(REPLACE(REPLACE(CAST(@xml AS VARCHAR(MAX)),'&','&'),'>','>'),'<','<'); --RETURNS 'abc & xyz><' However, this is a very inelegant solution, and does

SQL Query To Get Multiple Max Values From Multiple Columns

你离开我真会死。 提交于 2020-04-16 02:16:43
问题 I am trying to figure out how to pull multiple max values from multiple columns. Here is some sample data: DATE | A | B | C 4/4/2011 | 64.4 | 62.1 | 33.3 4/5/2011 | 34.6 | 33.5 | 32.3 4/6/2011 | 33.1 | 49.4 | 32.1 4/7/2011 | 55.2 | 32.8 | 33.5 4/8/2011 | 31.2 | 50.1 | 30.4 4/9/2011 | 31.7 | 31.1 | 30.4 I want the top 5 so: 4/4/2011 | 64.4 4/4/2011 | 62.1 4/7/2011 | 55.2 4/8/2011 | 50.1 4/6/2011 | 49.4 Thanks 回答1: How about: SELECT TOP 5 Date, Val FROM (SELECT Date, A as Val FROM T UNION ALL