Convert 24h to 12h datetime format in SQL Server - sql-server

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

Related

Default format when casting date(time) to varchar

What is the default format that is used when I cast date or datetime into varchar? What settings affect the result? Did I come to the right conclusion that date always converts into YYYY-MM-DD and datetime is only affected by language?
declare #myDatetime as datetime = dateadd(day,3,getutcdate());
declare #myDate as date = #myDatetime;
set dateformat ymd; --no difference?
set language us_english; --affects datetime only?
select
cast(#myDate as varchar(max)) --2017-10-13
, cast(#myDatetime as varchar(max)) --Oct 13 2017 1:47PM
;
set dateformat dmy; --no difference?
select
cast(#myDate as varchar(max)) --2017-10-13
, cast(#myDatetime as varchar(max)) --Oct 13 2017 1:47PM
;
set language Lithuanian; --affects datetime only?
select
cast(#myDate as varchar(max)) --2017-10-13
, cast(#myDatetime as varchar(max)) --spl 13 2017 1:56PM
;

How would I write a SQL Server query that changes all date field in the database?

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)

Combine datetime from two datetimes - SQL Server 2012

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

How to subtract two date as datetime2 format in Sql Server 2008

How can I create a method to subtract two dates and this is equal to in real date as format datetime2(7) in sql server 2008.
For example ,I create this method:
Delete from TblMessage
Where MDateTime<((SELECT TOP (1)MDateTime FROM TblMessage ORDER BY MDateTime DESC)- ('2013-10-04 16:47:56.0000000'))
but it is not working .I want to result of subtract two date like this:
MDateTime1:2013-10-05 16:47:56.0000000
MDateTime2:2013-09-04 16:47:56.0000000
Result:2013-01-01 00:00:00.0000000
Result=MDateTime1-MDateTime2
How can I do this. Thanks...
Perhaps you are looking for this?
select DATEADD( day, datediff(day,GETDATE(), getdate() - 10), GETDATE() ) ;
DATEDIFF(dayofyear,MDateTime1,MDateTime2) AS Result
http://msdn.microsoft.com/en-us/library/ms189794.aspx
DECLARE
#DATE1 DATETIME = '2013-10-05 16:47:56.000',
#DATE2 DATETIME = '2013-09-04 17:37:42.000',
#DATEDIFF AS INT,
#BASEDATE DATETIME;
-- USE WHAT EVER DATE YOU WISH TO BE YOUR BASE DATE
SET #BASEDATE = '1/1/1900';
SET #DATEDIFF = DATEDIFF(SECOND, #DATE2, #DATE1);
SELECT #DATE1,#DATE2,#DATEDIFF, DATEADD(SECOND,#DATEDIFF,#BASEDATE)
Thus a scalar function could be created like this...
CREATE FUNCTION dbo.sf_GetMeasureDate
(
#EndDate DATETIME,
#StartDate DATETIME,
#BaseDate DATETIME
)
RETURNS DATETIME
AS
BEGIN
DECLARE #DATEDIFF AS INT
SET #DATEDIFF = DATEDIFF(SECOND, #StartDate, #EndDate);
Return DATEADD(SECOND,#DATEDIFF,#BASEDATE)
END
GO
Then within you regular SELECT statement you can call the function as such.
SELECT dbo.sf_GetMeasureDate('2013-10-05 16:47:56.000','2013-09-04 17:37:42.000','1/1/1900')
or within an existing query:
SELECT dbo.sf_GetMeasureDate([fld1],[fld2],'1/1/1900')

How I can compare datetime datatype with string?

I would like to compare datetime datatype, like "20/12/2011 00:00:00", with compound string date format (I mean that it is composed of string of date, string of month and string of year).
For example, coloumn entime is datatime datatype which is stored "20/12/2011 00:00:00" data and other three column(date,month,year respectively) are string. so I want to compare between entime column with the date,month and year composed together, How I can write SQL Command to suppurt the above requirement ?
Hope you can help me ?
The best option is to convert the datetime to string and then make the needed comparisons.
You can see here how to make the conversion.
There is also the DATEPART function as an alternative.
SELECT *
FROM DateTable
WHERE
DATEPART(YEAR, [DATECOLUMN]) = #YearString
AND DATEPART(MONTH, [DATECOLUMN]) = #MonthString
AND DATEPART(DAY, [DATECOLUMN]) = #DayString
You can go below way, would you please try it out, thanks
SET DATEFORMAT DMY
SELECT CAST(CONVERT(VARCHAR(15), GETDATE(), 105) AS DATETIME)
SELECT CAST(('20'+'-'+'12'+'-'+'2011') AS DATETIME)
As an example:
SET DATEFORMAT DMY
SELECT CAST(CONVERT(VARCHAR(15), yourDateColumn, 105) AS DATETIME) FROM TableName
SELECT CAST((dayColumn+'-'+monthColumn+'-'+yearColumn) AS DATETIME)
FROM anotherTable
Finally the comparison:
SELECT t1.* FROM tableName t1, anotherTable t2
WHERE CAST(CONVERT(VARCHAR(15), t1.DateColumnName, 105) AS DATETIME)
= CAST((t2.dayColumn+'-'+t2.monthColumn+'-'+t2.yearColumn) AS DATETIME)
Is this your requirement ?
DECLARE #tblTemp TABLE
(
DAY VARCHAR(10)
,Month VARCHAR(10)
,Year VARCHAR(10)
)
DECLARE #dtDateTime VARCHAR(10) = '20/12/2011 00:00:00'
INSERT INTO #tblTemp VALUES
('01','01','2011'),
('01','02','2011'),
('01','03','2011'),
('01','04','2012');
select * from #tblTemp where CONVERT(DATE,Year +Month+DAY ,103) < CONVERT(DATE,#dtDateTime,103)

Resources