我的格式为日期的列
year / number_of_week_in_year / number_of_day_of_the_week,例如:
2015015 = 01.01.2015
如何编写将此日期转换为RRRRmmdd
格式的查询?
答案 0 :(得分:2)
这是一个未经测试的答案,因为我无法访问SQL Server(当前在我的手机上)。
简单的部分是获得年度的DATE值...
DATEADD(year, (input / 1000) - 1900), 0)
然后你需要添加一定天数...
- 每周7天(不包括第1周)
- 一周中每天的1天
((input / 10) % 100 - 1) * 7
+ input % 10
然后根据当年开始的那一天扣除若干天。
DATEPART(weekday, <your year as a date>)
这似乎给了......
DATEADD(
day,
((input / 10) % 100 - 1) * 7
+ input % 10
- DATEPART(weekday, DATEADD(year, (input / 1000) - 1900, 0)),
DATEADD(year, (input / 1000) - 1900, 0)
)
使用你的例子......
DATEADD(
day,
((2015015 / 10) % 100 - 1) * 7
+ 2015015 % 10
- DATEPART(weekday, DATEADD(year, (2015015 / 1000) - 1900, 0)),
DATEADD(year, (2015015 / 1000) - 1900), 0)
)
=&GT;
DATEADD(
day,
(01 - 1) * 7
+ 5
- DATEPART(weekday, DATEADD(year, 2015 - 1900, 0)),
DATEADD(year, 2015 - 1900, 0)
)
=&GT;
DATEADD(
day,
0 * 7
+ 5
- DATEPART(weekday, '2015-01-01'),
'2015-01-01'
)
=&GT;
DATEADD(
day,
0 * 7
+ 5
- 5,
'2015-01-01'
)
=&GT;
'2015-01-01'
答案 1 :(得分:2)
这是一个简单的解决方案,我把它放在一起,可能不是最聪明的方法,但希望有道理:
DECLARE @inDate CHAR(7),
@inYear CHAR(4),
@inWeek INT,
@inDay INT,
@OutDate DATETIME;
SET @inDate = '2015015';
SET @inYear = SUBSTRING(@inDate, 0, 5);
SET @inWeek = CAST(SUBSTRING(@inDate, 5, 2) AS INT) - 1 -- Reduce by 1 because it will be added to start of year
SET @inDay = CAST(SUBSTRING(@inDate, 7, 1) AS INT)
SET @OutDate = CAST(@inYear + '-01-01' AS DATETIME)
SET @OutDate = DATEADD(dd, -DATEPART(weekday, @OutDate) + @inDay, @OutDate)
SET @OutDate = DATEADD(ww, @inWeek, @OutDate)
PRINT @OutDate -- Gives Jan 1 2015
答案 2 :(得分:1)
您可以创建简单的功能来执行此操作:
CREATE FUNCTION dbo.GetDate ( @Date AS NVARCHAR(7) )
RETURNS DATE
AS
BEGIN
DECLARE @WeekDayOfYearStart INT ,
@WeekNum INT ,
@DayInWeek INT ,
@DateOut DATE;
SELECT @WeekNum = CONVERT(INT, RIGHT(LEFT(@Date, 6), 2));
SELECT @DayInWeek = CONVERT(INT, RIGHT(@Date, LEN(@Date) - 6));
SELECT @WeekDayOfYearStart = DATEPART(WEEKDAY,
LEFT(@Date, 4) + '0101');
SELECT @DateOut = DATEADD(DAY,
( @WeekNum - 1 ) * 7 + @DayInWeek
- @WeekDayOfYearStart,
LEFT(@Date, 4) + '0101');
RETURN @DateOut;
END;
GO
答案 3 :(得分:0)
如果我理解正确,输入是varchar(10)。你可以试试这个
<?php
if(isset($_REQUEST['submit']))
{
$to = "usman_86@rocketmail.com";
$subject = "Find Dealer @ flowsleeve";
$headers = "From: Find Dealer ";
$headers .= "Reply-To: ". strip_tags("usman_86@rocketmail.com") . "\r\n";
$headers = 'MIME-Version: 1.0' . PHP_EOL;
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$messsage = "
Name : ".$_REQUEST['first_name']."
Email : ".$_REQUEST['email']."
Event Type : ".$_REQUEST['event_type']."
Event location : ".$_REQUEST['event_location']."
Last Name : ".$_REQUEST['last_name']."
Home Phone : ".$_REQUEST['home_phone']."
Event Date : ".$_REQUEST['event_date']."
Referrer : ".$_REQUEST['referrer']."
Message : ".$_REQUEST['message']."";
mail($to,$subject,$message,$headers);