How do you convert SQL mm/dd/yy to mm/dd only? - sql

How do you convert SQL mm/dd/yy datetime to mm/dd only? On Microsoft server.
Thanks all.

With dates and times it is an extremely common mistake to believe that what you see is what is stored. If the field is date, datetime, smalldatetime or datetime2 then what is stored are integers, not strings. So if the field is one of these, then:
convert(varchar(5),[date_field],1)
or
format([date_field],'MM/dd') -- mssql 2012 onward
If the information is a string already then left() will do the job.

Since you have specified an input format, the input must already be a string. Simply truncate with
cast(dateIn as char(5)).

You can use LEFT to just return the day and month:
SELECT LEFT('12/12/2000', 5)

I realize this isn't directly answering your question the way you asked it, but the best advice I can give is: Don't.
Instead, send back the field in its native datetime type. The database is not the place to be doing formatting. Instead, format the date in your application code.
For example, if you are calling SQL Server from a C#/.NET application, you could retrieve the value from a DataReader like this:
DateTime dt = (DateTime) reader["YourDateTime"];
Then you would format it as a string like this:
string s = dt.ToString("MM/dd");
This will ensure that the date is formatted correctly. If you are using a different language to call SQL Server, there are probably similar methods in that language.
One of the problems with the other approach mentioned (trunacating the string) is that the original value might not be formatted in mm/dd/yyyy to begin with. That all depends on the environment settings where the SQL Server is running. If you run the same code on an environment with dd/mm/yyyy settings, you would have unexpected results. This is avoided by using the native data type, the way I described.

Related

I try to convert date format in sql but it still doesn't work

I use the CONVERT() function to trying convert the date format like DD/MM/YYYY with code 103 when I query database, and nothing happens. The data field still displays default format with YYYY/MM/DD.
UPDATE STAFF
SET BIRTH = CONVERT(smalldatetime,'26/08/1900',103)
WHERE ID = 'SF01'
How can I fix this problem ? I'm a newbie so i don't know lots of SQL
Avoid formatting dates in your database, it should be done in the application that queries the data. But IF you still need to do it in your database for whatever reason:
If you want to store the date in the DD/MM/YYYY format in your table then you can do 2 things,
Change your column data type to varchar(not ideal and you should try avoiding this method)
Change the regional settings on the machine running SqlServer and keep it as a date type (this is somewhat useless if you plan on querying data from another app as the app will probably format the date to its local format).
In case you decide to store it as varchar you will need to use the Format function when inserting or updating like this: Format(MyDate, 'DD/MM/YYYY').

Change Datetime format in Microsoft Sql Server 2012

Hi i want to change the default datetime type in sql server. I have already table who has rows and i dont want to delete them. Now the datetime format that had rows is: 2015-11-16 09:04:06.000 and i want to change in 16.11.2015 09:04:06 and every new row that i insert i want to take this datetime format.
SQL Server does not store DATETIME values in the way you're thinking it does. The value that you see is simply what the DBMS is choosing to render the data as. If you wish to change the display of the DATETIME type, you can use the FORMAT() built-in function in SQL Server 2012 or later versions, but keep in mind this is converting it to a VARCHAR
You can get the format you desire via the following:
SELECT FORMAT(YourDateField, N'dd.MM.yyyy HH:mm:ss')
There is no such thing as format of the DATETIME data type, it has no format by nature, formatted is the text representation you can set when converting to VARCHAR or some visualization settings of the client / IDE.
If you, however, want to be able to insert dates using string representations that are alternatively formatted (i.e. control the way string input is parsed to datetime type) you can check SET DATEFORMAT - as explained in the remarks section this will not change the display representation of date fields / variables.
SQL serve provide wide range of date formatting function or way by using that user can change date format as per his requirement.
Some of are giver bellow.
CONVERT(VARCHAR(19),GETDATE())
CONVERT(VARCHAR(10),GETDATE(),10)
CONVERT(VARCHAR(10),GETDATE(),110)
CONVERT(VARCHAR(11),GETDATE(),6)
CONVERT(VARCHAR(11),GETDATE(),106)
CONVERT(VARCHAR(24),GETDATE(),113)

Select GetDate() via VB.net different then Select GetDate() via SSMS

