Date and time change sql - sql-server

I am using the statement below to separate the date and time from one column into two. I get a result like this.
AppointmentDate AppointmentTime
10/11/2017 08:30:00.0000000
10/11/2017 16:50:00.0000000
How do I get the time to be in non military time and without the seconds and the milliseconds? Or if that's not possible, just remove the seconds and milliseconds?.
SELECT
CONVERT(varchar(19),ScheduleEntry.ScheduleDate,101) as AppointmentDate,
CONVERT(time,ScheduleEntry.ScheduleDate) as AppointmentTime

For 12-hr format, use:
SELECT format(getdate(), 'hh:mm tt');
And for 24-hr format, use:
SELECT format(getdate(), 'HH:mm');
Here are some other ways of formatting date-time:
SELECT format(getdate(), 'yyyy/MM/dd hh:mm tt'); -- 2017/10/10 06:30 PM
SELECT format(getdate(), 'yyyy/MM/dd HH:mm'); -- 2017/10/10 18:30
SELECT convert(varchar, getdate(), 100) -- mon dd yyyy hh:mmAM
SELECT convert(varchar, getdate(), 101) -- mm/dd/yyyy – 10/02/2008
SELECT convert(varchar, getdate(), 102) -- yyyy.mm.dd – 2008.10.02
SELECT convert(varchar, getdate(), 103) -- dd/mm/yyyy
SELECT convert(varchar, getdate(), 104) -- dd.mm.yyyy
SELECT convert(varchar, getdate(), 105) -- dd-mm-yyyy
SELECT convert(varchar, getdate(), 106) -- dd mon yyyy
SELECT convert(varchar, getdate(), 107) -- mon dd, yyyy
SELECT convert(varchar, getdate(), 108) -- hh:mm:ss
SELECT convert(varchar, getdate(), 109) -- mon dd yyyy hh:mm:ss:mmmAM (or PM)
SELECT convert(varchar, getdate(), 110) -- mm-dd-yyyy
SELECT convert(varchar, getdate(), 111) -- yyyy/mm/dd
SELECT convert(varchar, getdate(), 112) -- yyyymmdd
SELECT convert(varchar, getdate(), 113) -- dd mon yyyy hh:mm:ss:mmm
SELECT convert(varchar, getdate(), 114) -- hh:mm:ss:mmm(24h)
SELECT convert(varchar, getdate(), 120) -- yyyy-mm-dd hh:mm:ss(24h)
SELECT convert(varchar, getdate(), 121) -- yyyy-mm-dd hh:mm:ss.mmm
SELECT convert(varchar, getdate(), 126) -- yyyy-mm-ddThh:mm:ss.mmm
I hope this helped.

Related

How to insert date in SQL Server from any format to datetime format?

