SQL Syntax question - sql

I'm trying to remember the syntax to change a date field into a string. I know I'm close but not 100% correct. Here is what I'm using so far: TO_CHAR(FIELD_NAME). I'm using an Access database. The error I'm getting is: undefined expression. Any help would be very much appreciated.

Use either CStr(dateField) or Format(dateField) to convert.
Additionally you can add parameters to Format() to show it in a different format, such as:
Format(dateField, "general date") 9/12/2010
Format(dateField, "long date") Monday, September 12, 2011

Given that you're using MS Access and its a date field you're probably not just looking to convert to string but to also format the Date. If that is indeed the case you'll want the Format function
SELECT Format ([DateCreate], "yyyy/mm/dd") AS Foo
FROM MSysObjects;

If you're using SQL Server, try out CAST or CONVERT

You can use the CONVERT function, like this:
CONVERT(VARCHAR, DateField, 100)
Here's a link that shows the different date formats you can use:
http://www.sql-server-helper.com/tips/date-formats.aspx

I assume SQL Server as you're questions in the past are .NET Questions.
Use CONVERT
http://msdn.microsoft.com/en-us/library/ms187928.aspx

Related

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

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.

Convert Date to CYYMMDD SQL-Server

I am interested in converting a date value to CYYMMDD using native SQL Server 2012 functions.
I checked here:
http://www.sql-server-helper.com/tips/date-formats.aspx
and couldn't find anything.
Anyone know?
C stands for century I guess...
Have you looked at the new FORMAT function in SQL Server 2012?
http://msdn.microsoft.com/en-us/library/hh213505.aspx
You should be able to use something like this:
SELECT FORMAT(YourDateColumn, 'yyyyMMdd')
or whatever you really want to use - basically, the same formatting options as in C# / .NET are available when using FORMAT

Selecting date from Datetime field access

It may seems like stupid question but I cannot find any solution for it
I have column with StartDateTime dd.mm.yyyy hh.mm.ss
I need to select only dd.mm.yyyy because its access SQL Server CAST doesn't work. Is there any function in MS Access which I can use for selecting only dd.mm.yyyy?
It's been a while, but would the DateValue function work?

SQL Server 2008 DATETIME2 Format Question

I am dealing with a date/time from Australia (I am located in the USA). I am unable to get the following string to insert into a DATETIME2 column:
2010/19/10 04:38:12.892
As you can see, it's formatted in yyyy/dd/mm HH:MM:ss.MMM format. I understand that the normal format is yyyy-mm-dd HH:MM:ss.MMM. What I am wondering is if there is a locality setting on SQL Server that I can change to get it to accept this format, or if I need to parse it and rearrange it myself.
EDIT: Just for your information, I have been importing a mm/dd/YYYY HH:MM:ss.MMM format string into the field just fine.
It depends on the "locale" date format, I guess.
Some samples on how to convert here : http://www.java2s.com/Code/SQLServer/Date-Timezone/Formatdatemmddyyyy.htm
Hope this was helpful.
Try issuing SET DATEFORMAT ydm; before INSERT. Then CONVERT(DATETIME,('2010/19/10 04:38:12.892')); works fine.
More info
You can try this:
SET DATEFORMAT YDM;
select cast('2010/19/10 04:38:12.892' as datetime)
And it will parse it correctly
UPDATE:
I found help here.
But I tried casting to datetime2 directly and it didn't work. I don't understand why you can cast to datetime and not datetime2. Perhaps a good question for SO. :)

Specific format of datetime in insert to SQL Server

I have problem. I have script with more insert where is datetime in format 'DD.MM.YYYY'.
Problem is that server understand it as 'MM.DD.YYYY', so half of dates import with wrong value and half of dates do error.
Is any simple way how to say server that what is correct format?
I know that in past, if I worked with Oracle, I solve it by to_date function where I could specify format.
I believe http://support.microsoft.com/kb/173907 holds the answer
eg
set dateformat dmy
Using something like this should work:
convert(datetime, '28.6.2011', 104)
From here:
http://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/