Click here to Skip to main content
15,919,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have app for workers work time...

I need if is Saturday od Sunday to work with my code

Without
CASE WHEN WEEKDAY(a.vrijemeodjave) in (1,7) then 0 else 


CODE WORKING FINE

Statement is Microsoft SQL 2016

What I have tried:

SELECT a.id, a.redni_broj , a.radnik, isnull(convert(varchar(20), a.vrijemeodjave, 113), '') as vrijemeodjave, convert(varchar(20), b.vrijemeprijave, 113) as vrijemeprijave, 
CASE WHEN WEEKDAY(b.vrijemeprijave) in (1,7) then 0 else (CASE WHEN (DATEDIFF(SECOND, b.vrijemeprijave, a.vrijemeodjave) / 3600) > 8 THEN 8 ELSE(DATEDIFF(SECOND, b.vrijemeprijave, a.vrijemeodjave) / 3600) END) end    AS radni_sati, 
CASE WHEN WEEKDAY(a.vrijemeodjave) in (1,7) then 0 else (CASE WHEN(DATEDIFF(SECOND, b.vrijemeprijave, a.vrijemeodjave) / 3600) > 8  THEN(DATEDIFF(SECOND, b.vrijemeprijave, a.vrijemeodjave) / 3600) - 8  ELSE NULL END) end    AS prekovremeni 
FROM(SELECT id, redni_broj, radnik, vrijemeodjave, ROW_Number() OVER(Partition By id ORDER BY vrijemeprijave) as RowNum 
FROM dbo.prijava_radnika) a INNER JOIN(SELECT id, redni_broj, radnik, vrijemeprijave, (ROW_Number() OVER(Partition By id ORDER BY vrijemeprijave) - 1) as RowNumMinusOne FROM dbo.prijava_radnika) b ON a.id = b.id where 1 = 1 
Posted
Updated 17-Apr-18 7:55am
Comments
Goran Bibic 17-Apr-18 13:18pm    
1 is Sunday, 7 is Saturday...I hope so..

Not sure if I understand the question correctly but you can use DATEPART to exctract the weekday number.

For example
SQL
SELECT DATEPART(weekday, GETDATE())
 
Share this answer
 
Comments
Goran Bibic 17-Apr-18 13:40pm    
I need to find Saturday and Sunday
Wendelius 17-Apr-18 13:54pm    
The value depends on the DATEFIRST setting you have. You can query it as follows:

SELECT @@DATEFIRST

Depending on the returned value Saturday and Sunday are different values as described in DATEPART (Transact-SQL) | Microsoft Docs[^]

For example I have DATEFIRST as 7 and this weeks Saturday and Sunday for me are 7 and 1 respectively.
Goran Bibic 17-Apr-18 14:05pm    
Its work...thank you...I am accept answer
Wendelius 17-Apr-18 14:08pm    
You're welcome :)
In addition to answer 1, maybe this will make things clearer:
SQL
SELECT
  DATENAME(weekday, a.vrijemeodjave),
  DATEPART(weekday, a.vrijemeodjave)
FROM dbo.prijava_radnika

Also see SQL Fiddle here: SQL Fiddle[^]
 
Share this answer
 
Comments
Goran Bibic 17-Apr-18 14:05pm    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900