i have a table in DB and columns like
01-03-2013
01-04-2013
02-03-2013
i want show order by like
01-03-2013
02-03-2013
01-04-2013
Please help me.
Most probably your column is not stored as one of the date datatype this is being sorted as a string based on ASCII codes
You can cast your column for sorting.
SELECT * FROM tbl
ORDER BY CONVERT(datetime, userColumn, 106)
otherwise it's straight forward
SELECT * FROM tbl
ORDER BY userColumn
If your column contain invalid records as well, you can use default date for such records
SELECT * FROM tbl
ORDER BY CONVERT(datetime,
CASE ISDATE(userColumn)
WHEN 1 THEN userColumn
ELSE '01-01-1900'
END, 106)
Related
I currently have a table in SQL Server called masterTable. What I'd like to do is to add a new column to this table called report_Date.
I would like to store the results of a SELECT query in the report_Date column. The query works fine (it converts a nvarchar into a desired date format). What I can't do is append the results to the column.
My query:
SELECT
CONVERT(varchar(10), CAST(FileName AS date), 103) AS FileName
FROM dbo.masterTable
Any suggestions please?
If I understand your question correctly then
UPDATE dbo.masterTable SET
report_Date = CONVERT(varchar(10), CAST(FileName as date), 103)
I have two columns with date values.I'd like to filter them to see the result only when the two columns have similar values.
I have two questions in "Where" part. Can anyone help me with this?
1)How can i compare the value between this two column with date values?
2)If i have varchar value instead of dates, how can i compare two values?
SELECT [USERNAME], count(*) AS [NumberOfHappening], min([date1]) AS [FirstDate], max([date2]) AS [SecondDate]
FROM TableMain
WHERE CAST([FirstDate] AS DATE) = CAST([SecondDate] AS DATE)
GROUP BY [USERNAME]
ORDER BY 'NumberOfHappening' DESC
Thanks.
Are the orginal date values appropriately typed or do you store date/time-values in string columns? If so, you should really change this...
If I get this correctly, you want to find records, where date1 and date2 are on the same day. Casting a DATETIME to DATE will get rid of the time portion.
You can use a CTE to use the column aliases directly
;WITH cte AS
(
SELECT [USERNAME], count(*) AS [NumberOfHappening], min([date1]) AS [FirstDate], max([date2]) AS [SecondDate]
FROM TableMain
GROUP BY [USERNAME]
)
SELECT *
FROM cte
WHERE CAST([FirstDate] AS DATE) = CAST([SecondDate] AS DATE)
ORDER BY NumberOfHappening DESC;
How can I create a custom auto-generated ID in this format:
yyyymmdd-xxxxx
where:
yyyymmdd is the present date and
xxxxx is an auto increment integer number starting from 0
and the next day xxxxx should be restarted to 0.
Thanks
Create one autonumeric ID column in your db
save the date/time of each insert row.
Then use a ROW_NUMBER() function
SELECT *,
dayField + '-' + CAST(rn AS VARCHAR(100))
FROM (
SELECT ID, dateTimeField,
-- truncate the time and convert to yyyymmdd
CONVERT(VARCHAR(10), cast(dateField As Date), 112) as dayField,
ROW_NUMBER() OVER (PARITION BY cast(dateField As Date)
ORDER BY dateTimeField) as rn
-- or just ORDER BY ID
) T
Now if you want save this on the db, you probably will need a trigger.
You might consider including an Identity column and a datetime column that will be set on insert and than add a calculated column to put the two together.
Create table #temp
(
OrderDate datetime
)
insert into #temp values ('01/21/2015'),('01/20/2014'),('11/12/2013')
select distinct convert(varchar(10),orderdate,101) as OrderDate from #temp
order by convert(varchar(10),orderdate,101) asc
The above query gives me the result like below:
OrderDate
01/20/2014
01/21/2015
11/12/2013
But I want the result like below:
OrderDate
11/12/2013
01/20/2014
01/21/2015
The above is just a sample on which I am trying to do sorting on format 101. In my actual query I need to use distinct keyword and also the columns will come dynamically in the select statement by using parameter.
I can't use group by in my actual query.
Please help.
UPDATE
Referring to your comments the only way I've managed to get the UNIQUE results with only one column orderdate converted to VARCHAR 101 representation while still sorting it according to DATETIME sort order, was using a little workaround with GROUP BY clause:
SELECT
CONVERT(VARCHAR(10), A.OrderDate, 101) as orderdate
FROM
#temp AS A
GROUP BY
CONVERT(VARCHAR(10), A.OrderDate, 101)
ORDER BY
MAX(A.OrderDate) ASC
MAX(A.OrderDate) should always give you the exactly equal value to the value of every group, so it shouldn't be an improper way - I've put a working example with repeats under the following link on SQL Fiddle.
Still maybe the previous two-columned solution would happen to occur helpful:
select distinct
convert(varchar(10),orderdate,101) as OrderDateConverted,
orderdate
from
#temp
order by
orderdate asc
The above query sorts your query results according to DATETIME datatype whereas order by convert(varchar(10),orderdate,101) caused the alphanumeric sort order.
You can use subQuery as follows to solve the issue.
SELECT t.OrderDate FROM (
SELECT distinct Convert(VARCHAR(10), orderdate, 101) AS OrderDate
from #temp ) t
order by cast(t.OrderDate AS DATETIME) asc
I have a table with more than one date column,
each date column hold a date or null value,
I want to write a SQL query which will display each column have date into a new row with a new additional column named LogDate that contain the same date of column.
Its difficult to explain, please referrer the attached image.
Just use UNION ALL to concatenate the three result sets:
SELECT [ReceivedDate] AS LogDate, * FROM MyTable WHERE [ReceivedDate] IS NOT NULL
UNION ALL
SELECT [Closing Date] AS LogDate, * FROM MyTable WHERE [Closing Date] IS NOT NULL
UNION ALL
SELECT [LPODate] AS LogDate, * FROM MyTable WHERE [LPODate] IS NOT NULL
To sort by LogDate simply add the following ORDER BY clause to the end of this query:
ORDER BY LogDate