葉子函式分享二十二 根據日期返回星座

2021-08-25 18:25:04 字數 2524 閱讀 3175

go

--建立函式(csdn fredrickhu(小f)提供)

create

function udf_getstar (@ datetime

)returns

varchar

(100)

-- 返回日期所屬星座

begin

return

(select

max(star)

from

(select

'魔羯座'

as star,1 as [month],1 as [day]

union

allselect

'水瓶座'

,1,20

union

allselect

'雙魚座'

,2,19

union

allselect

'牡羊座'

,3,21

union

allselect

'金牛座'

,4,20

union

allselect

'雙子座'

,5,21

union

allselect

'巨蟹座'

,6,22

union

allselect

'獅子座'

,7,23

union

allselect

'**座'

,8,23

union

allselect

'天秤座'

,9,23

union

allselect

'天蠍座'

,10,24

union

allselect

'射手座'

,11,22

union

allselect

'魔羯座'

,12,22

) stars

where

dateadd

(month

,[month] - 1,

dateadd

(year

,year

(@) -

year

(0),0))

+ [day] - 1 =

(select

max(

dateadd

(month

,[month] - 1,

dateadd

(year

,year

(@) -

year

(0),0))

+ [day] - 1)

from

(select

'魔羯座'

as star,1 as [month],1 as [day]

union

allselect

'水瓶座'

,1,20

union

allselect

'雙魚座'

,2,19

union

allselect

'牡羊座'

,3,21

union

allselect

'金牛座'

,4,20

union

allselect

'雙子座'

,5,21

union

allselect

'巨蟹座'

,6,22

union

allselect

'獅子座'

,7,23

union

allselect

'**座'

,8,23

union

allselect

'天秤座'

,9,23

union

allselect

'天蠍座'

,10,24

union

allselect

'射手座'

,11,22

union

allselect

'魔羯座'

,12,22

) stars

where @ >=

dateadd

(month

,[month] - 1,

dateadd

(year

,year

(@) -

year

(0),0))

+ [day] - 1 )

)end

--測試示例

select

dbo.udf_getstar(

'2010-05-04'

)select

dbo.udf_getstar(

'2009-01-04'

)select

dbo.udf_getstar(

'2007-12-04')

--執行結果 /*

金牛座魔羯座

射手座 */

葉子函式分享二十七 根據日期得到星期的函式

最直接的方式就是用case when判斷,這裡用表變數來處理的 go 建立函式 create function dbo m getweek date nvarchar 2000 returns varchar 2000 asbegin declare weekday nvarchar 300 dec...

葉子函式分享二十七 根據日期得到星期的函式

最直接的方式就是用 case when 判斷,這裡用表變數來處理的 go 建立函式 create function dbo m getweek date nvarchar 2000 returns varchar 2000 as begin declare weekday nvarchar 300 ...

葉子函式分享四 綜合模糊查詢

建立函式 create function dbo m fuzzyquery v1 str nvarchar 2000 returns nvarchar 2000 as begin declare count int i int declare newchar nvarchar 200 nn nvar...