converting date/time format in SQL - sql

My question is similar to some other questions here in Stackoverflow, but none of them worked for me. I have date and time column in the format 1991-04-01 00:00:00.000, and I want to convert it to this format: 19910401
I have tried this code below:
Update [TestFamily].dbo.List
set dbo.List.DtBirth = convert(
varchar(10), cast(dbo.List.DtBirth as
datetime ),112)
after executing it says 320104 rows affected , but actually nothing has changed , the date format remains the same.
Every suggestion will be appreciated.

DateTime data in SQL Server doesn't have a format. They are just values that are only changed to a certain format when you display them. So it is taken into account when you view the results of a query in SSMS, show them in an app, report, etc.
Displaying dates is almost always the job of the display layer, not the database. I would strongly suggest making this change at that level (report, app, etc.) rather than at the SQL level. Otherwise, you'll have issues with sorting, filtering, etc.

I think your problem is that you are trying to use an update. SQL updates change values, not data types. You are trying to create a different datatype, a char or varchar.
The field dbo.List.DtBirth is presumably a datetime, and your update does not change that. You could add another field, perhaps called DTBirthAsVarChar varchar(8), then update that based on the date field.
Update [TestFamily].dbo.List
set dbo.List.DTBirthAsVarChar = convert(
varchar(10), cast(dbo.List.DtBirth as
datetime ),112)
You also don't need both a convert and a cast in the same statement, they basically do the same thing.

Related

How to get only a date from a datetime column?

I have a report to do, so I need to return a date from a datetime column, but it need to come only with the date from the column.
I am using the Microsoft SQL Server 2014. I've already tried to use CONVERT(name_of_the_column, GETDATE()) but I realised the it only works to return the current datetime from the server.
How can I do that?
Use CONVERT(DATE, <expression>).
create table t (dt datetime);
insert into t (dt) values ('2022-10-20 12:34:56');
select *, CONVERT(DATE, dt) from t;
Result:
dt (No column name)
----------------------- ----------
2022-10-20 12:34:56.000 2022-10-20
See example at db<>fiddle.
I prefer to use CAST instead.
CAST(name_of_the_field AS DATE)
I need to return a date from a datetime column
There are several things to cover in order to fully answer this.
First, if that really is a datetime column, something to understand here is datetime values will ALWAYS have a time component. It's even right there in the name. There is no way to return a datetime value that does not have a time portion. The closest you can get is casting to a string or date, but that's not really the same thing. Typically what happens instead is the time portion is truncated back to midnight (all 0s)... but that time portion is still there. That's where you see answers like this:
dateadd(dd, datediff(dd,0, name_of_the_column), 0)
or this:
CONVERT(varchar(10), name_of_the_column,101)
The next to thing to understand is the one thing you should not do is convert the date value to a string, as in the second example.
Because of cultural/internationalization issues, converting datetime or numeric values to and from strings is much slower and more error-prone than you'd like to believe. It's something to avoid, and probably best left to your client code or reporting tool.
A final consideration is datetime values NEVER have any time zone awareness (that would involve datetime2). So if there's anything on the client to show this in a local time it may be picking the wrong offset. Do that with a value where the time portion was already truncated, and you can end up with unexpected results.
Put all this together, and what you should do is CAST() to a DATE:
CAST(name_of_the_column AS DATE)
Now I have seen this comment on another answer with this recommendation:
keeps turning back time
This could be the time zone mismatch I've already mentioned, or it could also be that name_of_the_column is not really a datetime column in the first place, but rather a string/varchar column pretending to be a datetime column. In that case, the CAST() may be parsing the values with different cultural rules than you expect. It goes back to that whole "slower and more error-prone/something to avoid" thing again. Also in this is the case the schema is considered broken, and you really should fix it to use actual datetime or datetime2 values.
Speaking of "fix it", I also noticed the SQL Server 2014 tag. SQL Server 2014 has been fully end of life since 2019. That means it has not received any updates — not even critical security patches! — for several years now. It's dangerous and irresponsible to still be using it. Updating to a supported version is job #1 here.
I don't know why, but the only way that have returned the result I want to reach was using
CONVERT(VARCHAR, TB.COLUM, 103) AS 'NAME'.
But as mentioned before, this is not a good practice, and than I resolved to made do the convertion in the report tool of the system that am I working

