Inserting datatime type column into date type column [closed] - sql-server

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

Related

T-SQL how to properly union date and time in datetime [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 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

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)

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.

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)

Give me best possible loop to iterate in sql with performance [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 9 years ago.
Improve this question
suppose i have two table and both tables consist of one date column eg
OUTTime
12:05:40
12:08:30
12:20:40
and other table consist of
INTime
12:10:35
12:12:23
12:16:40
12:30:11
Now i want rows with Minimum time difference between two tables like 12:08:30 and 12:10:35 gives me OUT and IN. Similarly 12:20:40 and 12:30:11 gives me another row here by eliminating OUTTime 12:05:40 and eliminating 12:12:35 and 12:16:40 from INTime which gives me proper OUtTime and InTime
Any suggestion how to loop to get this data?
If you want to get a matching InTimes and OutTimes and only get the closest, try this:
Set Up
CREATE TABLE #OUTTime
(
[Time] Time
)
INSERT INTO #OUTTime
VALUES
('12:05:40'),
('12:08:30'),
('12:20:40')
CREATE TABLE #InTime
(
[Time] Time
)
INSERT INTO #InTime
VALUES
('12:10:35'),
('12:12:23'),
('12:16:40'),
('12:30:11')
Now the query:
;WITH CTE
As
(
SELECT O.Time AS outTime, (SELECT MIN([Time]) FROM #InTime I WHERE I.[time] > O.[Time]) AS InTime
FROM #OUTTime O
)
SELECT MAX(OutTime) As OutTime, InTime
FROM CTE
GROUP BY InTime
Results
OutTime InTime
12:08:30.0000000 12:10:35.0000000
12:20:40.0000000 12:30:11.0000000
If you UNION the two tables ... something like
SELECT Time, 'IN'
FROM InTimesTable
UNION
SELECT Time, 'Out'
FROM OutTimesTable
Order by Time
You can dump it in excel, and manipulate it manually.
Or, you can fill out your question here, and get a better answer.

Resources