I can not convert field run_date on sysjobhistory table from yyyymmdd to mmddyyyy.
select CONVERT(varchar(10),run_date),101) as Date from sysjobhistory
Please correct me. Thanks.
declare #dt date ='20161025'
select #dt,CONVERT(varchar(15),#dt,101)
See here
Date type does not have yyyymmdd nor mmddyyyy. This type only valid for string (as varchar, nvarchar). Plus, you have extra ) after rundate.
The correct one should be:
select CONVERT(varchar(10),run_date,101) from sysjobhistory
where 101 is style argument: https://msdn.microsoft.com/en-us/library/ms187928.aspx
shouldn't it be: CONVERT (varchar(10), run_date, 101). What I am trying to say is that you have an extra parenthesis after run_date.
Related
I'm using SQL Server 2014. I have a date stored as varchar(MAX) in the format of:
2019-02-18
However, I want it in the British format dd/mm/yyyy (103).
This is my SQL:
SELECT CONVERT(DATE, DateField, 103) AS "JobStartDate"
FROM tblTest
However, I keep getting this error:
Conversion failed when converting date and/or time from character string.
What am I missing?
Update: The date is initially stored as varchar max as it is coming from a 3rd party system. I have no control over this and I completly understand this is the wrong format, but this is what I have been given.
I have a date stored as varchar(MAX)
There's your problem right there.
Not only you are using the wrong data type to store dates, you are also using max which is a known performance killer.
The solution to the problem is to alter the table and store dates in a Date data type - but first, you must look up all the objects that depends on that column and make sure they will not break or change them as well.
Assuming this can't be done, or as a temporary workaround, you must first convert the data you have to Date, and then convert it back to a string representation of that date using the 103 style to get dd/mm/yyyy.
Since yyyy-mm-dd string format is not culture dependent with the date data type, you can simply do this:
SELECT CONVERT(char(10), TRY_CAST(DateField As Date), 103) As [JobStartDate]
FROM tblTest
Note I've used try_cast and not cast since the database can't stop you from storing values that can't be converted to dates in that column.
You want to format the DateField column and not convert it to date.
So first convert it to DATE and then apply the format:
SELECT FORMAT(CONVERT(DATE, DateField, 21), 'dd/MM/yyyy') AS JobStartDate
See the demo.
From DB2, we are getting one column value "2018-01-12-13.28.37.111972" like this format("YYYY-MM-DD-HH.MM.SS.MI"). I want to convert this one to "YYYY-MM-DD" in sql server. I used Cast(fieldname as date) is not working.
If you are interested in both the date and time, Larnu's answer is the way to go.
If it's only the date you care about, you can simply use left to get only the date part of the string:
SELECT CAST(LEFT('2018-01-12-13.28.37.111972', 10) As Date)
BTW, datetime isn't stored with a display format.
I can't speak for DB2, but I suspect you're it's passing the value as a varchar, and not a datetime, and hence the problem. If DB2 has to pass the values as a varchar, you have use STUFF to fix the values:
SELECT DB2.DateString,
CONVERT(datetime2(6),STUFF(STUFF(STUFF(DB2.DateString,11,1,'T'),14,1,':'),17,1,':'))
FROM (VALUES('2018-01-12-13.28.37.111972'))DB2(DateString);
select convert(varchar(10), '2018-01-12-13.28.37.111972', 5)
will produce: 2018-01-12
With a few string functions you can transform the timestamp text to a format that can be casted to a DATE or a DATETIME
For example this snippet:
select db2col,
try_cast(left(db2col, 10)+' '+replace(substring(db2col,12,8),'.',':')+substring(db2col,20,4) as datetime) as col_as_datetime,
cast(left(db2col, 10) as date) as col_as_date
from (values
('2018-01-12-13.28.37.111972'),
('2018-02-15-14.29.54.123456')
) as vals(db2col)
Returns:
db2col col_as_datetime col_as_date
-------------------------- ----------------------- ----------
2018-01-12-13.28.37.111972 2018-01-12 13:28:37.110 2018-01-12
2018-02-15-14.29.54.123456 2018-02-15 14:29:54.123 2018-02-15
I have a requirement. I have datetime field and I want data in datatype=date
Existing date: 2019-11-13 00: 00: 00: 000 ; datatype=datetime
Expected output require: 11/13/2019 (mm/dd/yyyy) ;
datatype= date
Please help me.
If the core requirement is a right type then:
SET DATEFORMAT MDY;
SELECT CAST(GETDATE() as DATE);
Explicit DATEFORMAT added becaise the output depends on a language settings, so can be yyyy/mm/dd or mm/dd/yyyy, some apps can be sensitive to this, as example SSRS.
However, if there is still a requirement to get value in a precisely right format on a database side, then consider to use a FORMAT statement:
SELECT FORMAT(GETDATE(), 'MM/dd/yyyy')
I have datetime field and I want data in datatype=date
If you want the data type as DATE, in that case you can try like following.
SELECT CAST(YourDateTimeColumn AS DATE) from [YourTable]
Formatting part you should be doing in UI.
You can use CONVERT() :
SELECT t.datecol, CONVERT(VARCHAR(10), t.datecol, 101)
FROM table t;
Your desired date format requires varchar type, if you want date only then you can do instead :
SELECT t.datecol, CONVERT(DATE, t.datecol)
FROM table t;
If you don't want to convert the type, then these type of conversation should do in presentation layer instead.
I have a char column that stores dates as yyyy-mm-dd. I need to display these dates as mm-dd-yyyy.
Thanks in advance.
The root of your problem is because you are using the wrong datatype. Dates should stored as dates. Anything else is nothing but a pain to work with. To get your formatting you will first have to convert your string to a date so
you can format it.
Here is an example.
select convert(date, YourDateColumn, 101)
You can read more about convert here. https://msdn.microsoft.com/en-us/library/ms187928.aspx
If 2012+ you could try
Select Format(GetDate(),'MM-dd-yyyy')
Returns
08-03-2016
To convert the string
Select Format(cast('2016-08-03' as date),'MM-dd-yyyy')
DECLARE #Date varchar(12)='2012-01-29'
SELECT FORMAT(CONVERT(datetime,LEFT(#Date,4) + SUBSTRING(#Date,6,2) + SUBSTRING(#Date,9,2)), 'MM-dd-yyyy', 'en-US' ) AS Result
This is a little bit heavy on processing, but it works.
This answer also assumes that ALL values in the date text column are formatted as YYYY-MM-DD. If you have any values that aren't formatted that way as strings, then you will run into problems.
Obviously you will need to replace the #Date variable with the name of your date text column. I wanted to provide an example you can run easily to check the answer.
I have a datetime2 format in my Database 2015-06-22 06:23:42.790. I need to convert this into the following format 22/06/2015 06:23:42.790.
Is it possible?
Here is one way to do this:
DECLARE #date DATETIME2 = '2015-06-22 06:23:42.790';
SELECT cast(convert(VARCHAR(10), cast(LEFT(#date, 10) AS DATE), 3) AS VARCHAR(10))
+ ' ' + substring(cast(#date AS VARCHAR(50)), 12, 12)
Query breakdown:
First part: take first 10 characters from your datefield and then convert it to date style 3 (dd/mm/yyyy).
Second part: Add a space between date and time.
Third part: cast your datefield as varchar and extract the time which should always start in the 12th position of your string.
Join them all together and there you have it! Hope this helps!
Don't try to convert the database layout. Year Month Day is how SQL server shows the date because it ignores any international date formats.
I notice you want it as 22/06/2015 are you in the UK ? In the USA it would be 06/22/2015 Not such a problem because it's obvious that the 22 is the day. But if the date was 05/06/2015 how would sql or anyone know what day or month you're talking about.
So, get in to the habit of working in the ISO format year month day.
You don't mention what programming language. When reading data out of the database youd read it into a datetime variable. That will convert the date correctly into whatever locale your user is using. Different languages have different ways of getting the date into a datettime variable.
If it's only for display-use you can convert to varchar with FORMAT() function:
DECLARE #tab TABLE
(
datevalue DATETIME2
)
INSERT INTO #tab VALUES(GETDATE())
SELECT datevalue,
FORMAT(datevalue,'dd/MM/yyyy hh:mm:ss.fff') as newformat
FROM #tab