sql-convert

SQL Server : how to convert binary back to int

家住魔仙堡 提交于 2021-01-29 07:57:44
问题 Admin of our IS stored 8 flags like '00010000' (only true and false) to SQL Server as binary(2) . In this format data has values like '0x1000'. Is possible to convert this binary back to '00010000' ? Convert , Cast , Substring don't work. 回答1: Query returns hexadecimal number (0x... - it is hexadecimal) as its bit mask CREATE TABLE #Temp( Test VARBINARY(2) ) INSERT #Temp VALUES (0x1001), (0x3001), (0x5000), (0x6000), (0xf000), (0xf250) SELECT *, REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE

Convert dd/mm/yyyy to date in SQL Server

痴心易碎 提交于 2020-01-04 06:06:32
问题 I'm going nuts trying to convert a string type column into date. The column name is StartDate , which contains a string date format dd/mm/yyyy . The field type is varchar(3000) . I tried the following: CONVERT(datetime, StartDate, 103) CAST(CONVERT(VARCHAR(10), StartDate, 110) AS DATE) CONVERT(DATE, RIGHT(StartDate, 4) + '-' + SUBSTRING(StartDate, 4, 2) + '-' + LEFT(StartDate, 2), 126) and other similar combinations. I keep getting "out of range" and "conversion failed" error messages. Does

Convert time float to format HH:mm sql server

☆樱花仙子☆ 提交于 2019-12-20 07:47:05
问题 I need to format a float decimal number into a time format hour:minute. I wrote this Scalar-Value Functions with an input float and output varchar(6): CREATE FUNCTIONE formatOre ( @input float ) returns varchar(6) as begin declare @n float; declare @hour int = floor(@input); declare @minutes int = (select (@input - floor(@input)) * 60); declare @val varchar(6) set @val = right('00' + convert(varchar(2), @hour), 2) + ':' + right('00' + convert(varchar(2), @minutes), 2); return @val end It

Conversion of varchar into Datetime

早过忘川 提交于 2019-12-13 03:30:36
问题 Why does the following code work: CAST(DateOfAction AS Date) but this code does not: CAST(DateOfAction AS Datetime) note: DateOfAction is a varchar field Can someone explain this and give the right code? 回答1: Possibly because your VARCHAR field contains a value that might convert to a DATE (no time values), but isn't in any of the valid supported formats that SQL Server CAST and CONVERT support. Since you didn't provide any samples of what your data looks like, we can only guess..... Check

SQL, CONVERT, CAST Data Type?

折月煮酒 提交于 2019-12-12 19:40:37
问题 I am not sure how to use either CAST or CONVERT in this script. SQL says: Conversion failed when converting the varchar value 'Amazing' to data type int. Can someone please tell me how to do this? USE AdventureWorks2012 GO DECLARE @Number01 AS INT DECLARE @Number02 AS INT DECLARE @Number03 AS INT DECLARE @Number04 AS INT DECLARE @Number05 AS INT DECLARE @Number06 AS INT DECLARE @Number07 AS INT DECLARE @Text01 AS VARCHAR (15) SET @Number01 = 150 SET @Number02 = 200 SET @Number03 = 350 SET

How to convert numeric number into datatime

蹲街弑〆低调 提交于 2019-12-11 14:57:59
问题 I need help to convert a bigint value into a datetime value using SQL Server. Example value 634254092190100000 How to convert to datetime value? So far this is what I have but getting an error: Arithmetic overflow error converting expression to data type int select dateadd(s, convert(bigint, 632979854880200000) / 1000, convert(datetime, '1-1-1970 00:00:00')) 回答1: Please do : SELECT CAST((YOURVALUE- 599266080000000000) / 864000000000 AS datetime) For exemple : SELECT CAST((635307578922100000 -

Convert time float to format HH:mm sql server

眉间皱痕 提交于 2019-12-02 13:16:39
I need to format a float decimal number into a time format hour:minute. I wrote this Scalar-Value Functions with an input float and output varchar(6): CREATE FUNCTIONE formatOre ( @input float ) returns varchar(6) as begin declare @n float; declare @hour int = floor(@input); declare @minutes int = (select (@input - floor(@input)) * 60); declare @val varchar(6) set @val = right('00' + convert(varchar(2), @hour), 2) + ':' + right('00' + convert(varchar(2), @minutes), 2); return @val end It looks like great, but not for everything records. This is my output: select formatOre (0) ---> 00:00 select

Custom Date/Time formatting in SQL Server

两盒软妹~` 提交于 2019-11-26 17:33:22
I am trying to write a stored procedure which selects columns from a table and adds 2 extra columns to the ResultSet. These 2 extra columns are the result of conversions on a field in the table which is a Datetime field. The Datetime format field has the following format 'YYYY-MM-DD HH:MM:SS.S' The 2 additional fields which should be in the following format: DDMMM HHMMT, where T is 'A' for a.m. and 'P' for p.m. Example: If the data in the field was '2008-10-12 13:19:12.0' then the extracted fields should contain: 12OCT 0119P I have tried using CONVERT string formats, but none of the formats

Custom Date/Time formatting in SQL Server

早过忘川 提交于 2019-11-26 05:29:27
问题 I am trying to write a stored procedure which selects columns from a table and adds 2 extra columns to the ResultSet. These 2 extra columns are the result of conversions on a field in the table which is a Datetime field. The Datetime format field has the following format \'YYYY-MM-DD HH:MM:SS.S\' The 2 additional fields which should be in the following format: DDMMM HHMMT, where T is \'A\' for a.m. and \'P\' for p.m. Example: If the data in the field was \'2008-10-12 13:19:12.0\' then the

How to convert DateTime to VarChar

杀马特。学长 韩版系。学妹 提交于 2019-11-26 02:38:39
I am working on a query in Sql Server 2005 where I need to convert a value in DateTime variable into a varchar variable in yyyy-mm-dd format (without time part). How do I do that? With Microsoft Sql Server: -- -- Create test case -- DECLARE @myDateTime DATETIME SET @myDateTime = '2008-05-03' -- -- Convert string -- SELECT LEFT(CONVERT(VARCHAR, @myDateTime, 120), 10) Here's some test sql for all the styles. DECLARE @now datetime SET @now = GETDATE() select convert(nvarchar(MAX), @now, 0) as output, 0 as style union select convert(nvarchar(MAX), @now, 1), 1 union select convert(nvarchar(MAX),