I have a requirement where I need to insert date from any format like mm/dd/yyyy, dd/mm/yyyy, yyyy/mm/dd, dd-mm-yyyy to datetime format yyyy-mm-dd.
how to do this?
This is what I tried:
declare #date nvarchar(20)
set #date='2018/10/20'
SELECT case when #date=FORMAT(convert(date, #date, 105),'dd-MM-yyyy')
then (select convert(datetime,convert(date, #date, 105), 105))
when #date= FORMAT(convert(date, #date, 103),'dd/MM/yyyy')
then (select convert(datetime,convert(date, #date, 103), 103))
when #date= FORMAT(convert(date, #date, 110),'MM-dd-yyyy')
then (select convert(datetime,convert(date, #date, 110), 110))
when #date= FORMAT(convert(date, #date, 101),'MM/dd/yyyy')
then (select convert(datetime,convert(date, #date, 101), 101))
when #date= FORMAT(convert(date, #date, 23),'yyyy-MM-dd')
then (select convert(datetime,convert(date, #date, 23), 23))
when #date= FORMAT(convert(date, #date, 111),'yyyy/MM/dd')
then (select convert(datetime,convert(date, #date, 111), 111))
end
but it failed when converting '2018/10/20' to 'dd/mm/yyyy' and other irrelevant formats that used in case statement except 'yyyy/MM/dd'.
CAST and CONVERT
DECLARE
#txt1 varchar(10)='12/05/2019', -- mm/dd/yyyy
#txt2 varchar(10)='05/12/2019', -- dd/mm/yyyy
#txt3 varchar(10)='2019/12/05', -- yyyy/mm/dd
#txt4 varchar(10)='05-12-2019' -- dd-mm-yyyy
SELECT
CONVERT(date,#txt1,101) -- mm/dd/yyyy
,CONVERT(date,REPLACE(#txt2,'/','.'),103) -- dd/mm/yyyy
,CONVERT(date,#txt3,111) -- yyyy/mm/dd
,CONVERT(date,#txt4,105) -- dd-mm-yyyy

Sql server gettime() with zero second

how to SELECT GetDate() with zero second example: getdate() value 2017-09-19 09:35:51.340 but l need to select 2017-09-19 00:00:00.000 ?
DECLARE #date DATE= GETDATE();
SELECT CAST(#date AS DATETIME);
OR
SELECT CONVERT(DATETIME, CONVERT(DATE, GETDATE()));
SELECT CONVERT(datetime, CONVERT(date, GETDATE()))

datetime format in sql server 2008

I have used below code to convert a datetime to string,
DECLARE #StartDate datetime = '08/07/2015 12:10 AM'
set #StartDate = dateadd(hour,12, #StartDate);
select CONVERT(VARCHAR(10),#StartDate, 101) + RIGHT(STUFF(CONVERT(VARCHAR(32), #StartDate,100), 18, 0, ' '),8)
but I am getting output as "08/07/201512:10 PM" , there is no space between date and time, How can I correct this?
If i correctly understood your problem then there is small correction required in your code. I added +' '+ i.e. a blank space between your date convert and right stuff. Complete code is as given below.
DECLARE #StartDate DATETIME = '08/07/2015 12:10 AM'
SET #StartDate = DATEADD(HOUR,12, #StartDate);
SELECT CONVERT(VARCHAR(10),#StartDate, 101) +' '+
RIGHT(STUFF(CONVERT(VARCHAR(32),#StartDate,100), 18, 0, ' '),8)
Result
08/07/2015 12:10 PM
i.e. space between date and time also space between 12:10 and PM
To cover new case provided :
DECLARE #StartDate DATETIME = '08/07/2015 2:10 AM'
SET #StartDate = DATEADD(HOUR,12, #StartDate);
SELECT CONVERT(VARCHAR(10),#StartDate, 101) +' '+
LTRIM(RIGHT(STUFF(CONVERT(VARCHAR(32),#StartDate,100), 18, 0, ' '),8))
Result
08/07/2015 2:10 PM
i.e. no extra space when time is like 2:10 PM
Here is one way to do it:
DECLARE #StartDate datetime = '2015-08-07T00:10:00';
SET #StartDate = dateadd(hour,12, #StartDate);
SELECT #StartDate As StartDate,
CONVERT(CHAR(10), #StartDate, 101) + ' ' + -- DateString,
SUBSTRING(CONVERT(CHAR(19), #StartDate, 100), 13, 5) + ' ' + -- TimeString
RIGHT(CONVERT(CHAR(19), #StartDate, 100), 2) As DateString -- AM/PM
Result:
StartDate DateString
----------------------- -------------------
2015-08-07 12:10:00.000 08/07/2015 12:10 PM
The following snippet will produce the output you've indicated in the question.
SET DATEFORMAT MDY;
DECLARE #StartDate DATETIME = '08-07-2015 12:10 AM';
SET #StartDate = DATEADD(HOUR, 12, #StartDate);
SELECT CONVERT(VARCHAR, #StartDate, 103) + ' ' +
CONVERT(VARCHAR, CAST(#StartDate AS TIME), 108) +
CASE WHEN DATEPART(HOUR, #StartDate) < 12 THEN ' AM' ELSE ' PM' END;
N.B. As others have pointed out, you would be better off using ISO format for input dates.
Updated dateformat from DMY to MDY and explicitly adding AM/PM to the end.

Comparing DateTIme Column value with DateTime String SQL Server

I am trying to compare DateTime string to a DateTime Value Column in DB but its returning me 0 records Here is the query
DECLARE #p_date DATETIME
SET #p_date= CONVERT( DATETIME, '9/1/2015 10:06:22 PM', 131 )
SELECT UpdateUserId, UpdateTimeA
From SubmitSheets
WHERE CONVERT( DATETIME, UpdateTimeA, 131 ) = #p_date
the value in UpdateTimeA column is 2015-09-01 22:06:22.447
Like I said in my comment, I'm not sure 131 is the right style to convert to datetime (at least, I get a very different value).
If you want to compare dates without the second fraction, you'll have to compare as strings. Converting both dates to a varchar(19) will cut off the fractions.
Finally found a way
SELECT UpdateUserId,UpdateTimeA
From SubmitSheets
WHERE CONVERT(VARCHAR(16),UpdateTimeA,100) = CONVERT(VARCHAR(16),CONVERT( DATETIME, '9/1/2015 10:06:22 PM',101),100)
I think the #p_date parameter is not in same format. Try this:
DECLARE #p_date DATETIME
SET #p_date= CONVERT( DATETIME, '9/1/2015 10:06:22 PM')
SELECT UpdateUserId, UpdateTimeA
From SubmitSheets
WHERE CONVERT(DATETIME, (CONVERT(VARCHAR, UpdateTimeA, 101) + ' ' + CONVERT(VARCHAR, UpdateTimeA, 108)), 131) = CONVERT(DATETIME, #p_date, 131)
So another tryout may be
An example:
DECLARE #a TABLE
(
ID int,
datet datetime
)
INSERT INTO #a
(ID, datet)
VALUES (1 , CONVERT(DATETIME, GETDATE(), 131))
,(2 , CONVERT(DATETIME, GETDATE(), 131))
,(3 , CONVERT(DATETIME, GETDATE(), 131))
,(4 , CONVERT(DATETIME, GETDATE(), 131))
,(5 , CONVERT(DATETIME, GETDATE(), 131))
,(6 , CONVERT(DATETIME, GETDATE(), 131))
,(7 , CONVERT(DATETIME, GETDATE(), 131))
,(8 , CONVERT(DATETIME, GETDATE(), 131))
,(9 , CONVERT(DATETIME, GETDATE(), 131))
SELECT ID, datet, ConvDatetime
FROM
(
SELECT ID, datet
,CONVERT(DATETIME, (CONVERT(VARCHAR, datet, 101)
+ ' '
+ CONVERT(VARCHAR, datet, 108)), 131) AS ConvDatetime
FROM
#a
) AS dt
WHERE
dt.ConvDatetime= CONVERT(DATETIME,
(CONVERT(VARCHAR, GETDATE(), 101)
+ ' '
+ CONVERT(VARCHAR, GETDATE(), 108))
, 131) -- CONVERT( DATETIME, '9/1/2015 10:06:22 PM', 131 )

Get only the Date part of DateTime in mssql [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get just the Date from grouping in select from DateTime column in SQL Server
How can I get only the Date part of a DateTime ? I'm searching for something like year() function but for the whole date.
This may also help:
SELECT convert(varchar, getdate(), 100) -- mon dd yyyy hh:mmAM (or PM)
-- Oct 2 2008 11:01AM
SELECT convert(varchar, getdate(), 101) -- mm/dd/yyyy - 10/02/2008
SELECT convert(varchar, getdate(), 102) -- yyyy.mm.dd – 2008.10.02
SELECT convert(varchar, getdate(), 103) -- dd/mm/yyyy
SELECT convert(varchar, getdate(), 104) -- dd.mm.yyyy
SELECT convert(varchar, getdate(), 105) -- dd-mm-yyyy
SELECT convert(varchar, getdate(), 106) -- dd mon yyyy
SELECT convert(varchar, getdate(), 107) -- mon dd, yyyy
SELECT convert(varchar, getdate(), 108) -- hh:mm:ss
SELECT convert(varchar, getdate(), 109) -- mon dd yyyy hh:mm:ss:mmmAM (or PM)
-- Oct 2 2008 11:02:44:013AM
SELECT convert(varchar, getdate(), 110) -- mm-dd-yyyy
SELECT convert(varchar, getdate(), 111) -- yyyy/mm/dd
SELECT convert(varchar, getdate(), 112) -- yyyymmdd
SELECT convert(varchar, getdate(), 113) -- dd mon yyyy hh:mm:ss:mmm
-- 02 Oct 2008 11:02:07:577
SELECT convert(varchar, getdate(), 114) -- hh:mm:ss:mmm(24h)
SELECT convert(varchar, getdate(), 120) -- yyyy-mm-dd hh:mm:ss(24h)
SELECT convert(varchar, getdate(), 121) -- yyyy-mm-dd hh:mm:ss.mmm
SELECT convert(varchar, getdate(), 126) -- yyyy-mm-ddThh:mm:ss.mmm
-- 2008-10-02T10:52:47.513
-- SQL create different date styles with t-sql string functions
SELECT replace(convert(varchar, getdate(), 111), '/', ' ') -- yyyy mm dd
SELECT convert(varchar(7), getdate(), 126) -- yyyy-mm
SELECT right(convert(varchar, getdate(), 106), 8) -- mon yyyy
The Source
The solution you want is the one proposed here:
https://stackoverflow.com/a/542802/50776
Basically, you do this:
cast(floor(cast(#dateVariable as float)) as datetime)
There is a function definition in the link which will allow you to consolidate the functionality and call it anywhere (instead of having to remember it) if you wish.
Another nifty way is:
DATEADD(dd, 0, DATEDIFF(dd, 0, [YourDate]))
Which gets the number of days from DAY 0 to YourDate and the adds it to DAY 0 to set the baseline again. This method (or "derivatives" hereof) can be used for a bunch of other date manipulation.
Edit - other date calculations:
First Day of Month:
DATEADD(mm, DATEDIFF(mm, 0, getdate()), 0)
First Day of the Year:
DATEADD(yy, DATEDIFF(yy, 0, getdate()), 0)
First Day of the Quarter:
DATEADD(qq, DATEDIFF(qq, 0, getdate()), 0)
Last Day of Prior Month:
DATEADD(ms, -3, DATEADD(mm, DATEDIFF(mm, 0, getdate()), 0))
Last Day of Current Month:
DATEADD(ms, -3, DATEADD(mm, DATEDIFF(m, 0, getdate()) + 1, 0))
Last Day of Current Year:
DATEADD(ms, -3, DATEADD(yy, DATEDIFF(yy, 0, getdate()) + 1, 0))
First Monday of the Month:
DATEADD(wk, DATEDIFF(wk, 0, DATEADD(dd, 6 - DATEPART(day, getdate()), getdate())), 0)
Edit:
True, Joe, it does not add it to DAY 0, it adds 0 (days) to the number of days which basically just converts it back to a datetime.
We can use this method:
CONVERT(VARCHAR(10), GETDATE(), 120)
Last parameter changes the format to only to get time or date in specific formats.
This may not be as slick as a one-liner, but I use it to perform date manipulation mainly for reports:
DECLARE #Date datetime
SET #Date = GETDATE()
-- Set all time components to zero
SET #Date = DATEADD(ms, -DATEPART(ms, #Date), #Date) -- milliseconds = 0
SET #Date = DATEADD(ss, -DATEPART(ss, #Date), #Date) -- seconds = 0
SET #Date = DATEADD(mi, -DATEPART(mi, #Date), #Date) -- minutes = 0
SET #Date = DATEADD(hh, -DATEPART(hh, #Date), #Date) -- hours = 0
-- Extra manipulation for month and year
SET #Date = DATEADD(dd, -DATEPART(dd, #Date) + 1, #Date) -- day = 1
SET #Date = DATEADD(mm, -DATEPART(mm, #Date) + 1, #Date) -- month = 1
I use this to get hourly, daily, monthly, and yearly dates used for reporting and performance indicators, etc.

Resources