SQL CE SELECT Convert - sql

I'm trying to convert a DateTime field (Date of Birth), within the an SELECT FROM that needs to work for SQL CE and SQL Access databases.
I've found a thread that shows me that it can be done, but not the formate I want it to be. Does anyone know how I can change this to show yyyy-mm-dd format not mm/dd/yyyy
SELECT CONVERT(nvarchar(10), C.DOB,101) FROM table

You can use the following query
SELECT CONVERT(NVARCHAR(10), C.DOB, 120) AS FormatedDate FROM Table

I've been able to find how I can do it. Its using the convert function, but the code passed into it is different
So to get the correct format, I need the below code
CONVERT(nvarchar(10), A.TestDate,21)
enter link description here

Related

Convert YYYYMMDD to MM/DD/YYYY in Snowflake

I need help in figuring out the date conversion logic in Snowflake. The documentation isn't clear enough on this.
In SQL Server, I would try
SELECT CONVERT(DATE, '20200730', 101)
and it gives me '07/30/2020'.
If I try the following in Snowflake,
to_varchar('20200730'::date, 'mm/dd/yyyy')
it gives me '08/22/1970'. Why would it give an entire different date? Need help in getting the logic with the correct date.
The issue with what you are doing is that you are assuming that Snowflake is converting your string of '20200730'::DATE to 2020-07-03. It's not. You need to specify your input format of a date. So, 2 options based on your question being a bit vague:
If you have a string in a table and you wish to transform that into a date and then present it back as a formatted string:
SELECT TO_VARCHAR(TO_DATE('20200730','YYYYMMDD'),'MM/DD/YYYY');
--07/30/2020
If the field in the table is already a date, then you just need to apply the TO_VARCHAR() piece directly against that field.
Unlike SQL Server, Snowflake stores date fields in the same format regardless of what you provide it. You need to use the TO_VARCHAR in order to format that date in a different way...or ALTER SESSION SET DATE_OUTPUT_FORMAT will also work.
Try select to_varchar(TO_DATE( '20200730', 'YYYYMMDD' ), 'MM/DD/YYYY'); which produces 2020-07-30
You may need to refer to https://docs.snowflake.com/en/user-guide/date-time-input-output.html#timestamp-formats

SQL time format

Im making a simple table in sql where i need to specify certain times.
I use datatype time.
I enter time like this: 5:38
It results in this: 05:38:00.0000000
How do I get rid of all the zeros? Do I have to change data-type?
To summarize the comments from other users. Possible solutions are:
Create a time(0) (see the sqlfiddle)
Use a convert to convert the results to your desired representation (also in sqlfiddle).
Sample:
select convert(char(5), t, 108)
from tableName

How to convert varchar to date

I am working in SQL Server 2008 R2
I need to convert a varchar(50) field in a view to a date format.
I have a view that uses the following to create the field delivered_date:
convert(varchar(50),[delvd_dt],110) as [delivered_date]
The formatted field looks like : 2012-03-11 16:24:42.0000000
I need the results to be a date so that I can check for dates within a range. I would prefer to do it within the view so that I can use SSRS to create the range for the report.
Have you tried cast()? The following returns the right date for me:
select CAST('2012-03-11 16:24:42.0000000' as DATE)
Perhaps you can simply truncate the millis and use CONVERT:
SELECT Convert(datetime, LEFT('2012-03-11 16:24:42.0000000', 19), 102) AS [Date]
Demo
This works even in MS SQL-Server 2005.
CAST and CONVERT (Transact-SQL)
Try this link website shows several formatting options.
Example:
SELECT CONVERT(VARCHAR(10), GETDATE(), 105)

How do I display varchar date string with mixed format to another format?

I am using SQL server 2008 R2. I know I can use CONVERT with different format code as the third parameter to do the conversion to DATETIME first and CONVERT again to VARCHAR with another format code to change the display format.
The real problem now is I have mixed raw data in a single column. So my question is how do you write a single SELECT statement to display from mixed YYYY/MM/DD, DD/MM/YYYY all to DD/MM/YYYY?
I tried to use ISDATE() but it think 31/01/2013 is not a date while 01/01/2013 is a date. Now I could only think of to see if the YYYY is on the left or on the right to determine the correct input format, but I dont know how to write it out in a single SELECT statement.
Any procedure to change the format first then do a simple SELECT is not an option. I am not allowed to change the source.
Thank you
Why not just use string manipulations? Something like:
select (case when substring(d, 5, 1) = '/' -- YYYY/MM/DD
then right(d, 2)+'/'+substring(6, 2)+'/'+left(d, 4)
else d
end)
By the way, if you are choosing formats for dates when represented as strings, I highly recommend YYYY-MM-DD (or YYYY/MM/DD) because comparison operators work on them.
If you are sure that only those 2 formats (yyyy/mm/dd and dd/mm/yyyy) exist in the data, then you could probably get away with a CASE statement along the lines of:
CASE
WHEN (SUBSTRING(dateColumn, 5, 1) = '/') THEN CONVERT(datetime, dateColumn, 111)
ELSE CONVERT(datetime, dateColumn, 103)
END

How to apply a particular format of date of birth in a SQL query?

Suppose I want a particular format of date of birth, like (mm-dd-yyyy).
How can I do that in SQL?
Although some people have listed the proper syntax for this in multiple RDBMSs (you really need to indicate which one you're using), I'd like to point out that formatting your data is typically something that should be done by the front end of your application. That's not to say that there's never a reason to do formatting in SQL, but usually it's best to just pass it as a date/time to the front end and let the front end handle how it will represent it to the user. Hopefully, you understand the difference between a date/time and a string.
Use CONVERT function if it is SQL SERVER
Select CONVERT(VARCHAR(10),Birthdate,110) from YourTable
Assuming your RDBMS is SQL Server, you can do the following:
SELECT CONVERT(VARCHAR(10), [DateOfBirth], 110)
There's more information about date formatting here.
SQL Server
SELECT convert(varchar, getdate(), 110)
SELECT convert(varchar, DateOfBirth, 110) FROM YourTable
How to format datetime & date in Sql Server 2005
If you're using Oracle:
select to_char(your_date_column,'MM-DD-YYYY') from your_date_table;