Get Formatted Date in SQL Server CE - sql

How to get the formatted date in SQL Server CE?
I have a column on a table that contains Date, but the column type is nvarchar
ID Date
----------------------
1 05/08/2012
2 10/08/2012
3 05/10/2012
The date format is MM/dd/yyyy, but it is in nvarchar.
Also, I want to have a WHERE clause to select the ID on a specific date. How can I do that? This is for SQL Server CE. Thank you very much.

you need to cover it use convert or cast function ...
cast(datecolumn as DateTime)
or
CONVERT(datetime,datecolumn,101)

For current date, you can use:
SELECT CONVERT(nvarchar,GETDATE(),101)
Output: (As of answered date)
05/31/2016

Related

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.

Need to select last week from nvarchar column

I have found some fixes about this easily. But I can not change the date column datatype from nvarchar to date in SQL Server. It is a deal for me to select between last 7 days.
How can I do this ?
You must use convert date from dd-mm-yyyy to yyyy-mm-dd in SQL Server.
SELECT CONVERT(date,'30-01-2015',103)
First you should be convert and update yyyy-mm-dd your row. After that you can easily get last 7 days.

Find rows in SQL that with date when it is stored as datetime?

My problem is this (using SQL Server 2008 R2).
There is some date columns with types as datetime.
So the original intention with the column was to store a date without any time.
Then datetime was chosen as datatype.
Sure it works but as the database is also logically connected to a UML-diagram I want to use the right datatype.
An example the column Parcel.DateofArrival has the type datetime.
There maybe rows that are
2011-08-05 00:00:00.000
this is a date. But if there is rows like
2011-08-05 07:30:00.000
it is a datetime.
Now I want to find a query that list rows only containing dates, not datetime.
Any hint ?
An easy way to do this is:
select p.*
from Parcel p
where DateOfArrival = cast(DateOfArrival as Date);
By casting the value to a date, the datetime portion is lost. If the original value equals this, then there is no time component.
A way to do this would be to convert it in your select statement, for example:
SELECT CONVERT(DATE, DateOfArrival) Date, *other columns*
FROM Parcel P
Another solution, would be converting it to VARCHAR, formatting it in a yyyyMMdd format, like:
SELECT CONVERT(VARCHAR(8), DateOfArrival, 112) Date, *other columns*
FROM Parcel P
And if you want to sort it or group it, you have to use the conversion.
I hope it was helpful!
select p.*
from Parcel p
where cast(DateOfArrival as time) = '00:00'

SQL datetime LIKE select - why do I need an extra %?

Can someone explain to me why when I perform a LIKE select in SQL (T-SQL) on a varchar column I can do the following:
SELECT *
FROM Table
WHERE Name LIKE 'Th%'
to get names beginning with Th, but when I do the same on a datetime column I need a % before the year, like:
SELECT *
FROM Table
WHERE Date LIKE '%2013%'
to get dates in 2013. The datetimes are stored in yyyy-MM-dd hh:mm:ss format. I know I could use a DATEPART style query but I was just interested in why I need the extra % here.
The DATETIME is converted to a VARCHAR before the comparison, and there definitely is no guarantee that the conversion will be in the pattern you mention. DATETIME is not stored internally as a VARCHAR but as a FLOAT.
You should stop wondering because the syntax is not useful.
SELECT *
FROM Table
WHERE Date LIKE '%2013%'
Will give you a full table scan because the date will be converted to a varchar when comparing. In other words, don't do it !
Use this syntax instead:
SELECT *
FROM Table
WHERE Date >= '2013-01-01T00:00:00'
and Date < '2014-01-01T00:00:00'
If the Date field is in timestamp:-
SELECT *
FROM Table
WHERE year(Date) = '2013'
The sql server converts datetime to this format (Jan 1, 1900 9:20AM.)Because of that reason We need to use an extra %.
If you want to search the records start with month Jan
you can use following query for date time
SELECT *
FROM Table
WHERE Date LIKE 'Jan%'.
No need of extra '%'.

In SQL, how do I convert a date field into a string if null?

I would like to write an SQL query in SQL Server 2008 R2 that converts a date to a string when it is NULL. For example...
Date ShipmentRef RecieptNo
2009-01-01 03:12:11.596 DS298-YYY 18060
FM298-YYY 95464
2010-11-11 08:33:55.974 IL298-YYY 56703
2003-08-01 07:00:44.846 UI835-XYX 40264
US655-YXY 34643
2004-03-07 12:46:33.352 WE242-XXX 83755
The above data is just a sample table of what my current data looks like. When I run the SELECT query, I want it to return the data as follows:
Date ShipmentRef RecieptNo
2009-01-01 03:12:11.596 DS298-YYY 18060
InsertRandomStringHere FM298-YYY 95464
2010-11-11 08:33:55.974 IL298-YYY 56703
2003-08-01 07:00:44.846 UI835-XYX 40264
InsertRandomStringHere US655-YXY 34643
2004-03-07 12:46:33.352 WE242-XXX 83755
I'm not sure which would be better, CASE or CONVERT. Any help you give me will be very much appreciated.
Assuming SQL-Server:
SELECT ISNULL(CONVERT(nVarChar(30), Date, 121), 'InsertRandomStringHere')
DEMO
CAST and CONVERT (Transact-SQL)
COALESCE(datefield, 'InsertRandomStringHere')
(though as others point out, for some DBMS you may need to do additional typecasting operations).
It depends what exactly you want to achieve.
Each value in column has to be the same type (in MS-SQL at least), so all values in Date columns have to be VARCHAR type if you want ''random string'' in case of NULL date.
Then something like:
SELECT CAST(COALESCE(GETDATE(), 'InsertRandomStringHere') AS VARCHAR) AS DATE
UNION
SELECT CAST(COALESCE(NULL, 'InsertRandomStringHere') AS VARCHAR)
should work.
However it could make hard to read values from Date column in your app (if there is an app) on the other end of wire.
SELECT ISNULL(Date, 'InsertRandomStringHere') AS Date
FROM Table
Try this . This is w.r.to mysql
select if(Date <> '',Date,'xxxxxxx'),ShipmentRef,RecieptNo from table