Ms sql output in date format - sql-server

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.

Related

T SQL Conversion failed when converting date and/or time from character string from VARCHAR(MAX)

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.

How to Convert 'YYYY-MM-DD-HH.MM.SS.MI' to 'YYYY-MM-DD' in SQL Server from DB2

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

How to create query with format date DD-MM-YYYY

I have query database like this:
SELECT * FROM TABLE WHERE start_date = '01-10-2016' //DD-MM-YYYY
But above code error and actually in database date format is YYYY-MM-DD.
So how to create query with format date DD-MM-YYYY?
A proper date column (data type date !) has no format. Then it's enough to get your data input for a date column right, use to_date() for non-standard input format like #Shiva posted. Or better yet, always provide date literals in ISO 8601 format 'YYYY-MM-DD' to begin with, which works with any locale setting.
If you are running a broken design with dates stored as text, then combine to_date() and to_char() to transform any valid date format into any other text format:
SELECT * FROM tbl
WHERE start_date = to_char(to_date('01-10-2016', 'DD-MM-YYYY'), 'YYYY-MM-DD');
May be you can use to_date function to convert the above format into the standard format.
For instance,
SELECT to_date('01-10-2016', 'DD-MM-YYYY');
----------
2016-10-01
1 row
https://www.techonthenet.com/postgresql/functions/to_date.php

Insert System date in dd/mm/yyyy hh:mi:ss format

Im trying to insert Checkout Date in dd/mm/yyyy hh:mi:ss format using the following query:
insert into Rawtransactions
(Card Number,Processing Date,CurrencyCode,Checkout Date
)
Values
(
#NewCardNumber,getdate(),'USD',CONVERT(VARCHAR(24),GETDATE(),113)
)
But It inserted date in this format 02 Sep 2015 14:45:09:390 instead of 02/09/2015 14:45:09:390. What is the right syntax for this?
EDITED - Please note that the Checkout Date is an nvarchar field and the Schema cant be changed now. I want to know how can I insert date in this field?
Try like this:
select CONVERT(VARCHAR(10), GETDATE(), 103) + ' ' + convert(VARCHAR(8), GETDATE(), 14)
However it is not recommended to store dates as varchar() as it may lead you to some formatting issues like this in future.
Dates are dates. You can't control the format in which the database stores them; they're just values with a date data type. If you want to use them later in an application and display them a certain way, then that is up to your application.

sql convert date format not working

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.

Resources