我将周末保存在表中为0,6(周日,周六)和工作日如1,4,6(周一,周四,周六)。在选择时我需要用适当的工作日替换数字。例如。,
N-1=5
返回
SELECT WEEKEND_DAYS FROM TABLE_1
但我需要o / p为
WEEKEND_DAYS
1,4,6
如何实现这一目标?提前谢谢......
答案 0 :(得分:2)
使用丑陋REPLACE()
可以完成工作
SELECT
WEEKEND_DAYS,
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(WEEKEND_DAYS
, 1, 'Monday')
, 2, 'Tuesday')
, 3, 'Wensday')
, 4, 'Thursday')
, 5, 'Friday')
, 6, 'Saturday')
, 0, 'Sunday') AS WEEKEND_DAYS_NAME
FROM (VALUES('1,4,6')) TABLE_1(WEEKEND_DAYS)
答案 1 :(得分:0)
你应该尝试以下,
它会给你 6 =星期天 0 =星期一
DECLARE @daynum VARCHAR(256)
SET @daynum = '1,5,3,2'
IF @daynum = '' RETURN
DECLARE @XML xml
SET @XML = '<root><csv>'+replace(@daynum,',','</csv><csv>')+
'</csv></root>'
Select STUFF(( SELECT ',' + dayname
FROM
(
Select DATENAME(DW,CAST(ListMember AS INT)) as dayname from
(
SELECT rtrim(ltrim(replace(Word.value('.','nvarchar(max)'),char(10),'')))
AS ListMember
FROM @XML.nodes('/root/csv') AS WordList(Word)
)T
) T2
ORDER BY dayname
FOR XML PATH('')
), 1, 1, '')
您还可以创建拆分功能,然后根据需要调用它。
CREATE FUNCTION dbo.fnParseStringXML (
@StringList NVARCHAR(MAX),
@Delimiter CHAR(1))
RETURNS @TableList TABLE(ID int identity(1,1) PRIMARY KEY,
[Substring] NVARCHAR(max))
BEGIN
IF @StringList = '' RETURN
DECLARE @XML xml
SET @XML = '<root><csv>'+replace(@StringList,@Delimiter,'</csv><csv>')+
'</csv></root>'
INSERT @TableList
SELECT rtrim(ltrim(replace(Word.value('.','nvarchar(max)'),char(10),'')))
AS ListMember
FROM @XML.nodes('/root/csv') AS WordList(Word)
RETURN
END -- FUNCTION
GO
你应该这样称呼它。
Select DATENAME(DW,CAST(Substring AS INT)) from
(
SELECT Substring FROM dbo.fnParseStringXML (@ProductCode, ',')
)T
答案 2 :(得分:0)
它对我有用。 希望能帮助到你。 使用1-7索引日期
<?php
$result="";
// include("database.php");
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("db_name") or die(mysql_error());
$query= mysql_fetch_row(mysql_query("SELECT weekend_days from weekend where weekend_days= '1,4,6'"));
if (!$query) {
echo "No weekend";
}
else {
$new= explode(",", $query[0]);
for ($i=0; $i < sizeof($new) ; $i++) {
if($new[$i]==1){
$result.= " Sunday";
}
elseif($new[$i]==2) {
$result.=", Monday";
}
elseif($new[$i]==3){
$result.= ", Tuesday";
}
elseif($new[$i]==4){
$result.= ", Wednesday";
}
elseif($new[$i]==5){
$result.=", Thursday";
}
elseif($new[$i]==6){
$result.= ", Friday";
}
elseif($new[$i]==7){
$result.= ", Saturday";
}
}
echo "WEEKEND_DAYS " . $result;
}
?>
答案 3 :(得分:0)
试试这个。
SELECT
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(WEEKEND_DAYS
, 1, 'Monday')
, 2, 'Tuesday')
, 3, 'Wednesday')
, 4, 'Thursday')
, 5, 'Friday')
, 6, 'Saturday')
, 0, 'Sunday') AS WEEKEND_DAYS_NAME
FROM TABLE_1