When I Use the below code:
Dim cmd As New OdbcCommand("SELECT GETDATE()", oConn)
retVal = cmd.ExecuteScalar()
The resulting output is:
8/1/2013 10:10:39 AM
When I run the exact same query directly in Management Studio I get:
2013-08-01 10:10:39.317
When I check my computer settings versus the SQL Server settings they match.
Anyone know what I need to do to ensure it matches?
Specifically I am talking about the Date format difference.
If you want the date output with a specific string format, then you can use CONVERT() with a style number. For example:
SELECT CONVERT(CHAR(20), GETDATE(), 22),
CONVERT(CHAR(23), GETDATE(), 21);
Results:
-------------------- -----------------------
08/01/13 10:53:54 AM 2013-08-01 10:53:54.943
However, if you are using the date for things other than direct display, only apply that formatting when you are displaying it. For all other purposes it should remain a datetime type and should not be converted to a string.
As for the differences in the actual time value, it's not clear what problem you're talking about, but I suspect you simply ran these queries half an hour apart. If those were run at or around the same time, it looks like the server is half an hour fast - maybe it's in a different time zone or maybe it's just a lot of drift or someone not bothering to use a time service. Your application should never use the time / time zone of the client, especially if it's distributed - always use the time on the server.
Dates have no format. Format comes into play only when you convert dates to a string. The forma used depends on who does the conversion: the server or the client?
Your VB.NET query returns a date from the server and converts it to a string when you write it to the console, a form or whatever. VB.NET uses your programm's CurrentCulture, whose defaults come from the current user's regional settings.
When you display data in SSMS, an ISO format is used so there is no ambiguity when you edit the data.
When you compare date and string values in a query, either explicitly by converting a date to a string or implicitly because you just typed MyDate = '13/1/2013, a conversion is made using the column's collation. Collations are inheritted so the column's collation is the same as the database's collation.
Try this:
net time \\SERVER_NAME
Note: Obviously SERVER_NAME is the name of your SQL Server machine.
Do you see a 30 minute difference in the result of that call?
I looked deeper into the code and found that some enterprising fellow had added code to a line of SQL later in the process which forces DMY format on that query.
so the code in the VB is returning the proper Date on the app machine. Which means that there must be a difference between my computer and the app machine.
Another coder ran into the same issue and so there solution was to add the below code to the SQL that was pulling from the DB.
SET DATEFORMAT dmy
This forces the SQL to use DMY format... I removed this code Compiled and ran the EXE from the server machine and my issue dried up!
Thanks for everyone's help.

Convert Coldfusion Error.DateTime to a normal format?

At my company we store the information gathered from our site wide error template, into the database, but because of the format of the error.datetime it is making hard for me to do any sql queries for different date ranges.
Has anyone used some t-sql or coldfusion code to convert it to a mm/dd/yyyy format?
Here is an example of the format it currently is as.
Sun Jun 13 21:54:32 CDT 2010
But for any queries, I need to do, I have in a better format, I believe.
On the CF side, you should be able to user createOdbcDateTime() to correctly format it for the database or dateformat() to format it as text. If the date is coming back as text instead of a date object, you could use parseDateTime() to convert to a date object.
As an alternative, you could avoid having to convert dates at all if you just use the SQL Server built-in getDate() function to fill out your date column as the error is being inserted into the database.
It may not be exactly the same time (i.e. it might be out by a ms or 10) but it should be pretty close and perhaps good enough for your purposes.
Just make sure that your database server and application server are time synchronised!

SQL: how to check for a specific DateTime

i need to check for a specific DateTime value in my table from my code (VB.NET) and i don't know how to format the DateTime as a string. i've read that SQL Server will recognize my string if it's in either date, time, or date and time format. i mean:
'May 15, 2004'
'5/15/2004'
'20040515'
'2004 4 am'
will SQL Server recognize these strings as valid DateTime values? i'm curious because if i check the actual DateTime values in the table they are in this format:
2/2/2006 3:49:33 PM
Don't put the date/time value in the SQL query in the first place - use a parameterized query and then you don't need to know or care what format SQL Server would parse literals as. You put the placeholder in the SQL, and specify the value as a DateTime in the parameter collection.
You should be using parameterized SQL as a matter of course, in fact - not only does it get rid of formatting and parsing problems like this, but possibly more importantly it's the single most effective weapon against SQL injection attacks.
If not using a parameterized query, use CAST/CONVERT to explicitly change a string to a DATETIME:
SELECT CAST('2/2/2006 3:49:33 PM' AS DATETIME)
On my SQL Server 2005, that returns to me:
2006-02-02 15:49:33.000
Mind that the default date format in SQL Server can be different than what you provide.
This has always been safe that I have found:
YYYY-MM-DD HH:MI:SS
If you're comparing DateTime to DateTime, you don't have to worry about conversion, necessarilly, but yes, Sql Server (at least as of 2k8, and I believe 2k5 as well) will automatically parse a DateTime from a string. That is, if you pass '5/15/2004' it will see 5/15/2004 12:00:00 AM or something similar.
a better way, though, is to use SqlParameters in your SqlCommand from Code.