convert stringformat 1 Jan 22 to datet 2022-01-01 - sql

I receive data from one of our partners with a column named "strip" (not sure of that matters) of type varchar(MAX) that contains dates in the form of: 01 Jan 22 and I need to change it to a date in SQL Server.
This works:
CONVERT(DATETIME, strip, 102) as Date
This also works:
CONVERT(DATETIME, strip, 106) as Date
However these options no longer work when I try to use this in the WHERE statement with the error:
Conversion failed when converting date and/or time from character string.
I'm stumped to be honest.

Related

SQL Server date conversion not is formatting the output

My SQL statement is supposed to add 5 days to the date column and reformat the output to MM/DD/YYYY.
The calculation is working, but the format won't change, no matter what value I use.
The date column is defined as varchar(128) (I've used datetime and varchar(128) in the convert statement).
What am I doing wrong?
SELECT
FSI_Date, DATEADD("d", +5, CONVERT(DATE, FSI_Date, 101)) AS NEWDAY,
FROM
DB_test
FSI_Date NEWDAY
---------------------------
12/12/2015 2015-12-17
SQL Server will convert mm/dd/yyyy to a date, so, you just need the dateadd() and then convert back to your desired format.
Remove the final convert(...,101) if you want a natural date.
Example
Select FSI_Date
,NewDay = convert(varchar(10),DateAdd(DAY,5,#FSI_Date),101)
From DB_Test
Returns
FSI_Date NewDay
12/12/2015 12/17/2015

Date conversion in SQL Server throws error

I am trying to convert a date in mm/dd/yyyy format
select convert(date,'31/12/2013',101)
but I'm getting this error
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
How to do this? My system (Windows 7) has a dd-mm-yyyy format.. will system date format will have any impact on it?`
see
http://msdn.microsoft.com/en-us/library/ms187928.aspx
101 is the US style (mm/dd/yyyy)
try 103 (dd/mm/yyyy) which is British/French
SELECT CONVERT(DATE, '31/12/2013', 103)
Style 101 is the US style, so this has months first - your string represents the 12th day of the 31st month ....
What you need to use is style 103 (British/French) which uses the day first - so this string is 31st of December:
SELECT CONVERT(DATE, '31/12/2013', 103)
See the official MSDN SQL Server Books Online Documentation on CAST and CONVERT and what styles are defined and what they mean

Datetime interpretation issue

I have the following data:
StartDate FinishDate Details
09/10/2013 11/10/2013 xxx
14/10/2013 13/10/2014 Taking a year off
Whilst editing this data I which to check the date ranges do not overlap.
I am running an SQL query from access via ado to do this; I am putting the dates entered into database format (ie 'mm/dd/yyyy'); This is the query I've got:
SELECT Count(*)
FROM MarkerAbsence
WHERE PerID = 718 AND
('10/09/2013' BETWEEN StartDate AND FinishDate OR
'10/11/2013' BETWEEN StartDate AND FinishDate)
If the data is valid, it should return zero records; however it doesnt it returns 1 (being the second listed record above) and therefore seems to be interpreting '10/11/2013' as dd/mm/yyyy instead of mm/dd/yyyy.
Yet if I do this in SMO:
DECLARE #datevar datetime2 = '31/12/2008';
SELECT #datevar;
I get:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
While
DECLARE #datevar datetime2 = '12/31/2008';
SELECT #datevar;
returns
2008-12-31 00:00:00.0000000
So why am I having this problem and how do I fix it?
If you're running a query using MS Access, you need to delimit dates with # symbols, i.e.: #12/31/2008#. If this won't work for whatever reason, it is best to use string dates in the 'yyyy-mm-dd' format, as it will be recognized and is unambiguous.
You're probably getting this problem as MS is a US company, and the US uses mm/dd/yyyy format, so MS has defaulted much of their older software to treat dates as being in this format if at all possible, whereas you're probably in a country that uses - and have your PC's locality set to use - dd/mm/yyy format. Since not all of MS' software follows this rule, you have this problem.
The solution is to use a string date format that is unambiguous, such as: yyyy-mm-dd, mmm/dd/yyyy, or dd/mmm/yyyy (where mmm returns a three-letter month such as Dec).
You are using dd/mm/yyyy formats for your date strings. By default, without an explicit conversion, SQL is expecting date strings in the mm/dd/yyyy or yyyy-mm-dd format. So either change your strings to match one of these formats or do this:
SELECT Count(*)
FROM MarkerAbsence
WHERE PerID = 718 AND
(CONVERT(DATETIME, '10/09/2013', 103) BETWEEN StartDate AND FinishDate OR
CONVERT(DATETIME, '10/11/2013', 103) BETWEEN StartDate AND FinishDate)

SELECT CONVERT(VARCHAR(10), GETDATE(), 110) what is the meaning of 110 here?

When we convert or cast date in sql, see below sql code
SELECT CONVERT(VARCHAR(10), GETDATE(), 110) AS [MM-DD-YYYY]
it works fine, I just want to know the meaning of 110 in above code. what it does actually, sometimes we use 102, 112 etc. what is the use of that number.
That number indicates Date and Time Styles
You need to look at CAST and CONVERT (Transact-SQL). Here you can find the meaning of all these Date and Time Styles.
Styles with century (e.g. 100, 101 etc) means year will come in yyyy format. While styles without century (e.g. 1,7,10) means year will come in yy format.
You can also refer to SQL Server Date Formats. Here you can find all date formats with examples.
110 is the Style value for the date format.
TSQL Date and Time Styles
When you convert expressions from one type to another, in many cases there will be a need within a stored procedure or other routine to convert data from a datetime type to a varchar type. The Convert function is used for such things. The CONVERT() function can be used to display date/time data in various formats.
Syntax
CONVERT(data_type(length), expression, style)
Style - style values for datetime or smalldatetime conversion to character data. Add 100 to a style value to get a four-place year that includes the century (yyyy).
Example 1
take a style value 108 which defines the following format:
hh:mm:ss
Now use the above style in the following query:
select convert(varchar(20),GETDATE(),108)
Example 2
we use the style value 107 which defines the following format:
Mon dd, yy
Now use that style in the following query:
select convert(varchar(20),GETDATE(),107)
Similarly
style-106 for Day,Month,Year (26 Sep 2013)
style-6 for Day, Month, Year (26 Sep 13)
style-113 for Day,Month,Year, Timestamp (26 Sep 2013 14:11:53:300)
10 = mm-dd-yy
110 = mm-dd-yyyy
SQL Server CONVERT() Function

sql convert ignores format?

running :
SELECT CONVERT(VARCHAR(20), GETDATE(), 100)
will result :
Jan 4 2012 1:25PM
Ive been knowing this for years.
However , yesterday, while I was driving , I thought to my self :
Hey! I gave him the format of 103 which is for datetime format ,
But I didnt actually TOLD the convert that Im passing also a datetime
object ! ( e.g. getdate()))
So ive tried :
SELECT CONVERT(VARCHAR(20), 'lalala', 100)
And the result was :
lalala
so , now Im trying to convert 'lalala' to string via datetime format (103).
which is ( by logic) should give me exception.
but it is not.
Any reasonable explanation ?
CONVERT takes into account both the target type and the expression's type to see whether the third argument should be considered. If you are converting a datetime to a string or the other way round, the third argument is considered. In your example, a string is converted to a string and so the third argument is ignored.
Consider this:
SELECT 1, CONVERT(datetime, '03/01/2011', 103)
SELECT 2, CONVERT(datetime, '03/01/2011', 101)
SELECT 3, CONVERT(varchar, '03/01/2011', 103)
SELECT 4, CONVERT(varchar, '03/01/2011', 101)
Here are the results:
--- -----------------------
1 2011-01-03 00:00:00.000
--- -----------------------
2 2011-03-01 00:00:00.000
--- ------------------------------
3 03/01/2011
--- ------------------------------
4 03/01/2011
As you can see, the last two ‘conversions’ didn't change anything, because both the source and the target type were varchar, even though the expression did look like a date/datetime.
Reference:
CAST and CONVERT (Transact-SQL)
The convert function only uses the 3rd parameter (style) if the 2nd one (data_to_be_converted) is datetime.
If it's already varchar it returns the value and don't make anything.
The MSDN documentation for CONVERT says that the style parameter is interpreted based on the type of the value passed - so I guess it inspects the value and can tell which type it is.
For example, notice that a style of '1' means "mm/dd/yy" for datetimes and "Always 8 digits. Always use in scientific notation" for float values.