If I use this
SELECT CONVERT(DATE, '26/03/2014', 101)
I get an error:
Conversion failed when converting date and/or time from character string.
But if I use this
SELECT CONVERT(DATE, '26/03/2014', 103)
There's no error and this is the result returned:
2014-03-26
I don't understand why the first code is not working, as far as I searched and understand is that 101 is for US date and 103 is for UK/French Date.
This:
SELECT CONVERT(DATE, '26/03/2014', 101)
will be interpreted in the US way (mm/dd/yyyy) : the 26th month, 3rd day of 2014 - this obviously fails (no 26th month).
This however:
SELECT CONVERT(DATE, '26/03/2014', 103)
will be interpreted the European way (dd/mm/yyyy): the 26th day of the 3rd month (March) of 2014.
You need to very careful with parsing strings to date! Check out all the defined styles for CONVERT here
If you want to be sure it works always, use the ISO-8601 format: YYYYMMDD or in your case:
SELECT CAST('20140326' AS DATE)
will always work, no matter what language/regional settings you have
Thats becuase of the format specifier(101) which you are using.
101 is mm/dd/yyyy
So 26 cannot be a month. Hence resulting in error.
103 is dd/mm/yy
And hence it is working correctly. if the day would have been less than 13, it would have taken it as month and there would be logical error.
The convert signature is as follows
CONVERT(data_type(length),expression,style)
for the date conversion, the styles are as follows
101 mm/dd/yy USA
103 dd/mm/yy British/French
more formats here http://www.w3schools.com/sql/func_convert.asp
Use language neutral date representations for literals. In case with style 101, SQL Server assumed the MM/dd/yyyy instead of dd/MM/yyyy.
Here's a nice link with more info from MVP Tibor Karaszi:
http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes
Related
I'm doing the date conversion the following way:
CONVERT(VARCHAR, CONVERT(DATE, ZA2_DTFIM), 103) AS DATA
How can I add 5 days to that system date?
I tried several methods but so far nothing, I'm a beginner in SQL Server.
In TSQL the function GETDATE() provides the system date (and time).
To add 5 days to that use DATEADD(day,5,GETDATE())
To display this as a string you can use FORMAT() or CONVERT() e.g. both of these will add 5 days to the system date and then display that in day/month/year style:
select
convert(varchar,dateadd(day,5,getdate()),103)
, format(dateadd(day,5,getdate()),'dd/MM/yyyy')
Note: you do not need to convert to date because converting (or formatting) GETDATE() to dd/MM/yyyy will suppress display of time anyway.
I have a SQL Server date in the format of dd/mm/yy in a varchar column. I am trying to convert this string to date but getting
Msg 241, Conversion failed when converting date/time from character string.
I have tried CAST(), TRY_CAST(), TRY_CONVERT(), CONVERT, PARSE(), TRY_PRASE(). None of them is working.
This is my code:
DECLARE #d varchar(50) = '13/09/22'
SELECT TRY_CONVERT(datetime, #d, 103) -- I have tried different formats to no avail.
Also I have tried casting as below
SELECT TRY_CAST(#d AS datetime) -- Tried datetime2 as well, but all failing.
If I change the day part to < 12, the conversions work. Which means SQL Server is somehow interpreting this as mm/dd/yy?
Thanks
Actually CONVERT() works if you use the correct mask:
SELECT CONVERT(datetime, '13/09/22', 3); -- 2022-09-13 00:00:00.000
The problem is that mask 103 assumes a 4 digit year, not a 2 digit year. For a 2 digit year date in dd/mm/yy format, use mask 3.
I need to convert a string to a valid date format
I have tried using cast and convert functions to no avail.
Convert( mystring, mm/dd/yy)
I have got a string like: Tuesday, July 09, 2019 12:00 AM to get the output mm/dd/yy . I am not able to get this work.
Tuesday, July 09, 2019 12:00 AM---> mm/dd/yy
Try this:
SELECT TRY_PARSE('Tuesday, July 09, 2019 12:00 AM' AS DATETIME USING 'en-US')
to convert to valid date.
Then:
SELECT CONVERT(VARCHAR(10), TRY_PARSE('Tuesday, July 09, 2019 12:00 AM' AS DATETIME USING 'en-US'), 101)
to convert it to mm/dd/yy.
The most important hint was provided by Larnu as a comment: Do not store date as string, rather use the native date formats and think about the string format in output scenarios only.
So hopefully you are trying to fix this technical issue and therefore you need this conversion in order to store the values in neatly typed columns ;-)
The suggestion by gotqn is the best, as you obviously need to use a specific language/culture and TRY_PARSE is the only approach which allows to specify this parameter. TRY_PARSE will need at least v2012 and - this might be a draw back, it is really slow.
So, if this is a one-way-ticket (just one conversion in order to store your values correctly) and you have a very large count of rows, you might go this route:
DECLARE #yourstring VARCHAR(100)='Tuesday, July 09, 2019 12:00 AM';
SET LANGUAGE ENGLISH;
SELECT CAST(SUBSTRING(#yourstring,CHARINDEX(',',#yourstring)+1,100) AS datetime);
The simple idea is: Cut away the leading Weekday's name. The rest is working implitly.
I'm using this query
SELECT convert(nvarchar(MAX), GETDATE(), 22) AS Date
Result: 08/05/16 12:23:08 PM
But I want result like this 8/5/2016 12:23:08 PM
dd/mm/yyyy hh:mm:ss a
As of SQL Server 2012 the FORMAT function is available allowing you to specify the format of data types and is locale-aware so it will consider date formatting in relation to the session's language or optional culture parameter.
You can achieve your custom formatting like so: FORMAT(GETDATE(), 'd/M/yyyy hh:mm:ss tt')
Note your requested format dd/mm/yyyy hh:mm:ss a is incorrect as in the case of single digits you want to remove zero padding i.e. 10/8/2016 not 10/08/2016. That's why in the format string I use only d and M.
Also, pay attention to #GarethD comment about the cost on larger datasets.
You could use the FORMAT function in T-SQL : https://msdn.microsoft.com/en-us/library/hh213505(v=sql.120).aspx
Here is the code :
SELECT FORMAT(GETDATE(), 'd/M/yyyy hh:mm:ss tt')
I am trying to display the previous day's date Sybase using a select query:
select dateadd(day,-1,convert(char(10), getdate(), 23))
this query displays as 2015-06-18 00:00:00.0
I expect the output to be 2015-06-18.
How can I get that?
Try select dateadd(day,-1,convert(Date, getdate(), 365))
Try select convert(char(10),dateadd(day,-1, getdate() ), 23 )
Dateadd expects a date parameter as the third argument. In your example you're feeding it a char(10) . Even though implicit conversion from Char->DateTime is supported in Sybase, I would not code to depend on it in this case.
Well, datetime is a binary type. How it is formatted for display is up to you.
getdate() returns a datetime representing the current date/time. And dateadd() returns a datetime or date value, depending on what it started with (in your case, that would be datetime). And when you run your select statement, it's getting converted to a string using the default format configured for your Sybase instance. Hence your results.
In a nutshell, you are:
Converting the datetime value to char(10) to get an ISO 8601 format date string (yyyy-mm-dd).
Converting that back to a datetime value (so the time component is start-of-day)
Subtracting one day.
The easiest way to get what you want (yesterday's date) is this:
dateadd(day,-1, convert(date,getdate()) )
Which, when formatted for display, will come out as something like (depending on the default format configured for your Sybase instance) yyyy-mm-dd.
Or it might come out like November 29, 2015. If you want to ensure that it is an ISO 8601 date representation, you'll need to be explicit about it and cast it a char or varchar, thus:
convert(char(10) , dateadd(day,-1, convert(date,getdate()) ) , 23 )
which leaves you with a char(10) value containing yesterday's date.
If your version of Sybase doesn't support date, you'll have to fall back to what you were doing, but something like this:
convert(char(10) , dateadd(day,-1, getdate() ) , 23 )
You are telling it to give you hh:mm:ss, so that's what you are getting.
The 23 inside the convert is the format code for yyyy-mm-ddTHH:mm:ss There is no code to get yyyy-mm-dd, the closest you can get is 105 (dd-mm-yy) or 110 (mm-yy-dd).
If you need yyyy-mm-dd, then you'll have to convert the date to a string(char or varchar), and truncate the parts you don't want.
Converting Datetime