I have a problem in converting sql server 2008 datetime to varchar,
Select convert(varchar(20),convert(datetime, '2013-12-11 00:59:00.000'))
the result is Dec 11 2013 12:59AM but I need it to be actually Dec 11 2013 00:59AM as in the database dates are of 24-Hour format.
How can I correct the query?
There are two functions, cast and convert that you can use.
http://msdn.microsoft.com/en-us/library/ms187928.aspx
You forgot to put in the format style.
-- Using cast
Select cast('2013-12-11 00:59:00.000' as varchar(20)) as my_casted_date
-- Using convert
Select convert(varchar(24), '2013-12-11 00:59:00.000', 113) as my_converted_date
Use format with custom date time strings for utlitmate flexibility.
http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
-- Using format
DECLARE #my_date datetime2 = '2013-12-11 00:59:00.000';
SELECT format(#my_date, 'MMM dd yyyy HH:MM tt', 'en-us') as str_english_date
Solution that will work with SQL Server 2008.
-- Create a date time variable
DECLARE #my_date DATETIME2 = '2013-12-11 00:59:00.000';
-- Using convert
SELECT
CONVERT(varchar(24), #my_date, 113) +
CASE
WHEN DATEPART(HH, #my_date) < 12 THEN ' AM'
ELSE ' PM'
END
AS my_converted_date;
This will be perfect for you..
declare #dt datetime
set #dt='12-Jan-2014 23:59'
SELECT Right(CONVERT(VARCHAR, #dt, 100),7) AS DateTime_In_12h_Format
It's not nice but it does its job:
DECLARE #Var DATETIME;
SET #Var = '2013-12-11T00:59:00.000';
SELECT
#Var AS SourceValue,
CONVERT(VARCHAR(50), #Var, 100) AS FormatedValue1,
CASE
WHEN CONVERT(VARCHAR(50), #Var, 100) LIKE '%[ ]12:__AM' THEN REPLACE(CONVERT(VARCHAR(50), #Var, 100), ' 12:', ' 00:')
ELSE CONVERT(VARCHAR(50), #Var, 100)
END AS FormatedValue2
SourceValue FormatedValue1 FormatedValue2
----------------------- ------------------- -------------------
2013-12-11 00:59:00.000 Dec 11 2013 12:59AM Dec 11 2013 00:59AM
Please try this
Select convert(varchar(20),convert(datetime, '2013-12-11 00:59:00.000'),113) + ' ' +RIGHT(CONVERT(VARCHAR(26), Convert(datetime,'2013-12-11 00:59:00.000'), 109), 2)
Output
11 Dec 2013 00:59:00 AM
Fiddle Demo
Related
I have a varchar column which has values like "Aug 07 2017, 04:14 AM,EDT".
I need to convert this to a date column so that its possible to take the maximum value of the date.
I tried this:
select CONVERT(datetime, #dateField, 108)
from table
But I am getting the following error:
Conversion failed when converting date and/or time from character string.
You can just use left() and convert():
select convert(date, left('Aug 07 2017, 04:14 AM,EDT', 11))
If you want a datetime then convert the date and time separately, then:
select ( convert(datetime,
left('Aug 07 2017, 04:14 AM,EDT', 11)
) +
convert(datetime,
convert(time,
substring('Aug 07 2017, 04:14 AM,EDT', 14, 8)
)
)
)
Note: This is not taking the time zone into account.
Better Datetime solution
DECLARE #dateField AS NVARCHAR(50) = 'Aug 07 2017, 04:14,EDT'
-- Get index of last comma
DECLARE #validDateField AS NVARCHAR(20) = REPLACE(LEFT(#dateField, LEN(#dateField)- CHARINDEX(',', reverse(#dateField))), ',','')
SELECT CONVERT(DATETIME, #validDateField, 108)
Here is a solution:
select CONVERT(datetime, REPLACE(LEFT(#dateField, LEN(#dateField) - 3),',','') ,108 )
I have varchar date in this format:
03/13/2015 : 2130
and i would like to convert it into datetime something like this:
2015-03-13 21:30:00.000
i have seen example like this but did not work for what i am looking for
DECLARE #Date char(8)
set #Date='12312009'
SELECT CONVERT(datetime,RIGHT(#Date,4)+LEFT(#Date,2)+SUBSTRING(#Date,3,2))
This will work assuming all date times parts are padded with 0's consistently.
DECLARE #Input VARCHAR(50);
SET #Input = '03/13/2015 : 2130';
SET #Input = LEFT(#Input, 10) + ' ' + LEFT(RIGHT(#Input, 4), 2) + ':' + RIGHT(RIGHT(#Input, 4), 2);
PRINT #Input;
PRINT CONVERT(DATETIME, #Input);
PRINT CONVERT(VARCHAR(50), CONVERT(DATETIME, #Input), 121);
Output:
03/13/2015 21:30
Mar 13 2015 9:30PM
2015-03-13 21:30:00.000
OP wants mmddyy and a plain convert will not work for that:
select convert(datetime,'12312009')
Msg 242, Level 16, State 3, Line 1
The conversion of a char data type to a datetime data type resulted in
an out-of-range datetime value
so try this:
DECLARE #Date char(8)
set #Date='12312009'
SELECT CONVERT(datetime,RIGHT(#Date,4)+LEFT(#Date,2)+SUBSTRING(#Date,3,2))
OUTPUT:
2009-12-31 00:00:00.000
(1 row(s) affected)
I think this is what you're looking for:
DECLARE #Date VARCHAR(20)
SET #Date = '03/13/2015 : 2130'
-- Format the date string
SET #Date = LEFT(#Date, 10) + ' ' + SUBSTRING(#Date, 14, 2) + ':' + SUBSTRING(#Date, 16, 2)
-- convert to date
Select CONVERT(varchar, CONVERT(DATETIME, #Date), 121)
SQL Fiddle
More Info
Sql Function:
CONVERT(data_type(length),expression,style)
you can try this:
CONVERT(datetime,#varCharDate,121)
For more see this link
If convert is not working for you then you can use mid to take date, month, year etc. And then use str_to_date to construct datetime in desired format.
In oracle use to_date and this stackoverflow link for taking substring
I have a table which has a datetime column. I want to show date in date-month-year forma.I am using SQL Server 2008.
You need to convert datetime column
select convert(varchar,datecolumn,103) from yourtable
Some datetime convertions:
SELECT convert(datetime, '10/23/2016', 101) -- mm/dd/yyyy
SELECT convert(datetime, '2016.10.23', 102) -- yyyy.mm.dd
SELECT convert(datetime, '23/10/2016', 103) -- dd/mm/yyyy
SELECT convert(datetime, '23.10.2016', 104) -- dd.mm.yyyy
SELECT convert(datetime, '23-10-2016', 105) -- dd-mm-yyyy
SELECT CONVERT(VARCHAR(10), GETDATE(), 111) AS [YYYY/MM/DD]
/* YYYY/MM/DD
2015/07/11 */
SELECT CONVERT(VARCHAR(10), GETDATE(), 112) AS [YYYYMMDD]
/* YYYYMMDD
20150711 */
-- SQL convert date string to datetime - time set to 00:00:00.000 or 12:00AM
PRINT CONVERT(datetime,'07-10-2012',110) -- Jul 10 2012 12:00AM
PRINT CONVERT(datetime,'2012/07/10',111) -- Jul 10 2012 12:00AM
PRINT CONVERT(datetime,'20120710', 112) -- Jul 10 2012
You can learn all DateTime convertion from here
FORMAT can be used for this;
SELECT FORMAT(GETDATE(), 'dd-MM-yyyy') --20-09-2021
in your case;
SELECT FORMAT(datetime, 'dd-MM-yyyy') AS datetime --20-09-2021
Edit;
Sorry to inform that I've just seen you mentioned SQL Server 2008 this code works for SQL Server 2012 for those who use 2012 and ends up in here!
How can I display a DATETIME value (2010-12-02 15:20:17.000) as 02/12-2010 15:20?
For SQL Server:
select stuff(convert(varchar, getdate(), 105), 3, 1, '/') + ' ' + left(convert(varchar, getdate(), 8), 5)
DateTime is a DateTime is a DateTime - it just holds a date and time and doesn't have any string representation, really.
See the CAST and CONVERT topic in the SQL Server Books Online for details - it shows all supported date formats that SQL Server supports.
For your source format (2010-12-02 15:20:17.000) you could probably use style no. 121
DECLARE #source VARCHAR(50)
SET #source = '2010-12-02 15:20:17.000'
DECLARE #Date DATETIME
SELECT #Date = CONVERT(DATETIME, #source, 121)
SELECT #Date
but your target format is a bit odd..... I don't see any "out of the box" style that would match your needs. You'll need to use some string manipulation code to get that exact format.
Use MSSQL's build-in function to convert datetime to string with format,
SELECT CONVERT(VARCHAR(8), GETDATE(), 1) AS [MM/DD/YY] --2/5/12
SELECT CONVERT(VARCHAR(10), GETDATE(), 103) AS [DD/MM/YYYY] --5/2/2012
You need to create custom function to get various format to use like this;
SELECT dbo.ufn_FormatDateTime(GETDATE(),'YYYY-MM-DD HH:mm:SS tt')
--Output : 2012-02-05 01:58:38 AM
SELECT dbo.ufn_FormatDateTime(GETDATE(),'(dddd) mmmm dd, yyyy hh:mm:ss.fff tt')
--Output : (Sunday) February 05, 2012 01:58:38.723 AM
SELECT dbo.ufn_FormatDateTime(GETDATE(),'dd/MM/yyyy')
--Output : 05/02/2012
SELECT dbo.ufn_FormatDateTime(GETDATE(),'yyyy MMM, dd (ddd) hh:mm:ss tt')
-- Output : 2012 Feb, 05 (Sun) 01:58:38 AM
Get the code snippet from this link.
http://www.tainyan.com/codesnippets/entry-62/sql-server-date-time-format-function.html
http://msdn.microsoft.com/en-us/library/ms189491.aspx
Is this what you're looking for?
Assuming Oracle:
select TO_CHAR(SYSDATE, "dd/mm-yyyy HH24:mi")
from DUAL;
Assuming SQL Server:
select STR(DATEPART(DAY, GETDATE()), 2)
+ '/'
+ STR(DATEPART(MONTH, GETDATE()), 2)
+ '-'
+ STR(DATEPART(YEAR, GETDATE()), 4)
+ ' '
+ STR(DATEPART(HOUR, GETDATE()), 2)
+ ':'
+ STR(DATEPART(MINUTE, GETDATE()), 2);
Little example I use for Germany and Switzerland: dd.mm.yyyy hh:mm
SELECT CONVERT(varchar, GETDATE(), 104) + ' ' + LEFT(CONVERT(varchar, GETDATE(), 108), 5)
MySQL has a function called STR_TO_DATE, that converts a string to date.
Question:
Is there a similar function in SQL Server?
If you need to parse a particular format, use CONVERT(datetime, #mystring, #format). Use this as a reference: https://web.archive.org/web/20200729210252/http://www.sqlusa.com/bestpractices/datetimeconversion/
Some examples:
SELECT convert(datetime, '10/23/2016', 101) -- mm/dd/yyyy
SELECT convert(datetime, '2016.10.23', 102) -- yyyy.mm.dd ANSI date with century
SELECT convert(datetime, '23/10/2016', 103) -- dd/mm/yyyy
SELECT convert(datetime, '23.10.2016', 104) -- dd.mm.yyyy
SELECT convert(datetime, '23-10-2016', 105) -- dd-mm-yyyy
-- mon types are nondeterministic conversions, dependent on language setting
SELECT convert(datetime, '23 OCT 2016', 106) -- dd mon yyyy
SELECT convert(datetime, 'Oct 23, 2016', 107) -- mon dd, yyyy
-- 2016-10-23 00:00:00.000
SELECT convert(datetime, '20:10:44', 108) -- hh:mm:ss
-- 1900-01-01 20:10:44.000
What if the string is 7/7/2010?
Then use CONVERT with either 101 (mm/dd/yy) or 103 (dd/mm/yy) depending on what you want:
SELECT CONVERT(DATE, '7/7/2010', 103)
Result:
2010-07-07
CAST(<string> AS DATETIME)
Use CAST.
declare #MyString varchar(10)
declare #MyDate datetime
set #MyString = '2010-08-19'
set #MyDate = cast(#MyString as datetime)
select #MyDate
Here is a good example:
declare #myDate datetime
set #myDate = '06/09/2017'
select concat(convert(varchar(20), #myDate,101), ' -- ',
convert(varchar(20), #myDate,103), ' -- ',
convert(varchar(20), #myDate,6))
This is what you get, depending on 101 or 103 or 6:
09/06/2017 -- 06/09/2017 -- 06 Sep 17
A good summary of types of dates is here - https://www.w3schools.com/sql/func_convert.asp
On MSSQL:
select cast('2012/06/12 10:32AM' as datetime);
You will get it:
2012-06-12 10:32:00.000