Issue with chosing between dates in SQL Server, error msg241 - sql-server

I'm trying to query this:
SELECT *
FROM dbo.rate_all_ports
WHERE start_date <= CONVERT(DATE, GETDATE())
AND stop_date >= CONVERT(DATE, GETDATE())
But it doesn't work. I get the error:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
In the table I'm querying from the dates are formatted like YYYY-MM-DD so I don't know where the error.
If I query like this:
SELECT *
FROM dbo.rate_all_ports
WHERE start_date <= '2022-12-02'
AND stop_date >= '2022-12-02'
it works.
And if query SELECT CONVERT(DATE, GETDATE()), I get 2022-12-02.

Related

Get results for one day back in sql server

I am trying to get results from 1 day back, for example if i have a job that runs today at 1:00:00 am the 22/05/2018 i want it to get back the results for the 21/05/2018 00:00:00 am to 21/05/2018 23:59:59 pm.
i tried the follwing
select *
from table
where CreatedDateTime BETWEEN DATEADD(day, -1, GETDATE()) AND DATEADD(day, -0, GETDATE()) // it brings back everything from yesterday and today
example of how my created date time is stored in the db 2018-05-21 16:39:09.4830000
The bewteen operator filters the dates based on >= and <=
You need :
select *
from table
where CreatedDateTime >= DATEADD(day, -1, GETDATE()) AND
CreatedDateTime < GETDATE();
I suspect you would need cast(... as date) if so, the you can directly express this as
select *
from table
where cast(CreatedDateTime as date) = cast(DATEADD(day, -1, GETDATE()) as date);
Here is a good BLOG on filtering date range in query.
SELECT *
FROM table
WHERE CreatedDateTime BETWEEN GETDATE() -1 AND GETDATE()

SQL query in between dates does not work for "AM to PM"

SELECT GETDATE() StartDate
2018-03-28 10:24:44.747
SELECT GETDATE()+15 EndDate
2018-04-12 10:24:44.747
Column Login date is type DateTime
SELECT * FROM employee WHERE Logindate BETWEEN GETDATE() AND GETDATE()+15
I have around 300 records that matches the filter condition but I see only 60 records here for these dates
How can I check for Logindate in between "AM" till "PM"?
What you see as result of GETDATE() is the datetime format in 24 hours (so you don't see AM or PM).
If you want to check complete days, use the comparison with DATE format, or even better, truncate your GETDATE() result time component.
select
*
from
employee
where
Logindate >= CONVERT(DATE, GETDATE()) and
Logindate < CONVERT(DATE, DATEADD(DAY, 16, GETDATE())
Note that I'm adding 16 days but the filter is lower than (not including equal).

Convert varchar to datetime and add seconds

I have two columns startDate (160812 - year, month, day) and startTime (112345 - hour, mimutes, seconts) with a varchar datatype my target is concatenate them and convert them into datetime. And I should added to them other column (duration - int)
I tried something like this:
WITH [A] AS
(
SELECT (startDate + startTime) AS time1
FROM [Date]
)
SELECT
CONVERT(datetime, A.time1, 20)
FROM
[A]
however I get an error message:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
Any better ideas to try?
I think you want something like this:
select (convert(datetime, startDate, 12) +
convert(time, stuff(stuff(startTime, 5, 0, ':'), 3, 0, ':')))
) as dt

Inside a where clause: Casting a substring as a date only when IsDate() is true

I am trying to delete all rows that are older than 6 months from a table.
The issue is that the date is grabbed using a substring from a varchar column, and it is not always a valid date.
Here's the query that is failing:
Delete ExampleDatesTable
where CONVERT(datetime, SUBSTRING(DateField, LEN(DateField) - 18, 19)) < DATEADD(month, -6, GETDATE())
Here is what I am trying to do, but it isn't working:
Delete ExampleDatesTable
where IsDate(SUBSTRING(DateField, LEN(DateField) - 18, 19)) = 1
AND CONVERT(datetime, SUBSTRING(DateField, LEN(DateField) - 18, 19)) < DATEADD(month, -6, GETDATE())
Is there a way around this?
You could select valid records first.. then delete based on date.
WITH data AS (
SELECT * FROM ExampleDatesTable
WHERE ISDATE(SUBSTRING(DateField,LEN(DateField) - 18,19)) = 1
)
DELETE data
WHERE CONVERT(DATETIME,SUBSTRING(DateField,LEN(DateField) - 18,19)) < DATEADD(month,-6,GETDATE())

Construct DateTime using today's date at a specific time

I'd like to get 4:30 PM of the current day. Hard-coding this way doesn't work:
SELECT '07242012 16:30:00.000'
This is proving to be more difficult than I thought it would be. How do I approach this?
SQL Server 2000 / 2005:
SELECT DATEADD(MINUTE, 30, DATEADD(HOUR, 16, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP)));
-- or
SELECT DATEADD(MINUTE, (16*60) + 30, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP))
-- or
SELECT CONVERT(DATETIME, CONVERT(CHAR(9), CURRENT_TIMESTAMP, 112) + '16:30');
SQL Server 2008+:
SELECT CONVERT(DATETIME, CONVERT(DATE, CURRENT_TIMESTAMP)) + '16:30';
SQL Server 2012:
SELECT SMALLDATETIMEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), DAY(GETDATE()), 16, 30);
Probably the easiest thing to do is to cast the current date/time to a date (stripping the time off), cast it back to a datetime to allow use of datetime's overloaded + (plus) and, finally cast your desired textual time to a datetime. As follows:
select cast(cast(sysutcdatetime() as date) as datetime) + cast('16:30' as datetime)
returns (when run on 11th Jan 2018):
2018-01-11 16:30:00.000
You can construct this as you like with day, hour, minute etc.
SELECT CURDATE() - interval 1 DAY + interval 2
select(dateadd(day, datediff(day, 0, getdate()), 0) + '20:00') as specified_date
specified_date - Output Column name
20:00 - Specified time(24 hr Format -Default)
getdate() - To get Today's date.

Resources