[Solved] Get first day of week T-SQL

DECLARE @YEAR int = 2020; DECLARE @WEEKSTOADD int = 6; SET DATEFIRST 1; SELECT DATEADD(day, 1 – DATEPART(dw,DATEADD(week,@WEEKSTOADD,cast(cast(@YEAR as varchar(4)) + ‘0101’ as date))), DATEADD(week,@WEEKSTOADD,cast(cast(@YEAR as varchar(4)) + ‘0101’ as date))) 3 solved Get first day of week T-SQL

[Solved] Trouble writing a select query [closed]

It looks like vb code if you want c# code then use following code cmd = new SqlCommand(“select name from wq where id='” + TextBox1.Text + “‘”, con); con.Open(); dr = cmd.ExecuteReader(); dr.Read(); TextBox2.Text = dr[0].ToString(); dr.Close(); con.Close(); 7 solved Trouble writing a select query [closed]

[Solved] If I have two columns, how can I select all distinct values of columnA where columnB is never a specific value given that columnA is the same value?

If I have two columns, how can I select all distinct values of columnA where columnB is never a specific value given that columnA is the same value? solved If I have two columns, how can I select all distinct values of columnA where columnB is never a specific value given that columnA is the … Read more

[Solved] Break down long multi day periods of time into several single day periods

The method I have used here is a Tally Table to create extra rows. I JOIN onto the tally table where the number of hours / 24 (integer maths is useful) is greater than the tally number, and then can use that to calculate the hours. WITH YourTable AS( SELECT * FROM (VALUES(1,CONVERT(date,’2018/01/24′,111),4 ), (2,CONVERT(date,’2018/03/21′,111),40), … Read more

[Solved] SQL query for display one field only once which having multiple record

Using CASE Condition and Row_number we can achieve the above Output It’s Purely based on your sample Data DECLARE @Table1 TABLE (projName varchar(1), percentage int) ; INSERT INTO @Table1 (projName, percentage) VALUES (‘A’, 10), (‘A’, 25), (‘B’, 20), (‘B’, 30) ; Select CASE WHEN RN = 1 THEN projName ELSE NULL END projName, percentage from … Read more

[Solved] SQL group data based on a column and pivot rows to (unknown) columns [duplicate]

A straight-up PIVOT in concert row_number() with should do the trick Select * From ( Select ID = fd_Interpretation ,Item = concat(‘Word’,row_number() over (partition by fd_Interpretation order by fd_Word) ) ,Value = fd_Word From YourTable ) src Pivot ( max( Value ) for Item in ([Word1],[Word2],[Word3],[Word4],[Word5],[Word6],[Word7],[Word8]) ) pvt 2 solved SQL group data based on … Read more

[Solved] How do I convert a SQL server timestamp to an epoch timestamp? [duplicate]

You can do it using DATEDIFF function like below : select DATEDIFF(s, ‘1970-01-01 00:00:00’, ‘2017-11-20 23:12:02.000’) as EpochTimeStamp Output : EpochTimeStamp ————– 1511219522 You know already how can we get back the original date : SELECT DATEADD(ss, 1511219522, ‘19700101’) as OriginalDate OriginalDate ———————– 2017-11-20 23:12:02.000 solved How do I convert a SQL server timestamp to … Read more

[Solved] SQl CASE Statement

— ok, Was a date format issue. i used a European Date Format(dd-mm-yyyy) in my query but the system i use takes USA date Format(mm-dd-yyyy) so my query failed. ** Solution ** Select Cast(Case When EventDate <=’01/01/2016′ then ‘PreSeason’ When EventDate >=’12/31/2016′ then ‘PostSeason’ Else ‘Season’ End As Varchar(25)) as Season, EventName From tblEvent Thanks … Read more