T_SQL: Conversion of Date to DateTime ... error [closed] - tsql

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am getting this error message when running one of my queries:
Msg 242, Level 16, State 3, Line 3 The conversion of a date data type
to a datetime data type resulted in an out-of-range value.
I started out by removing all date-comparisons in the Where clause but I am still getting the error.
Any ideas what is going on and what I should look for?
Thanks!

Without seeing more I can only guess, however, the likely cause is related to either NULL values or out of range values. The different date and datetime data types have different date ranges. For example date has a range 0001-01-01 through 9999-12-31 but datetime has the range 1753-01-01 through 9999-12-31.

Related

Joi validate Dates for range and exact date

I am working on a date input for interfacing with stripe. When testing we need to allow the specific date of 01/01/1901 but for all other cases we need to ensure that the birthdate is 18 or more years ago but not more than 100 years ago. This last part I can easily do with the following joi command
Joi.date().format("MM/DD/YYYY").raw().required().max(<a specific date value>).min(<Another Date Value>).messages(errorMessages)
My question is, for the sake of testing we need to allow specific dates to pass validation and I'm wondering if there is a way to have joi validate dates between a range or dates that are exactly equal to
thanks for any advice and help.
Yes. This can be achieved using any.alternatives() and any.valid().
The following will allow your range rule plus a list of arbitrary dates ['01/01/1901', '12/12/2020']
Joi.object().keys({
date: Joi.alternatives(
Joi.date().format("MM/DD/YYYY").raw().required().max(<a specific date value>).min(<Another Date Value>).messages(errorMessages),
Joi.any().valid('01/01/1901', '12/12/2020')
)
})

Postgres Select all Sundays in 2019 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
how would I go about selecting data from only Sundays in 2019 in Postgres?
I think Sundays are a 0 but I am unsure as to how to query it like that.
I am selecting from a preexisting table that contains timestamp strings like 2020-09-03 02:11:60, so basically I want to select all the timestamps that occurred in 2019 in my table usermetrics in the column timestamp.
You have two requests.
The first to find timestamps in 2019:
SELECT
*
FROM
usermetrics
WHERE
timestamp BETWEEN '01/01/2019'::date AND '01/01/2020'::date
The second to find those that occurred on Sundays:
SELECT
*
FROM
usermetrics
WHERE
timestamp BETWEEN '01/01/2019'::date AND '01/01/2020'::date
AND
extract(DOW FROM timestamp) = 0;
01/01/2020::date is used as 12/31/2019::date would miss timestamps that occurred after midnight.

In Snowflake, how to derive the last 365 days for all dates in Date Dimension table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
My date dimension table has a column "date_id" which has dates from 2016-01-01 till 2025-01-01. I wanted to create a view on the top of this date table and introduce an extra column say "date_id_365". The new column should have ALL the last 365 dates for each date_id.
Like for example If I pick a date_id to say 2020-01-15, that date_id should return dates from 2019-01-15 till 2020-01-15.
How to write the SQL in snowflake for this?
I think maybe you're looking for something like this...which is pretty standard SQL, not specifically for Snowflake. This assumes that date_id is a DATE field, which I'm not seeing in your comments, but you could convert it or use the date field.
SELECT a.date_id, b.date_id as date_id_365
FROM dim_date a
JOIN dim_date b
ON b.date_id BETWEEN DATEADD(year,-1,a.date_id) AND a.date_id
I haven't tested this myself, but pretty sure this will work for you. The only issue will be with your dates for the year 2016, since it won't have any dates in 2015 to join back to.

MongoDB storing one day before than actual date [duplicate]

This question already has an answer here:
Mongodb saves one day less - Time Zone Issue
(1 answer)
Closed 4 years ago.
I am facing one issue to store the actual date(01/08/2018) that what I passed from model. My document is stored successfully but MongoDB store the one day before than what I passed in model.
Here I am passing the date(dd/MM/yyyy) is 01/08/2018
For more detail please check the below snaps.
After successfully saved the record, I checked in Robo 3T(MongoDB) and I show that it stored the one day before than actual value. Stored date is 2018-07-31. For more detail please see the below snap.
I hope so might be issue with timezone or offset but I don't know what is the solution.
By using Moment Library https://momentjs.com/ your issue can be solved. you have to just passed a date like below and follow the format.
var myDate = new Date(moment("2018-07-04").format("YYYY-MM-DD"))
save this to you DB it will work

How to convert a 5 digit INT to date

I apologise if this question has been asked but I cannot find an answer that quite fits.
I have a database with dates stored as 5 digit integers. I can covert these to datetime, however the dates are showing in the future.
For example,
select convert(datetime,StartDate,103)
from dpm.Schedule
where ScheduleID like 50003;
Gives the results of
2107-05-31 00:00:00:000 but this date should actually be 26/05/2008.
I am pretty new to T-SQL and have looked for sometime to find the answer to this but I am reaching the end of my sanity.
We cannot answer because you are doing a lot of confusion. To start 5003 is the Id of the record and [StartDate] is the value you are trying to convert.
Also drop using operator LIKE in the Id
Example:
select convert(datetime,50003,103)
returns:
2036-11-26 00:00:00.000
but you are trying to convert the value returned by
select StartDate
from dpm.Schedule
where ScheduleID = 50003;
Please edit your question and show us a nice example using SQL Fiddle