How to convert varchar to date - sql

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)

Related

HOW TO INSERT DATE WITH DOTS FORMAT IN SQL

I want to insert a date like 31.12.2021 in SQL, and I get something wrong
what should I write to make it works?
In sql-server you can use convert(varchar, [date you want to convert], 104) if your column is a date datatype as shown in microsoft sql-server cast and convert docs.

Convert decimal to date format SQL

I am trying to convert a column FC_FROM_DATE containing decimal values (ex. 20,200,721) into a date format 2020/07/21.
I have tried this code
SELECT TO_DATE(CHAR(CAST(FC_FROM_DATE AS DECIMAL(8,0))), 'YYYY/MM/DD')
FROM MARKETS.FORECAST
I get an error
Argument for chr should be between 0 and 127
Would much appreciate your help!
If you are using Oracle then you can use the following query:
SELECT TO_DATE(CAST(FC_FROM_DATE AS VARCHAR(8), 'YYYY/MM/DD') FROM Table
Seeing as the persisted format is YYYYMMDD you could convert the value to a varchar(8) and then use CONVERT to get a Date instance.
SELECT CONVERT(DATE, CAST(FC_FROM_DATE AS VARCHAR(8))) FROM MARKETS.FORECAST
Ideally Dates are stored as Date and a Date with a time component is stored as DATETIME2 (or equivalent if not Sql Server).
Test of the code above
DECLARE #FC_FROM_DATE decimal(8,0) = 20200721
SELECT CONVERT(DATE, CAST(#FC_FROM_DATE AS VARCHAR(8)))
2020-07-21
Disclaimer: This works in MS Sql Server.

How to get datepart from datetime in a Column

I want to convert the column (EXECUTION_LOCAL_DATE_TIME) which has datetime format as (YYYY-MM-DD hh:mm:ss.nnnnnnn) to format (YYYY-MM-DD). Ho do i get this. I am working on SQL server management studio
If your intention is to just get the DATE part of a DATETIME then you can just convert the format to DATE (note that this will return a 'Date' datatype, not specifically formatted to a string 'YYYY-MM-DD'.)
eg:
DECLARE #Dt DATETIME = '2019-01-25T12:00:00'
SELECT CONVERT(DATE, #Dt)
Will return '2019-01-25'
You want the CAST() function:
https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-2017
Ideally you should return the datetime format from your query and let your presentation layer handle formatting.
However if you are using SQL server 2012 or higher then you can use the Format() function. See the below answer:
Convert Date format into DD/MMM/YYYY format in SQL Server
If you're using SQL Server 2012 or higher you can use the FORMAT() function.
In your case you'd need
SELECT FORMAT(EXECUTION_LOCAL_DATE_TIME, 'yyyy-MM-dd') FROM TABLE_NAME
You can find additional info here
https://www.mssqltips.com/sqlservertip/2655/format-sql-server-dates-with-format-function/
https://learn.microsoft.com/en-us/sql/t-sql/functions/format-transact-sql?view=sql-server-2017
Here Is the Code Worked For me
SELECT convert(varchar, EXECUTION_LOCAL_DATE_TIME, 111) from Tablename
The Execution_Local_Date_Time will be Converted to yyyy/mm/dd format.

How to get date from sql server in user specified format

I want get date from sql server in user specified format using GETDATE() function.
if i give this query
select GETDATE()
then it is displating output date in this format
2015-03-17 07:29:58.377
but i want output like this .
2015-03-17
what statement should be added with query to get the result.
help me from this problem.
Just use convert():
select convert(varchar(10), getdate(), 121)
Just have a look to the following link. which provides more conversion options.
https://msdn.microsoft.com/en-us/library/ms187928.aspx
In SQL Server 2012 onward you may use FORMAT() which is a bit more intuitive than recalling style numbers.
e.g.
select FORMAT( GETDATE() , 'yyyy-MM-dd' )
see: https://msdn.microsoft.com/en-AU/library/hh213505.aspx
however do note the second parameter is case sensitive
'yyyy-MM-dd' works
'YYYY-MM-DD' would only work for the MM

SQL CE SELECT Convert

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