How can I combine a datetime value from two datetimes.
The first value contains appropriate date and the second value contains appropriate time (but also has a date that I want to skip).
DECLARE #date DATETIME = '2016-12-08 00:00:00.000'
DECLARE #time DATETIME = '2016-12-07 12:15:00.000'
SELECT #date + #time returns: 2133-11-14 12:15:00.000
but I want: 2016-12-08 12:15:00.000
Use style part in Convert function to extract date and time in varchar(avoids some explicit conversion to varchar) type and concat the the results to get the result date
112 to extract date
114 to extract time
Try this
DECLARE #date DATETIME = '2016-12-08 00:00:00.000'
DECLARE #time DATETIME = '2016-12-07 12:15:00.000'
SELECT CONVERT(DATETIME, CONVERT(VARCHAR(50), #date, 112) + ' '
+ CONVERT(VARCHAR(50), #time, 114))
Result : 2016-12-08 12:15:00.000
Related
I have old DBF database that contains date in old 6 digit formats like 291292 or 150793 or even 010302.
I import tables into SQL Server and now I need to convert it to datetime format.
Of course must be converted to similar strings 29.12.1992, 15.07.1993, 01.03.2002 first.
You may try this.
GO
declare #date date
set #date = '901124'
select CONVERT(varchar(10), #date, 102) as [Date] --- for yyyy.MM.dd format
TRY THIS
declare #dd varchar(10)
set #dd = '201292'
select CONVERT(VARCHAR(10), #dd, 2), CONVERT(VARCHAR(10), GETDATE(), 2)
, (SUBSTRING(#dd, 5,2) + SUBSTRING(#dd, 3,2) + SUBSTRING(#dd, 1,2))
, CONVERT(varchar(10), cast((SUBSTRING(#dd, 5,2) + SUBSTRING(#dd, 3,2) + SUBSTRING(#dd, 1,2)) as DATE) , 102)
Ok it pretty simple:
GO
declare #date varchar(6)
set #date = '241190'
select CONVERT(date, concat(SUBSTRING(#date, 5,2),SUBSTRING(#date, 3,2),SUBSTRING(#date, 1,2)), 101) as [Date]
result is 1990-11-24
declare #date date = '901124'
select CONVERT(date, #date) as [Date]
I have a Column date of datatype datetime which has values (2017-05-17 00:00:00.000) now i want to save this datetime as 20170517 to a variable.
i tried this:
declare #AAA VARCHAR(50)
select #AAA = convert(varchar, #Date, 112)
but it returns May 17 2017 12:00AM
I tried this and it worked fine:
declare #aaa varchar(50)
SELECT #AAA= FORMAT( #Date, 'yyyyMMdd', 'en-US' )
select #AAA= REPLACE(#AAA,'-','')
How would I write a SQL Server query that changes all fields in datetime columns in a database to random dates in the same, current year?
UPDATE t
SET datetime_column = DATEADD(DAY, abs(checksum(newid())) % 364, DATEADD(YEAR, DATEDIFF(YEAR, 0, datetime_column), 0))
FROM yourtable t
the DATEADD() + DATEDIFF() pair will gives you 1st day of the year
abs(checksum(newid())) % 365 will return a random number between 0 and 364
This approach will convert the datetime ranges into a FLOAT. Then we simply calculate a random number between these two values and convert that random value back into a datetime
Example
Declare #D1 float = cast(cast('2017-01-01 00:00:00' as datetime) as float) -- 42734
Declare #D2 float = cast(cast('2017-12-31 23:59:59' as datetime) as float) -- 43098.9999884259
Select Top 10
RandomDate = cast(rand(cast( NewID() as varbinary ))*(#D2-#D1)+#D1 as datetime)
From master..spt_values A
Returns
RandomDate
2017-06-03 02:01:28.650
2017-06-12 01:05:54.107
2017-04-29 14:23:00.160
2017-10-13 14:37:51.290
2017-10-29 16:35:20.723
2017-06-30 20:54:03.197
2017-08-31 22:46:20.440
2017-02-11 23:42:24.323
2017-04-22 06:31:48.477
2017-12-01 18:05:49.177
EDIT - Dynamic
Declare #SQL varchar(max) = '
Declare #D1 float = cast(cast(''2017-01-01 00:00:00'' as datetime) as float);
Declare #D2 float = cast(cast(''2017-12-31 23:59:59'' as datetime) as float);
'
Select #SQL=#SQL+';Update '+quotename(Table_Schema)+'.'+quotename(Table_Name)+' Set '+quotename(Column_Name)+'=cast(rand(cast( NewID() as varbinary ))*(#D2-#D1)+#D1 as datetime)'+char(13)
From INFORMATION_SCHEMA.COLUMNS
Where Data_Type='datetime'
and Table_Catalog = 'OnlineStore'
Print #SQL
--Exec(#SQL)
I want a datetime variable which will be having 12 hour datetime format.
Below example converts date in 12 hour but not helpful because it is in VARCHAR format, if tries to convert in datetime it shows like 2016-03-08 00:00:00.000-
declare #t datetime = NULL
set #t = '2016-03-08 00:00:00'
SELECT CONVERT(VARCHAR, #t, 100) AS DateTime_In_12h_Format
I want a variable which will be holding 12 hour format something like this
declare #t datetime = NULL
set #t = '2016-03-08 00:00:00'
set #t = CONVERT(datetime, #t, 100)
select #t -> this should be -> Mar 8 2016 12:00AM
If you want to convert the current datetime for example:
SELECT CONVERT(VARCHAR, getdate(), 100) AS DateTime_In_12h_Format
Instead of getdate() you can put your desired column in a query (such as tdate in your example). If you want JUST the time in 12h and not the date and time use substring/right to separate them. It seems that you already know how to =).
This page lists every datetime conversion. It's really handy if you need other types of conversions.
Declare one more varchar to save new datetime
declare #t datetime = NULL
declare #t1 varchar(20) = NULL
set #t = '2016-03-08 00:00:00'
set #t1 = CONVERT(varchar(20), #t, 100)
select #t1 as DateTime_In_12h_Format
Working in SQL Server, I have a column that contains a year in numeric format. I need to make that year into a January 1st date of that 'year'. I've tried a few commands and the latest attempt is:
cast('01/01/' + X.[YEAR] as datetime)
What am I missing?
DECLARE #Year INT = 2010
SELECT CAST(CAST(#Year AS varchar) + '-1-1' AS DATETIME) -- 2010-01-01
Another way:
select GETDATE(),
DATEADD (day, - DATEPART(dayofyear, GETDATE()) + 1, CONVERT(date, GETDATE()))