Converting datetime variable format - sql

I need help in converting a DateTime variable with format mmm dd yyyy
to a Datetime variable with format yyyy-mm-dd

How about this?
Select CAST('Sep 07 2016' AS DATE)
Returns
2016-09-07

I'm assuming your datetime variable is a VARCHAR - this can be directly cast via CONVERT:
Declare #YourVariable Varchar (15) = 'JAN 01 1996'
Select Convert(Date, #YourVariable)
1996-01-01

Related

Convert DATETIME to varchar in SQL

I want to convert DATETIME to VARCHAR with Friday 07-Feb-20 8:30 AM this type of output. I tried to do it like this. But it didn't give the correct format that I want.
DECLARE #STARTDATE DATETIME,#SDATE VARCHAR(250)
SELECT #STARTDATE = SESSION_START FROM SESSION_INFO WHERE SESSION_ID = 2071 //#STARTDATE = 2013-01-28 14:00:00.000
SET #SDATE = CONVERT(VARCHAR,#STARTDATE,100)
PRINT #GOOGLE //Jan 28 2013 2:00PM
I want day-mon-year hh:min AM/PM (Friday 07-Feb-20 8:30 AM) this format. Thank you.
You want format for that e.g.
select format(current_timestamp, 'dddd dd-MMM-yy hh:mm tt');
Note: format doesn't perform as well as convert or cast but it has the added flexibility you need.
You can use the format as follows:
FORMAT (SESSION_START, 'dddd dd-MMM-yy hh:mm tt')
DB<>Fiddle with the example output

hijri to gregorian date, why 30/02/1436 doesn't exist in SQL?

I have being confused as SQL cannot convert (Saudi Arabian dates) Hijra to Gregorian calendar as cannot convert the 30/02/1436.
2nd month is called Safar. Does Safar has 30 or 29 days?
Tank you for your time,
Regards,
PS: Safar in year 1436 only had 29 days
Does this work for you?
CONVERT date format 130 and 131 are for Hijri datetimes
DECLARE #DateTimeNow DATETIME = GETUTCDATE() ;
-- Get Gregorian UTC datetime
SELECT #DateTimeNow --2017-11-15 17:10:52.110
-- Get Hijri datetime
SELECT CONVERT(nchar, #DateTimeNow, 131) --26/02/1439 5:10:52:110PM
--Convert back again
SELECT CONVERT(datetime, CONVERT(nchar, #DateTimeNow, 131), 131) --2017-11-15 17:10:52.110

Cast Datetime offset to Date

I have the date column in datetimeoffset format.
2016-01-09 05:49:06.3744350 +00:00
I want to convert it to only date format, e.g. 2016-01-09.
I was able to convert datetimeoffset to datetime2 with this query.
convert(datetime2, sampleDate, 1) as date
Would be much obliged if I could know how to convert this to the desired format in MS SQL.
Simply:
SELECT CONVERT(DATE, CONVERT(DATETIMEOFFSET, '2016-01-09 05:49:06.3744350 +00:00'))
Returns:
2016-01-09
You can use CAST function
SELECT CAST('2016-01-09 05:49:06.3744350 +00:00' as date)

converting to date time with only date and hour passed in

I am using SQL server, i want to insert datetime into my datetime column. The parameters i recieved as follows
#hour int,
#date varchar(10) -- format YYYY-MM-DD
How do I convert to this datetime format:
YYYY-MM-DD HH:MM:SS
MM will be 00 as we only consider the hour.
HH:MM will be in 24 hours format
Example :
#date = '2014-10-2'
#hour = '8'
Should be converted to
2014-10-2 08:00:00
How do I do this?
I consider that both the variables are of varchar type since you enclosed both the variables with quotes
SELECT CONVERT(DATETIME, #date + ' ' + convert(varchar(10),#hour) + ':00:00')
here is one another way to do this:-
declare #hour int=8,
#date varchar(10)='2014-10-2'
select dateadd(hh,#hour,CONVERT(datetime,#date))

How to extract date from datetime stored as a varchar

How can I convert the string 'Fri, 9 Mar 2012 10:43:21 +0000 (UTC)', stored as a varchar(max), to the datetime value '09-03-2012', which is the date part of the string?
Use convert and reference to this page http://msdn.microsoft.com/en-us/library/ms187928.aspx
e.g.
CONVERT(varchar(8), GETDATE(), 112)
According to MS: http://msdn.microsoft.com/en-us/library/ms187928.aspx
The CONVERT() function is a general function that converts an expression of one data type to another.
The CONVERT() function can be used to display date/time data in different formats.
and the table lists 105 as the code for the style you want.
So, for your example date:
CONVERT(VARCHAR(10), 'Fri, 9 Mar 2012 10:43:21 +0000 (UTC)', 105)
should probably do the trick.
I had to chop up your date first, but here goes:
DECLARE #ds varchar(30)='Fri, 9 Mar 2012 10:43:21 +0000 (UTC)';
SELECT CONVERT(VARCHAR(10)
,CAST(SUBSTRING(#ds
,CHARINDEX(' ',#ds)
,LEN(#ds)-(PATINDEX('%[0-9][0-9][0-9][0-9]%',#ds)+6))
AS DATETIME)
,105
)
Result:
09-03-2012
If you have SQL Server 2012, you can try this:
select CONVERT(date, TRY_PARSE('Fri, 9 Mar 2012 10:43:21 +0000' AS datetime2))
NOTE: (UTC) is removed, it didn't parse correctly.
Also, be aware if you need to localize that time value. For example, I live in AZ, so typically I just subtract -7 hrs from UTC times to localize. You will need to make similar adjustments based on the time zone