I have a column adetdate which has date in the format '2015-05-01 00:00:00.000'
is it possible to write a case statement like
case adetdate
adetdate between '2015-05-01 00:00:00.000' and '2015-05-01 23:59:59.999' then print 'MAY 1st'
adetdate between '2015-05-02 00:00:00.000' and '2015-05-02 23:59:59.999' print 'MAY 2nd'
If correctly understood. You could convert your DATETIME column to DATE in this case time will be ignored. Something like:
PRINT CASE CAST(adetdate AS DATE)
WHEN '2015-05-01' THEN 'MAY 1'
WHEN '2015-05-02' THEN 'MAY 2'
--........
END AS Something
Another, more dynamic way is to use CONVERT in following:
declare #datetime datetime = '2015-05-01 00:00:00.000'
select upper(convert(varchar(6), #datetime, 7))
OUTPUT
Will be in following format:
MAY 01
Maybe a generic approach like this:
SET LANGUAGE English
DECLARE #d DATETIME=GETDATE();
SELECT FORMAT(#d,'MMM d') + CASE DAY(#d)
WHEN 1 THEN 'st'
WHEN 2 THEN 'nd'
WHEN 3 THEN 'rd'
ELSE 'th' END
If you are using a SQL Server <2012 you might find this helpful:
DECLARE #d DATETIME=GETDATE();
SELECT UPPER(DATENAME(MONTH,#d)) + ' ' + CAST(DAY(#d) AS VARCHAR(2))
+ CASE DAY(#d)
WHEN 1 THEN 'st'
WHEN 2 THEN 'nd'
WHEN 3 THEN 'rd'
ELSE 'th' END
Related
This seems like it should be fairly simple and straight forward but I haven't yet found the right combination. I have a column called last_assess_yr that is an integer. I am trying to find all rows from my_table where '01-01' + 'year' < current_date and give them a value in a new column. I have the following:
SELECT last_assess_yr,
CASE
WHEN format('01-01-%s'::text, last_assess_yr)::timestamp without time
zone < current_date
THEN YES
ELSE NO
END AS assess_value
FROM my__table
but, the results are not correct
You don't actually need to convert the integer into a timestamp, you could just compare the extracted year to the integer.
select
case when last_assess_yr < extract('year' from current_date)::int
then 'YES' else 'NO'
end
However for reference the following will work:
select
case when format('01-01-%s'::text, 2017)::timestamp < current_date
then 'YES' else 'NO'
end
i.e. you do not need to remove time (of day) when you convert a string of '01-01-2017' to a timestamp.
and: I assume YES and NO also need to be treated as literals: 'YES' and 'NO'
In case someone else is looking, this worked for me:
WHEN last_assess_yr > 0 AND format('%s-05-05'::text, w.last_assess_yr)::timestamp <
current_date THEN 'yes'
ELSE 'no'
END
I'm trying to concatenate a column's date to a fixed time of the day and then CAST the whole thing as DATETIME.
The fixed time is 5:30am.
The date column I'm using needs to be adjusted as it shows the end date/time of when something ran; I want to use the start date/time.
The start date/time time is not available as its own column, but I have another column that has the duration the process took in seconds, so I can use DATEADD to roll the end date/time back to the start date/time.
Here's the full statement:
CAST(CONVERT(VARCHAR(10), DATEADD(ss,-ConfTask.[LastExecutedDuration], ConfTask.[LastExecutedDate]), 103) + ' ' + '05:30' as DATETIME)
Here's the error message I'm receiving:
The conversion of a varchar data type to a datetime data type resulted
in an out-of-range value.
I've tried testing these statements to investigate the issue, but they all run OK on their own:
CAST(CONVERT(VARCHAR(10), GETDATE(), 103) + ' ' + '05:30' as DATETIME)
CAST('2017-03-02' + ' ' + '05:30' as DATETIME)
DATEADD(ss,-ConfTask.[LastExecutedDuration], ConfTask.[LastExecutedDate])
I'm a bit stuck on how to get round this issue. Any help would be much appreciated.
Clearly, you must have some unexpected values in the column.
I would suggest finding them using a query such as this:
SELECT LastExecutedDuration, LastExecutedDate
FROM ConfTask
WHERE TRY_CONVERT(datetime,
CONVERT(VARCHAR(10),
DATEADD(second,
-ConfTask.[LastExecutedDuration],
ConfTask.[LastExecutedDate]
)
103
) + ' ' + '05:30')
)
You can also simplify the logic, by just using date functions:
select dateadd(minute,
5 * 60 + 30,
convert(datetime,
convert(date,
dateadd(second,
- ConfTask.LastExecutedDuration
ConfTask.LastExecutedDate
)
)
)
)
This worked:
CONVERT(DATETIME, CONVERT(CHAR(8), DATEADD(ss,-ConfTask.[LastExecutedDuration], ConfTask.[LastExecutedDate]), 112)) + ' ' + CONVERT(CHAR(8), '05:30:00', 108)
I'm trying to convert some MS Access queries to T-SQL to use in SSIS (basically converting Access db to SQL server 2008) and I'm having trouble converting an IIF() statement. I tried several approaches and it always resulted in an error.
The query creates a column with dates that are "original Date + 2 years if the condition is met and original Date + 1 if the condition is not met". The first part of the IIF() eliminates the case of the original year being a leap year and so the possibility of generating a non-existing date.
The original IIF() statement is:
IIf((Day(Date)=29 And Month(Date)=2),
IIf(Desc Like "*" & "123" & "*",
DateSerial(Year(Date)+2,Month(Date),Day(Date)-1),
DateSerial(Year(Date)+1,Month(Date),Day(Date)-1)),
IIf(Desc Like "*" & "123" & "*",
DateSerial(Year(Date)+2,Month(Date),Day(Date)),
DateSerial(Year(Date)+1,Month(Date),Day(Date)))) AS Term
So the problem is not only an IIF() statement but also DATESERIAL function. I found the solution for the DATESERIAL() function using CAST() (SQL server 2008 does not have the DATEFROMPARTS() function...).
I tried using CASE() like this:
CASE
WHEN DAY(Date)=29 AND Month(Date)=2 THEN
CASE
WHEN Desc LIKE "%123%" THEN
CAST(CAST(YEAR(Date)+2 AS VARCHAR(4)) + RIGHT('0' + CAST(MONTH(Date) AS VARCHAR(2)), 2) + RIGHT('0' + CAST(DAY(Date)-1 AS VARCHAR(2)), 2) AS DATETIME )
ELSE CAST(CAST(YEAR(Date)+1 AS VARCHAR(4)) + RIGHT('0' + CAST(MONTH(Date) AS VARCHAR(2)), 2) + RIGHT('0' + CAST(DAY(Date)-1 AS VARCHAR(2)), 2) AS DATETIME ) END
ELSE CASE
WHEN Desc LIKE "%123%" THEN
THEN CAST(CAST(YEAR(Date)+2 AS VARCHAR(4)) + RIGHT('0' + CAST(MONTH(Date) AS VARCHAR(2)), 2) + RIGHT('0' + CAST(DAY(Date) AS VARCHAR(2)), 2) AS DATETIME )
ELSE CAST(CAST(YEAR(Date)+1 AS VARCHAR(4)) + RIGHT('0' + CAST(MONTH(Date) AS VARCHAR(2)), 2) + RIGHT('0' + CAST(DAY(Date) AS VARCHAR(2)), 2) AS DATETIME )END END AS Term
I also tried using COAELSCE() but with no better outcome.
I really don't know if I have made some kind of syntax error or where the problem could be.
Thank you in advance for any help.
edit: I'll add error message I'm getting: Incorrect syntax near '...'. The '...' changes as I try different approaches, sometimes its ELSE, THEN etc.
SQL Server's DATEADD may help to simplify things here...
CASE WHEN (<your condition>)
THEN DATEADD(YEAR, 1, [OriginalDate])
ELSE DATEADD(YEAR, 2, [OriginalDate])
END
Should also cope with leap years too.
DateSerial is such a useful function to have around, just create your own.
I got my version from here:
CREATE FUNCTION dbo.DateSerial
(
#year int,
#month int,
#day int
)
RETURNS datetime
AS
BEGIN
DECLARE #date datetime
-- convert date by adding together like yyyymmdd
SET #date = cast(#year * 10000 + 101 AS char(8));
-- Add to date the proper months subtracting 1, since we used 1 as start instead of zero.
SET #date = dateadd(mm , #month - 1 , #date)
-- Add to date the proper days subtracting 1, since we used 1 as start instead of zero.
SET #date = dateadd(dd , #day - 1 , #date);
RETURN #date ;
END;
GO
Here is another way: Date serial in SQL?
Then use the function just as you did in Access.
Edit: just noticed that your outer IIF is to handle leap years. This is not needed when using dateadd().
I want to use a case statement where my logic determines a date. If the value returned is a null value I instead want to display 'none' instead of a null value.
See code below.
CASE WHEN DATEDIFF(d, vso.pod_ata, ISNULL(cncr.cntr_date3, GETUTCDATE())) - 14 > 0 THEN vso.pod_ata + 14
ELSE CONVERT(NVARCHAR(10), 'none')
END AS 'start_dem',
SQL SERVER was complaining about conversion so I tried converting it to nvarchar but thats not working. Does anyone know a good solution for this?
You should convert the date, not the string.
CASE WHEN DATEDIFF(d, vso.pod_ata, ISNULL(cncr.cntr_date3, GETUTCDATE())) > 14 THEN convert(VARCHAR(10), dateadd(day, 14, vso.pod_ata), 120)
ELSE 'none'
END AS 'start_dem',
SQL, to my knowledge, does not allow mixed datatypes in the same column. Convert the vso.pod_ata + 14 to an NVARCHAR as well.
CASE WHEN DATEDIFF(d, vso.pod_ata, ISNULL(cncr.cntr_date3, GETUTCDATE())) - 14 > 0 THEN CONVERT(NVARCHAR(50), vso.pod_ata + 14)
ELSE CONVERT(NVARCHAR(50), 'none')
END AS 'start_dem',
I need to use an if statement in tsql to break execution of the stored procedure if the specified date is not two days in the future.
#date is one of the parameters of the stored proc. Users know to format it correctly as mm/dd/yy (tsql date format 1)
Check it out:
date_check:
if #date <> convert(varchar, getdate(), 1)+datepart(day, 2)
begin
print 'Date is not two days in the future. Please enter an acceptable date.'
goto the_end
end
else
goto part_2
--Please excuse the goto's.
Does this look like it might work?
Thanks!
if #date <> dateadd(day, 2, convert(datetime, convert(varchar, getdate(), 101)))
begin
print 'Date is not two days in the future. Please enter an acceptable date.'
end
This assumes that #date is a date-only component and that there is no time.
You should look at DATEADD for that.
Something like:
IF #date <> DATEADD(day, 2, GETDATE())
Though you will probably need to get rid of the hours/minutes/seconds/milliseconds portion.