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

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.

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.

Ms sql output in date format

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.

SQL Server : convert date to YYYMMDD?

I have a date in one of the column in SQL Server, the sample dates are:
10/02/2012
23/11/2012
13/01/2012
10/02/2012
10/02/2012
I have tried the approach to convert the dates to YYYYMMDD
DECLARE #v DATE= '1/11/2012'
SELECT CONVERT(VARCHAR(10), #v, 112)
I have another column in a same table in which i want to update the date in YYYYMMDD format ,the problem here is that the date are not proper
and throws an error
DECLARE #v DATE= '23/11/2012'
SELECT CONVERT(VARCHAR(10), #v, 112)
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
Any help is appreciated in this, The date will come in any order either dd/mm/yyyy or mm/dd/yyyy, it should be able to convert it properly
You may try using the following format to one data type to another.(dd/mm/yyyy to yyyy/mm/dd)
CONVERT(data_type(length),expression,style)
As well as update like following
UPDATE table_name SET
destination_column_name=orig_column_name
WHERE condition_if_necessary
if you want to insert date in SQL server you want to follow certain formats. You can follow either yyyy-MM-dd or MM-dd-yyyy
for your question if you follow MM-dd-yyyy this format and if you using SQL server 2012 or newer you can use this and you can get the result
DECLARE #v DATE = '11/23/2012';
SELECT FORMAT ( #v, 'yyyy/MM/dd', 'en-US' )
Refer these links FORMAT (Transact-SQL) , SQL Server date format function

display current date -1 in sybase

I am trying to display the previous day's date Sybase using a select query:
select dateadd(day,-1,convert(char(10), getdate(), 23))
this query displays as 2015-06-18 00:00:00.0
I expect the output to be 2015-06-18.
How can I get that?
Try select dateadd(day,-1,convert(Date, getdate(), 365))
Try select convert(char(10),dateadd(day,-1, getdate() ), 23 )
Dateadd expects a date parameter as the third argument. In your example you're feeding it a char(10) . Even though implicit conversion from Char->DateTime is supported in Sybase, I would not code to depend on it in this case.
Well, datetime is a binary type. How it is formatted for display is up to you.
getdate() returns a datetime representing the current date/time. And dateadd() returns a datetime or date value, depending on what it started with (in your case, that would be datetime). And when you run your select statement, it's getting converted to a string using the default format configured for your Sybase instance. Hence your results.
In a nutshell, you are:
Converting the datetime value to char(10) to get an ISO 8601 format date string (yyyy-mm-dd).
Converting that back to a datetime value (so the time component is start-of-day)
Subtracting one day.
The easiest way to get what you want (yesterday's date) is this:
dateadd(day,-1, convert(date,getdate()) )
Which, when formatted for display, will come out as something like (depending on the default format configured for your Sybase instance) yyyy-mm-dd.
Or it might come out like November 29, 2015. If you want to ensure that it is an ISO 8601 date representation, you'll need to be explicit about it and cast it a char or varchar, thus:
convert(char(10) , dateadd(day,-1, convert(date,getdate()) ) , 23 )
which leaves you with a char(10) value containing yesterday's date.
If your version of Sybase doesn't support date, you'll have to fall back to what you were doing, but something like this:
convert(char(10) , dateadd(day,-1, getdate() ) , 23 )
You are telling it to give you hh:mm:ss, so that's what you are getting.
The 23 inside the convert is the format code for yyyy-mm-ddTHH:mm:ss There is no code to get yyyy-mm-dd, the closest you can get is 105 (dd-mm-yy) or 110 (mm-yy-dd).
If you need yyyy-mm-dd, then you'll have to convert the date to a string(char or varchar), and truncate the parts you don't want.
Converting Datetime

Convert One Datetime format to another in SQL Server

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

Resources