Converting Date format in Sql Server - sql

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 :)

Related

Convert String datetime to numeric date time

I have a column that I need converting from string date time to an actual data time data value.
The current format is as follows
15-JUN-22 10.24.10.414000
and I need to change it to the following format
15-06-22 10.24.10.414000
I use a stored procedure to automatically change the format, but it fails on this stage due to the non numeric characters.
Is there a way map or change all the string months to int values within the datetime? and if so how?
Converts used so far
TRY_CAST(CREATE_DATE AS DATETIME2(7)) AS CREATE_DATE
AND
CASE WHEN LEN([INTERVAL_START_DATE]) > 0 THEN TRY_CONVERT(DATETIME2, [INTERVAL_START_DATE], 103) ELSE NULL END
try this
CAST({your field name} as date) CONVERT(date,'{systemClock.UtcNow.ToString("o")}',127)

How to convert string to date type in the Pentaho?

I am try to convert string value to date. The string has this format : yyyy-MM-dd. But when I try to convert using select values (in meta-date I selected fildname, type = Date and currency = dd/MM/yyyy I got this error :
String : couldn't convert string [2017-01-30] to a date using format [yyyy/MM/dd HH:mm:ss.SSS] on offset location 4
If I do in calculator step : Create a new field, Final_date as a Copy of field A; on Field A put the name of your input string; Data type is date and on Conversion mask choose the yyyy-MM-dd format (you don't have to pick one from the dropdown menu, you can write your own). I got the same error.
I am using the Pentaho Data Intagration 9.
I am try to convert the string format in yyyy-MM-dd to date type format in dd/MM/yyyy. For this case, how to convert string to date ?
When converting from string to date you specify the source format that the string is using, so in this case yyyy-MM-dd. That should be in the format selection list, but you can also manually type in any format needed.
Once the field is in date format, it will be correctly output to most database types. For files, you can define the new format (dd/MM/yyyy) in the output step like Text File Output or Excel Writer. Alternatively, you convert the date back into a string with the desired format using Select Values.

How to convert column in date format with "Date" Data type

I am using SQL Server 2017. I am trying to convert one column's values into a date format like DDMMYY. I want to keep the same data type as DATE.
Using the CONVERT and FORMAT functions, it returns VARCHAR and NVARCHAR.
How can I do this conversion?

How to convert string with a specific date format like 'mm/dd/yy' to date object in hibernate query

One of the columns my_column in my table is of type varchar where i store value of date in string. The format is 'MM/DD/YY'. I want to use cast function of hibernate to convert this column value to date. But it appears that the cast function accepts string of 'yy-mm-dd' format to convert. Is it possible to use cast for string with specific date format for conversion. Something like this: cast(am.my_column as date format 'mm/dd/yy')
cast function in hibernate is used this way: cast(am.my_column as date).
My use case is basically converting the column value from string to date and making a date comparison. cast(am.my_column as date format 'mm/dd/yy') > '12/30/2017'
Instead of casting a column value inside the query, you may consider to give '12/30/2017' as parameter (with type of Java Date). So that you can directly write am.my_column > :parameterDate

Convert Date Against Where Clause

I have Following dummy table with data:
ACID srno date(mm/dd/yyyy) name
3 1 04/12/2010 mahesh
3 2 04/12/2010 mahendra
Now if I try with Following SQL Transact:
select srno from dummy
where name = 'mahesh'
and date= convert(datetime,'12/04/2010',101) –- I have date in dd/MM/yyyy Format
and ACID=3
It’s Not returning the srno of the table. That means Date is not execute convert statement as above
What’s the reason?
Try using style 103 instead of 101.
select srno from dummy
where name = 'mahesh'
and date= convert(datetime,'12/04/2010',103) –- I have date in dd/MM/yyyy Format
and ACID=3
If you convert 12/04/2010 using format 101, you get date "December 4, 2010", which is not in your database. Use format 103 to convert a date in format dd/mm/yyyy to DateTime.
The database stores dates using the DateTime type which is format-agnostic. It does have a default format for string conversions, which seems to be mm/dd/yyyy (101) on your database.
However, when you convert a string to add it to your table, you want to specify the format of your input string, in your example dd/mm/yyyy (103).
Take a look at the MSDN article for CAST and CONVERT which details all format styles that you can use with dates.
To be honest, if you want to specify a DATE LITERAL in SQL Server, please stick with the simplest YYYYMMDD format, e.g.
and dummy.date = '20100412'
It is robust and works for all regional, user language and dateformat settings. This assumes the other side of the comparison is already a date column. Even if you had to CAST it, using this format you don't need to specify a format
and dummy.date = cast('20100412' as datetime)