Converting date 24-NOV-49 to yyyy-mm-dd format - sql

I am trying to convert the date 24-NOV-49 to 1949-11-24.
This is the code I am currently using:
TRY_CONVERT(DATE, NULLIF(GEB_DATUM,''), 103)
Unfortunately this is giving me wrong conversions:
DATE WRONG CONVERSION SHOULD BE
24-NOV-49 2049-11-24 1949-11-24
01-MEI-62 NULL 1962-05-62
05-DEC-71 NULL 1962-12-05

The default cut-off on two digit years for SQL Server is 50 so anything before this is moved to the current century and anything after into the previous one.
You can change the default and instructions are here
More likely though you'll need to handle it manually, break the string into year, month and day, apply logic to get the right numerical values and then build it up again with DATEFROMPARTS.

You may configure the Server properties of the-two-digit-year-cutoff-server-configuration-option as below:
Then try execute the below statements:
SELECT CONVERT(VARCHAR, CONVERT(DATE,ISNULL('24-NOV-49','')),23);
SELECT CONVERT(VARCHAR, CONVERT(DATE,ISNULL('05-DEC-71','')),23);
SELECT CONVERT(VARCHAR, CONVERT(DATE,ISNULL('01-MAY-62','')),23);
Ref: https://learn.microsoft.com/en-us/sql/database-engine/configure-windows/configure-the-two-digit-year-cutoff-server-configuration-option?view=sql-server-2017

Related

Parse values of different type to date

I'm trying to parse certain columns into a date format and have had success doing so, but have run into an error due to inconsistency that lies in the data.
My date columns are currently integers in the form of YYYYMMDD, so I've used the following in the select statement to parse into dates:
CONVERT(datetime, CAST(date_column AS CHAR(8)), 112)
This works as expected, transforming my data into a YYYY-MM-DD format.
I run into the following error though:
Conversion failed when converting date and/or time from character
string.
After looking through the data a bit, it turns out I have some cases of inconsistent data values, such as -1 and 10630 instead of the expected YYYYMMDD value.
Do I just need to add a WHERE statement to only apply CONVERT and CAST to YYYYMMDD fields while filtering out fields with bad data? If so, how would I do this, or is there a better way?
Thanks in advance
Let me assume you are using SQL Server, based on the syntax of your SQL. You shouldn't actually need the 112. 'YYYYMMDD' is the default date format for SQL Server.
In any case, you can use TRY_CONVERT():
TRY_CONVERT(datetime, CAST(date_column AS CHAR(8)), 112)
This returns NULL if the value cannot be converted. You can find the offending values using:
select date_column
from t
where try_convert(datetime, cast(date_column as char(8)) is null and
date_column is not null;
Use Parse() or Try_parse()
select parse('22-JAN-1989' as date)
select parse('04-March-1992' as date)
select parse('03/10/2020' as date)
select parse('07-05-1958' as date)
select parse('Aug 25,2016' as date)
Every example above returns a valid date.
You can also add USING 'en-US' or such if you want a different culture.
If you are getting numeric values, and you know the starting date, use
select IsNull(try_parse('16500' as date),dateadd(d,cast('16500' as int),'1/1/1980'))

Trying to convert SQL getdate into varchar with no in-between characters

I have a list of auto-generated tables in MS-SQLServer that differ only in the date listed at the end of the name.
The challenge is that I want to create a script that always references a table 'x days back'.
So for example if the table name would be:
dbo.tablename_20200825
I can get "close" to the date format I need as a string with the following statement and style
select convert(varchar(10), getdate(), 102);
However I still have those periods of separation between each part of the date.
How do I make the resulting string appear as '20200825' instead of '2020.08.25'
Thank you as always for any insight and help.
I think you're looking for 112
select convert(varchar(10), getdate(), 112);
Results
20200825
You can use format() to get exactly what you want:
select format(getdate(), 'yyyyMMdd')

How can I switch the Month and Day when the column format is M/D/Y?

I am not sure if this has already been answered but I could not find anything.
I am trying to convert the column of dates from MM/DD/YYYY to DD/MM/YYYY.
I must confess that I don't even know where to begin on this. I was thinking I might try an update statement but I am unsure as to how to format it. Any help would be appreciated.
You need to do two level of conversation :
select convert(varchar(12), convert(date, datecol, 101), 103)
In general, you need to fix the data-type (if that is in your hand) instead of doing conversation in SQL. Date formatting should be done at the presentation layer.
You can try this by fixing the data type in the actual table. For displaying purpose either on a webpage, reports or whatever.
SQL Server provides a number of options you can use to format a date/time string. In select, you can try one of the suggested methods as here.
For all these conversions you need to pass the date values in proper data type which may be the date or date-time.
Here is one of the examples of your illustration.
declare #DateInString varchar(20) = Cast(getdate() as Varchar(20))
select convert(varchar(12), convert(date, #DateInString, 101), 103)
You could also do Select Format(datecol, 'dd/MM/yyyy')
This will return your datetime field as a varchar - you should then be able to insert that into your target.

SQL Server Compare Month-Day portion of date

I'm trying to compare 2 people and see whose birthday falls earlier in the year. I'm trying to hack together a solution where I take the mm-dd from a datetime field using RIGHT(DOB, 5) and concatenating it with '1900' to create a new date field and use that for comparison.
CONVERT(DATE,'1900-' + RIGHT(DOB,5),126) DOB
However I'm getting the following error:
Conversion failed when converting date and/or time from character string.
Any suggestions for how to fix this?
Try this
right(convert (char(10), dob, 120), 5)
the 120 format is yyyy-mm-dd
You can use
format(date,'MMdd')

how to convert datetime value to another date only format?

my datetime field looks like this:
2011-02-07 06:51:32.000 (yyyy-mm-dd)
User input is in this format:
02-07-2011 (only the date)
This should be converted to:
02-07-2011 00:00:00.000
i tried CONVERT:
CONVERT(VARCHAR(10),myDateTimeField,113)
but this only works with date values not with datetime.
please help!
I used the following function:
CONVERT(datetime,Cast(myDateInput AS Char (10)), 105)
thanks for all your answers! and my apoligizes for changing my question!
select convert(varchar, myDateTimeField, 105) ...
Source: http://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-server/ and http://msdn.microsoft.com/en-us/library/aa226054(v=sql.80).aspx
Crystal has a function called Date; it takes either YYYY, MM, DD or a datetime as arguments and returns a date.
So I would suggest you make a formula to change the date to a datetime and display it.
date( mytable.mydatetimevar )
You should then be able to format this date as you want with the normal date field formatting options.
An alternative method of doing it is using Crystal's DatePart function to pull out the month, year, day and then restructure as required.
Try this:
select DATEPART(mm, myDateTimeField) + '-' +
DATEPART(dd, myDateTimeField) + '-' +
DATEPART(year, myDateTimeField)
I don't think it's surprising that a DATETIME field carries time information ;)
In fact, you shouldn't alter the field itself, as you would lose the database engine's ability to use date/time related functions with this field.
(the format used is ISO 8601 which helps the engine to easily identify the single parts)
As others said, it's good practice to format the date when you are retrieving it, either in your SELECT statement or in your application. (for more tips regarding this we would indeed need the information what sql server and what appliccation language you are using).
try converting with Style ID - 110
CONVERT(VARCHAR(10),myDateTimeField,110)
Result will be: 02-07-2011