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

Diberikan nomor tahun, bulan, hari dan minggu bagaimana menemukan tanggal? (Berbasis Sql Server 2005 Set)

coba ini:

--given info
DECLARE @Year        varchar(4) --= 2010  
DECLARE @MonthName   varchar(10)  --= Feb  
DECLARE @WeekDayday  varchar(10) --= Wed   
DECLARE @WeekNumber  int         --=4
SET @Year        ='2010'
SET @MonthName   ='Feb'  
SET @WeekDayday  ='Wed'   
SET @WeekNumber  =4


--used to solve
DECLARE @StartDate datetime
       ,@EndDate   datetime
       ,@FirstWeek int

SET @StartDate='01 '[email protected]+' '[email protected]
SET @[email protected]+38
SET @FirstWeek=DATENAME(week,@StartDate)-1

;with AllDates AS
(
    SELECT @StartDate AS DateOf, DATENAME(week,@StartDate)[email protected] AS WeekOf, DATENAME(weekday,@StartDate) AS WeekDayOf
    UNION ALL
    SELECT DateOf+1, DATENAME(week,DateOf+1)[email protected] AS WeekOf, DATENAME(weekday,DateOf+1) AS WeekDayOf
        FROM AllDates
    WHERE DateOf<@EndDate
)
SELECT
    DateOf ,WeekOf ,WeekDayOf
    FROM AllDates
    WHERE [email protected] AND WeekDayOf LIKE @WeekDayday+'%'
    ORDER BY DateOf

KELUARAN

DateOf                  WeekOf      WeekDayOf
----------------------- ----------- ------------------------------
2010-02-24 00:00:00.000 4           Wednesday

(1 row(s) affected)



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. 4 Cara untuk Mengetahui Kolom Apa yang Akan Dikembalikan oleh Prosedur Tersimpan di SQL Server

  2. DENGAN CHECK ADD CONSTRAINT diikuti oleh CHECK CONSTRAINT vs. ADD CONSTRAINT

  3. Metode Ekspor dan Impor Tabel Database SQL Server

  4. Ganti beberapa karakter dari string tanpa menggunakan fungsi ganti bersarang

  5. .NET SQL insert lambat dibandingkan dengan JDBC? (JDBC 4x lebih cepat)