I Have this date "27/03/1985" and because it starts with days i can't convert it to datetime.
See: http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx
DateTime.ParseExact() lets you specify the format of the string representation.
Yes you can. You just need to use the right culture specifier. It's a UK date so you need so supply the correct format provider:
dateValue = DateTime.Parse(dateString, culture);
where culture is of type CultureInfo
Source
Related
Hello I want to convert a varchar '190613' in a date format 'DD.MM.YY'
The main problem is that the varchar is 'YY.MM.DD'
You can do convert to a date and back to a string:
select to_char(to_date('190613', 'DDMMYY'), 'YY.MM.DD')
You can try the below:-
select to_char(to_date('190613', 'DDMMYY'), 'YY.MM.DD') as dates from dual;
Your starting value is '190613', and you want to convert it to 'DD.MM.YY' format.
You should use as - Gordon stated
to_char(to_date('190613','YYMMDD'),'DD.MM.YY')
You also specified you have a starting template which is "YY.MM.DD". In that case, you need to specify dots in the to_date function
to_char(to_date('19.06.13','YY.MM.DD'),'DD.MM.YY')
to_date function serves to convert the varchar to a date sql type, you provide the starting template as second argument of the function to define which pattern your string follows. More info:
to_date() SQL function
Whilst to_char takes a date object and converts it to a specific template. More info: to_char SQL function
You can convert the next form:
CultureInfo provider = CultureInfo.InvariantCulture;
String strDate = "190613"
DateTime dtDate= DateTime.ParseExact(strDate, "mmddyy", provider);
if you want in especific format :
String.Format("{0:DD.MM.YY}",dtDate) // print "19.06.13"
I have a date in string format in hive table (like "20121021") How do I convert this into "yyyy-mm-dd" (ex: 2012-10-21 or 2012/10/21)?
You can also use cast():
select cast(substr(col, 10) as date)
At least, this works for the YYYY-MM-DD format. I should also note that in a date context, a string such as YYYY-MM-DD will typically be converted automatically.
You can use TO_DATE(). Try following:
TO_DATE('20121021')
Or
from_unixtime(unix_timestamp('20121021', 'yyyyMMdd'),'yyyy-mm-dd')
I have a column in my table for storing dates and it is in 12-06-2013 15:32:45. I want to convert it to MM/DD/YYYY format. How can I do it?
Coulmn type is varchar
First you need to CONVERT VARCHAR() to datetime type and and then to CONVERT it to string in desired format:
SELECT CONVERT (varchar (10), CONVERT (date,'12-06-2013 15:32:45' ,103),101)
First 103 is used to interpret current date format, and second - 101 - target format.
If you change target date format from varchar to date then your output in MSMS will be in default display date format of you SQL Server, not the desired format. This is because SQL Server stores dates as integers and converts them before dispalying the value. Therefore if you need to store in certain format, then store in VARCHAR type.
Check out CAST and CONVERT functions on MDSN
or you can do it in your c# code like that :
First you have to take your date from your table and put it in string var "DT" exemple :
string strDT =db.TableTitle.date;
string date = strDT .ToString("MM/DD/YYYY");
DateTime DT = Convert.ToDateTime(date);
then you can use the DT time variable :)
I currently have a challenge of storing a DateTime value in a NVarChar field so that it's culture independent.
I've read that you can convert the value to an int by using CONVERT(int, GETDATE(), 112) which should make it culture independent but the former statement doesn't store the time.
What is the industry standard of storing a DateTime as culture independent?
EDIT
Please note that I can't use DateTime in my scenario. It must be NVarChar.
EDIT 2
Alright, found the answer to my own question.
To convert a DateTime to it's binary(8) raw format:
convert(binary(8), GETDATE())
I then store the value in a VARCHAR field as follows:
CONVERT(VARCHAR(MAX), convert(binary(8), GETDATE()), 2)
To retrieve it back from the varchar field and convert it to DateTime:
CONVERT(DateTime,CONVERT(binary(8), [TextField], 2))
As var as I'm concerned, this will store a DateTime as culture independent.
EDIT 3
It seems like user Kaf has the best solution. I will rather use format 126 to convert it to text and then back to DateTime from text.
Thanks everyone and sorry for the confusion.
If you CANNOT store date as Datetime, you can use style 126 which gives ISO8601 format (yyyy-mm-ddThh:mi:ss.mmm (no spaces)). I think it is culture independent.
Fiddle demo
select convert(nvarchar(50),getdate(),126)
Best thing is to store Date as a DateTime/Date type.
You should use DATETIME or DATETIME2 data type to store date and time values. They are stored in binary format in the database and are culture independent.
You can read more on MSDN here: http://msdn.microsoft.com/en-us/library/ms187819(v=sql.100).aspx
More on how SQL Server stores the datetime values: "It uses 8 bytes to store a datetime value—the first 4 for the date and the second 4 for the time." (from: http://sqlmag.com/sql-server/solving-datetime-mystery)
I do not get this idea to store a date in a varchar field so that it is 'culture independant'. dateTime data type is culture independant. What is culture dependent is the way date values are displayed:
MM/dd/YYYY
dd/MM/YYYY
YYYY-MM-DD
etc
But, if the display changes, the underlying value itself is still the same ... and this is why you can easily 'convert' dates from one format to another....
So, for the sake of simplicity, I do strongly advise you to switch to a culture-independant, datetime field. Otherwise any further use of this field's content (calculation, display, print out, etc) will be a real PITA ...
I want to create a function, which has a paramater ( a string which contains a date ) and then the function converts it and returns it. In our company we have workstations with three different languages. We have hungarian, english and german workstations too. I want to read a date from the registry, but this date will be written into the registry according to the current regional setting.
So if the regional setting is hungarian, then date written to date registry is 2012.01.25 (YYY.MM.DD), but if i change the regional setting to german then the value written to the registry will be 25/01/2012 (MM.DD.YYYY). If i change the regional setting to english, then the value will be 01/25/2012 (DD.MM.YYYY).
Unfortunately i don't know which regional setting was used when the date was written into the registry, because it can be changed since the value was written into the registry.
This iy why i want to create a function which gets a date, and then converts it to this format: YYYY.MM.DD. but i don't know how to do it.
Could someone help me how to do this?
Thanks!
Are you managing this registry value directly or it belongs to another software and just trying tomread it?
If it's yours, then
A. if it is a string value, then simply format it before storing, to ISO format (yyyy-MM-dd), format or formatdate function will do the trick
B. if it is a binary value, convert the datetime value to double with cdbl and store that
Well, if it's not yours, then it's not your lucky day. I've done it a couple of years ago and I used the text around the date to make an assumption on the format ...
You can use this expression to convert your strings to SQL type date. This expression uses the DD/MM/YYYY format only when it cannot use its default MM/DD/YYYY format.
CASE
WHEN CHARINDEX('/', val)=5 THEN CONVERT(date,val,111)
ELSE CONVERT(date, val, CASE WHEN LEFT(val,2) <= '12' THEN 101 ELSE 103 END)
END
This expression should be used inside a select statement. I am assuming that the name of your varchar column containing a date string is val.
If you are saving those dates on a date type of column (DATETIME, DATE, SMALLDATETIME, etc), then it won't matter how it is displayed or under what language it was saved. If you want to convert a DATE to a VARCHAR on the format YYYY.MM.DD then you can use: CONVERT(VARCHAR(10),YourDate,102).