T-SQL how to properly union date and time in datetime [closed] - sql-server

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 21 hours ago.
This post was edited and submitted for review 10 hours ago.
Improve this question
dear gurus, SQL.
Please advise how to do the following:
You need to take a date from one column and join it with the time of another column,
and then add one day to the received date.
I wrote such a query, but on some data it gives a conversion error.
Please tell me a more optimal query so that it always works when combining the date and time format into datetime.
Thank you.
create table dt (date1 datetime, date2 datetime)
insert into dt values('19000101 17:17:00.000','19070101 17:51:00.000')
insert into dt values('19000101 18:20:00.000','19080101 18:21:00.000')
insert into dt values('20000101 06:00:00.000','20100101 06:40:00.000')
select
dateadd(dd,1,convert(datetime,convert(date,date1))+ convert(datetime,convert(time, date2)))
from dt

Here is an example where such an error could occur:
CREATE TABLE Example (
Duration TIME,
AdditionalDelay SMALLINT
)
INSERT INTO dbo.Example (Duration, AdditionalDelay)
VALUES ('00:01:02',3)
SELECT Duration+AdditionalDelay FROM dbo.Example
To fix this, you need to identify the proper unit of measure for AdditionalDelay and use DATEADD instead of +. For example:
SELECT DATEADD(MINUTE,AdditionalDelay,Duration) FROM dbo.Example

Related

How to update dates in a table so that each row's date is pushed ahead by 1 day? [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 12 months ago.
Improve this question
I have a table whose Date column needs pushing ahead by 1 day.
My update query is:
UPDATE TABLENAME
SET DATECOL=DATECOL+1
Is this correct approach? Or do I need to use CTE, for example:
;WITH CTE AS (
SELECT ID, DATECOL
FROM TABLENAME)
UPDATE T
SET T.DATECOL=CTE.DATECOL+1
FROM TABLENAME T
JOIN CTE ON T.ID=CTE.ID
To add a value to any part of date you can use DATEADD function. In your case the part time is DAY.
UPDATE TABLENAME
SET DATECOL=DATEADD(DAY, 1, DATECOL)

Inserting datatime type column into date type column [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 last year.
Improve this question
I want to get 29-01-2021 in column in MYTABLE.
It will works if I:
insert into MYTABLE (column) -- datatype is date
select
Column -- datatype is datetime for example 29-01-2021 15:07:56.000
from OTHERTABLE
This will truncate the time portion, leaving you with a date value:
insert into MYTABLE (column) -- datatype is date
select
cast(Column as date)
from OTHERTABLE
so you want to put a datetime value into a date column, this can be done but you will loose the time portion off course
insert into MYTABLE (column) -- datatype is date
select
convert(date, Column) -- datatype is datetime for example 29-01-2021 15:07:56.000
from OTHERTABLE

How to give system date while creating SQL 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 2 years ago.
Improve this question
SQL Server creating table error.
How to give system date, system date + 2 days while creating a table?
Is the system date the same as the current date?
You can try the SQL DEFAULT Constraint as shown below if I understood your question properly:
Create table Test(id int
, name varchar(20)
, dtDate datetime default DateAdd(dd, 2, getdate()))
insert into Test (id, name) values
(1, 'A')
select * from Test
Live db<>fiddle demo.

SQL Server insert: using a static value to determine date value [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
I am inserting production orders into a SQL Server database with a stored procedure called via Microsoft PowerApps. Currently, the build date column is just using today's date as the build date. Attached below is a sample of records:
As you can see, all 10 of these orders have today's date as the set build date. However, I'd like to dynamically set this build date based upon the necessary output for the day. This will most likely be passed a variable to the stored procedure. For example - if the build output was 5 per day, rows 1-5 would have a build date of '02/17/2020' while the next 5 would have have a build date of '02/18/2020'.
This is simple enough since the number of orders is divisible by the build output. However, let's say the build output is 3 instead of 5. The first three orders would be '02/17/2020', the next three would be '02/18/2020', and the next three would be '02/19/2020'. This would leave one order to be set to '02/20/2020'. Then if I added more orders, it would stack upon each date filling each up to 3. Is there a way to do this via a stored procedure?
You might need to do some adjustments but based on my understanding, this will do the trick ([Date] is the date column in your sample table above):
CREATE PROC yourProcName #pNUM AS INT = 1 --PARAMETER
AS
(
DECLARE #pDate DATE;
SET #pDate = COALESCE((SELECT TOP 1 CONVERT(DATE, MAX([Date])), GETDATE()) FROM yourDestTable); --SETTING THE DATE AS MAX DATE ALREADY EXISTS IN THE TABLE. IF NOTHING IS THERE, WE'LL USE TODAY
DECLARE #pLead INT = COALESCE((SELECT TOP 1 COUNT(*) FROM yourDestTable WHERE [Date] = #pDate), 0);--SEE HOW MANY RECORDS IN THE DATABASE EXIST WITH THE DATE WE DEFINED
WITH cte
AS
(
SELECT *,
FLOOR((
ROW_NUMBER() OVER(ORDER BY #pDate) -
0.001)/#pNUM) AS RN -- SEPARATING RECORDS USING ROW_NUMBER DIVIDED BY DEFINED NUMBER OF PRODS PER DAY. USING FLOOR AND MINUS A SMALL AMOUNT TO ROUND IT RIGHT
FROM yourSourceTable
)
INSERT INTO yourDestTable
SELECT
C.*,
DATEADD(DAY, LEAD(C.RN, #pLead), #pDate) AS [Date] --PUSHING THE NUMBER BASED ON ROWS ALREADY EXISTED WITH THE MAX_DATE
INTO yourDestTable
FROM cte AS C
)
You might want to use MOD in CTE based on your usecase. But the idea should work. Then call it like this EXEC yourProcName <number of transactionsday>

tsql between two dates which can be null [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 8 years ago.
Improve this question
I'm new in TSQL.
I need to write a stored procedure which returns filtered records.
So the user has two dates to enter. He may enter only one date, or two, or both, or any.
In stored proc I have two params #From, #To - date type.
I need to search records.
If user didn't enter dates - I don't care about dates in the select query.
If the user entered 2 dates - I need to search with them inclusively.
If the user entered #From date - I need to search up to today inclusively.
If the user entered #To date - I need to search dates less than #To.
Thanks for your help.
SELECT ColumnList
FROM MyTable
WHERE
(#FromDate IS NULL OR FromDateColumn >= #FromDate)
AND (#ToDate IS NULL OR ToDateColumn <= #ToDate )
(But be aware that this can suffer from the effects of an unsuitable cached query plan due to parameter sniffing, especially if there is a large number of parameter conditions in the WHERE clause)
SELECT
Columns
FROM
Table
WHERE
DateColumn BETWEEN (CASE WHEN #FromDate IS NULL THEN DateColumn ELSE #FromDate END)
AND (CASE WHEN #ToDate IS NULL THEN DateColumn ELSE #ToDate END)

Resources