I have an Oracle SQL script that I'm converting to run in MS SQL Server. The script has the to_date function in it and uses the RR date format. Here's the function:
to_date('07-AUG-14','DD-MON-RR')
I'm going to use the MS SQL Server function CONVERT like this
CONVERT(DATETIME, '07-AUG-14', num)
where num is the code of the format I need.
What code should I use in MS SQL Server to get the same type of functionality as the Oracle RR format?
For a U.S. datetime conversion, you can use:
CONVERT(DATETIME, '07-AUG-14', 10)
-- ^^
For other cultures, style (the 3rd argument) will differ; but style needs to be a Without century (yy) value for your purpose.
The semantics of Oracle's RR date format apply to SQL Server's yy date styles too. CAST and CONVERT (Transact-SQL) (for SQL Server 2008) on MSDN explains...
By default, SQL Server interprets two-digit years based on a cutoff year of 2049. That is, the two-digit year 49 is interpreted as 2049 and the two-digit year 50 is interpreted as 1950.
...and lists the numeric values for the Without century (yy) date styles.
(For reference, another SO answer explains the equivalent semantics of Oracle's RR date format.)
You can more broadly confirm the default semantics of SQL Server's (U.S.) yy date style with the following queries...
SELECT CONVERT(DATETIME, '07-AUG-14', 10)
SELECT CONVERT(DATETIME, '07-AUG-49', 10)
SELECT CONVERT(DATETIME, '07-AUG-50', 10)
SELECT CONVERT(DATETIME, '07-AUG-51', 10)
..., which yield...
2014-08-07 00:00:00.000 -- This century assumed.
2049-08-07 00:00:00.000 -- This century assumed.
1950-08-07 00:00:00.000 -- Last century assumed.
1951-08-07 00:00:00.000 -- Last century assumed.
...with a default two-digit year cutoff configuration. (yy date styles for other cultures should behave similarly.)
Related
I'm importing a CSV file and one of the fields is a non-standard date string with entries like 7-Dec-2021
Any ideas for to convert this into a DATETIME object that I can insert into my SQL table? Standard CAST/CONVERT didn't work.
Microsoft SQL Server 2012 - 11.0.2100.60
TRY_CONVERT seems to be working here:
SELECT TRY_CONVERT(datetime, '7-Dec-2021'); -- 2021-12-07 00:00:00.000
Edit:
Your input date almost matches format mask 106, once we replace the dashes with spaces. Consider this solution:
SELECT dt, CONVERT(datetime, REPLACE(dt, '-', ' '), 106) AS dt_out
FROM yourTable;
This outputs 2021-12-07 00:00:00.000 for dt_out on SQL Server 2014, and should behave the same way on SQL Server 2012.
I am guessing that you are using a LOGIN where its language isn't English based, and as a result 'Dec' (and/or other months) isn't recognised as a valid month name.
You can specify your language to be used for the batch and then CONVERT:
SET LANGUAGE BRITISH;
SELECT TRY_CONVERT(date,'7-Dec-2021',106);
I have table where the date field (TimeStringID ) is in numerical format represented as the number of seconds that have past since 12:00am March 1,1980.
In SQL Server the command is DATEADD(s, CAST(TimeStringID AS numeric), '1980-03-01') AS 'StartTime'
The field 1289260817.0000000501 converts to 2021-01-07 00:00:17.000 using the above SQL Server statement.
How would I convert this for Access queries?
I believe this is the equivalent:
select dateadd("s", TimeStringId, #03/01/1980#)
Today I wrote two queries on Datepart() and get different returns below
Query #1:
Select Datepart(day,'2015-07-05')
returns '5', which I expected.
Query #2:
Select Datepart(day, 2015-07-05)
Returns '27', which is a little bit funny, and I don't understand how 27 is being returned.
The difference between these two queries is one with the date inside ' ', and the other without.
Anybody can help me out here?
2015-07-05 is just a mathematical expression which adds up to the integer 2003. (Subtracting 7 from 2015 gives 2008 then subtract 5)
2003 evaluates to '1905-06-27' when implicitly cast to datetime as casting int to datetime works the same as adding that number of days to the base date of 1 Jan 1900 (i.e. equals DATEADD(DAY, 2003,'19000101')).
So this is where the 27 comes from.
The correct way to denote date literals in SQL Server is as a string '2015-07-05' (ISO format - unambiguous for newer datetime datetypes) or '20150705' (unambiguous for legacy datatypes) or using the ODBC format { d '2015-07-05' }.
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)
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