Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

Bagaimana cara menemukan nomor minggu tanggal menurut DATEFIRST

Setel hari pertama Anda ke Tuesday

SET DATEFIRST 2

Jalankan kueri berikut

DECLARE @FROMDATE DATE='31-Dec-2014'

SELECT CAST(UPPER(DATENAME(YEAR, @FROMDATE)) AS VARCHAR(20))
+'_'+CAST(UPPER(DATENAME(MONTH, @FROMDATE)) AS VARCHAR(20))
+'_WEEK_NO_'+REPLACE(CAST((DAY(@FROMDATE) 
+ (DATEPART(DW, DATEADD (MONTH, DATEDIFF (MONTH, 0, @FROMDATE), 0))-1) -1)/7  AS VARCHAR(10)),'0','1')

PERBARUI

SET DATEFIRST 2

DECLARE @FROMDATE DATE='12-JAN-2015'
DECLARE @ALLDATE DATE=DATEADD(month, DATEDIFF(month, 0, @FROMDATE), 0)
DECLARE @FIRSTDATE DATE


; WITH  CTE as
 (
     SELECT 1 RNO,CAST(@ALLDATE AS DATE) as DATES 
     UNION ALL
     SELECT RNO+1, DATEADD(DAY,1,DATES )
     FROM    CTE
     WHERE   DATES < DATEADD(MONTH,1,@ALLDATE)
 )
 SELECT TOP 1 @FIRSTDATE =   DATES 
 FROM    CTE 
 WHERE DATEPART(W,DATES)=1


SELECT CAST(UPPER(DATENAME(YEAR, @FROMDATE)) AS VARCHAR(20))
+'_'+CAST(UPPER(DATENAME(MONTH, @FROMDATE)) AS VARCHAR(20))
+'_WEEK_NO_'+CAST((DATEDIFF(DAY,@FIRSTDATE,@FROMDATE)/7)+1  AS VARCHAR(10))


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Bagaimana cara menunggu selama 2 detik?

  2. Konversi dan pembulatan implisit

  3. Fungsi agregat sql untuk mendapatkan daftar

  4. Microsoft SQL Server 2008 Management Studio - Masalah Sambungan/Server/Instance

  5. SQL Server 2014 :Enkripsi cadangan asli