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);
Related
I have a little task to do and I need to get ddmmyyyy format for my DateTime values in SQL Server.
I tried to do it with this code
SELECT convert(varchar, getdate(), 104)
and it works fine, but it returns dd.mm.yyyy or 01.11.2021. I need it to be 01112021 but I can't achieve this yet.
I am using this link for getting datetime format codes but I can't find any code that really helps me achieve my goal.
There's no exact match for the required format you need - you can either use
SELECT REPLACE(CONVERT(varchar(20), SYSDATETIME(), 104), '.', '')
(use what you had and just strip out the . after first step of formatting)
or you can use FORMAT which is available in SQL Server 2012 and newer:
SELECT FORMAT(SYSDATETIME(), 'ddMMyyyy')
There is no defined format that fulfills your request. However, you can use the 'Replace' function to strip your value from date separators.
select replace(convert(varchar, getdate(),104),'.','')
OR use the newer FORMAT function (SQL server 2012+)
SELECT FORMAT(getdate(), 'ddMMyyyy')
You can use replace function to complete it.
select replace(convert(varchar, getdate(), 104),'.','')
I want my date to show as MM.dd.yyyy. But in SQL Server there is no such format to format your date in this format. What I can do is:
select replace(convert(varchar(10), getdate(), 101), '/', '.')
It will give me the expected output. But my question is: can we create a new format all together to display this kind of data without replace?
We have a dateformat options from 0 to 131 in SQL Server. Can we create new format like 132 in SQL Server?
You can use SQL Server format() function.
select format(getdate(), 'MM.dd.yyyy') as date
I want get date from sql server in user specified format using GETDATE() function.
if i give this query
select GETDATE()
then it is displating output date in this format
2015-03-17 07:29:58.377
but i want output like this .
2015-03-17
what statement should be added with query to get the result.
help me from this problem.
Just use convert():
select convert(varchar(10), getdate(), 121)
Just have a look to the following link. which provides more conversion options.
https://msdn.microsoft.com/en-us/library/ms187928.aspx
In SQL Server 2012 onward you may use FORMAT() which is a bit more intuitive than recalling style numbers.
e.g.
select FORMAT( GETDATE() , 'yyyy-MM-dd' )
see: https://msdn.microsoft.com/en-AU/library/hh213505.aspx
however do note the second parameter is case sensitive
'yyyy-MM-dd' works
'YYYY-MM-DD' would only work for the MM
In Oracle, you can use:
SELECT to_char(sysdate, 'yyyy-mm') FROM dual;
to show just the year and month portion of a date. Additionally, you can set NLS_DATE_FORMAT to change the way dates are returned by default.
I know in SQL Server you have a set of predefined options you can use:
SELECT convert(varchar, getdate(), 110) –- mm-dd-yyyy
SELECT convert(varchar, getdate(), 111) –- yyyy/mm/dd
But is there an option that gives me the same freedom as Oracle's to_char? Preferably one that doesn't require me to create a custom function.
Currently the best option is to have a CLR function that is a wrapper to .NET's DateTime.ToString(string format).
If you don't want a separate function, you can build the required string from pieces:
YEAR(#d) + '-' + MONTH(#d) + '-' + DAY(#d)
As for the definite solution, there will be a formatting function in the next version of SQL Server.
It depends on what version of SQL Server you are using.
SQL Server 2005, 2008, and 2008 R2
For these versions you would need to make use of SQLCLR to expose that functionality from .NET. You can either write your own SQLCLR function using the DateTime class (as noted in #GSerg's answer), or you can simply download and install the Free version of SQL# (which I am the author of, but the Date_Format function is free).
Example:
SELECT SQL#.Date_Format(GETDATE(), 'dddd in MMMM', '') AS [Default_language],
SQL#.Date_Format(GETDATE(), 'dddd in MMMM', 'he') AS [Hebrew],
SQL#.Date_Format(GETDATE(), 'dddd in MMMM', 'de') AS [German];
Returns:
Default_language Hebrew German
Monday in January יום שני in ינואר Montag in Januar
SQL Server 2012 and newer
Use the built-in FORMAT function.
Example:
SELECT FORMAT(GETDATE(), 'dddd in MMMM') AS [Default_language],
FORMAT(GETDATE(), 'dddd in MMMM', 'he') AS [Hebrew],
FORMAT(GETDATE(), 'dddd in MMMM', 'de') AS [German];
Returns:
Default_language Hebrew German
Monday in January יום שני in ינואר Montag in Januar
For both SQLCLR and built-in FORMAT options
The following two MSDN pages detail the available formatting options:
Custom Date and Time Format Strings
Standard Date and Time Format Strings
I want format like
17-April-2011 9:05 PM
How to do this in sql query
Use the CONVERT or CAST function (see documentation) which offer a number of formats. But be careful with what you do with it. This is fine for displaying, but could result in inefficient queries if you use the result in a query expression.
And if you are using it for display, you will probably have better formatting options in the language you are using at the UI level.
Try CONVERT(varchar(50), datecol, 130) - format 130 seems closest and replace some spaces?
The closest you can get from the built-in CONVERT function is 17 Apr 2011 9:05:00:000PM:
SELECT convert(varchar, getdate(), 130)
See also:
How to format datetime & date in Sql Server 2005 by Anubhav Goyal
CAST and CONVERT (Transact-SQL) on MSDN
113 is also similar:
SELECT convert(varchar, getdate(), 113)