Is it possible in SSMS to pull a DATETIME value as DATE?

I want to start by saying my SQL knowledge is limited (the sololearn SQL basics course is it), and I have fallen into a position where I am regularly asked to pull data from the SQL database for our ERP software. I have been pretty successful so far, but my current problem is stumping me.
I need to filter my results by having the date match from 2 separate tables.
My issue is that one of the tables outputs DATETIME with full time data. e.g. "2022-08-18 11:13:09.000"
While the other table zeros the time data. e.g. "2022-08-18 00:00:00.000"
Is there a way I can on the fly convert these to just a DATE e.g. "2022-08-18" so I can set them equal and get the results I need?
A simple CAST statement should work if I understand correctly.
CAST( dateToConvert AS DATE)

SQL query for date formats

Can anyone help me in writing a query for creating a table which will store date in the format of month/date/year (mm/dd/_ _ _ _)?
I do not want to use varchar, because I will be needing to compare my dates also. I also do not want to use any other date formats because my inputs are being entered in the format of month/date/year (mm/dd/_ _ _ _)?
You can use varchar and it can be converted using the option 101 to the format you want (mm/dd/yyyy).
SELECT CONVERT(varchar(10), CONVERT(datetime, '2017/10/02', 111), 101)
The answer is going to depend on what type of database you are using (SQL Server, MySQL, PostgreSql, etc.) and whether you are doing this with 100% SQL, or if you have an application that uses another language that could manipulate the data as well.
Assuming this is an all SQL application, if you need to compare the dates, you will likely need to use the native Date data type for whatever database you are using.
By contrast, you could store it as a varchar, but then would need to cast it as a date for comparison.
To get the date into the format the you are needing, again depending on the database, you may have some sort of date_format function available to input/output the date in the format that you need. If there is not a native function for your database, someone has likely come up with a solution for that database. Searching for your database type (MySQL, Postgresql, etc.) and "date format" will be a good starting point.
If your application is also using another language, it may also have some native functions to convert the date into the format that you need.
I know this didn't directly answer your question, but hopefully gets you thinking about different ways to solve the problem and gets you pointed in the right direction.
You can use varchar and then convert it to your desired format. Here is a sample query.
DECLARE #s DATETIME
SELECT #s = CONVERT(DATETIME, '03/13/2013', 101)

Is it possible to add just Date in a datetime column and not get appended 00:00.000

MY Table consists of a datetime column and I wanted to know if its possible to add just Date in the column without getting appended zeros in Time part. Currently I used
CONVERT(date, getdate()) and tried to insert , but it comes in as
2013-01-20 00:00:00.000 in the column.
Pardon me if this is a very basic question
Yes, it is. Use the DATE data type instead of DATETIME.
However do not concern yourself with whether the 0s are there are not - that's not how SQL Server stores it internally, it's just how Management Studio is presenting it to you.
If you are using SQL Server you could use the Date type rather than DateTime. Otherwise the zeros will be there for the time portion. Of course you can choose not to display the time portion in your application if desired.

how to reformat DateTime data type

I know that it is possible to reformat the DateTime data type of the current date and time by using select convert. However, I havent been able to find a method to reformat an existing column (DateTime data type) to: hh:mm:ss yyyy/mm/dd. Or even better, I would like to reformat it to show time only. I dont want to simply convert to time data type because I am working with a chart that accepts either Date or Date time. But what I really want to display on that specific axis of the chart is time. Is there any way to reformat DateTime to my requirements? Thanks.
SQL Server's convert function can take an optional style argument depending upon the datatype. This is how you can get a datetime converted to a string in a variety of formats. For example, try running:
select convert(varchar(30), getdate(), 108) -- returns hh:mi:ss
You can replace getdate() in this example with a column name as well.
There are many styles available, as shown in the documentation.
Note that you will be returning a varchar, so you may want to sort by the original column's datatype.
You can use SET DATEFORMAT but this will change all DateTime's on your SQL Server. You can do some custom formatting, but it will convert the DateTime into a VarChar, so you won't be able to treat it as a DateTime.
SET DATEFORMAT: http://msdn.microsoft.com/en-us/library/ms189491.aspx
I'm not sure if you will be able to use the format you're asking about, though.